AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Do variable equal to each of these numbers? (https://forums.alliedmods.net/showthread.php?t=183051)

Diegorkable 04-16-2012 13:25

Do variable equal to each of these numbers?
 
Hey guys,

If I want to check if a variable equal to 1 of for example 10 numbers, I think doing:

if ( var == 5 || var == 6 || var == 7 ..... )
because then it would be hell long, and if I needed to check 50 numbers?

so... I was wondering. Is it possible to do:

new var = ( 1 | 2 | 3 )

if ( 2 & var )

or something like that?
help me out here :X

Devil259 04-16-2012 13:42

Re: Do variable equal to each of these numbers?
 
if( 1 <= var <= 50 )

This is the same as

if( var == 1 || var == 2 || var == 3 || ... || var == 50 )

Diegorkable 04-16-2012 13:55

Re: Do variable equal to each of these numbers?
 
Quote:

Originally Posted by Devil259 (Post 1690559)
if( 1 <= var <= 50 )

This is the same as

if( var == 1 || var == 2 || var == 3 || ... || var == 50 )

and if I want to check all 50 except 3 12 23 27 38 40 59? (giving random numbers)

my question is, is it possible to do

new var = ( 1 | 2 | 3 )
new cmp = 2

if I do...

if ( cmp & var )
will it be true?

claudiuhks 04-16-2012 13:58

Re: Do variable equal to each of these numbers?
 
PHP Code:

new found_at 0, var = 15;

for( new 
= -102012i++ )
{
  if( 
== var )
  {
    
found_at i;

    break;
  }
}

// Output for found_at: 15 

Or, with bytes:

PHP Code:

enum
{
  
CL_CONNECTED 1,
  
CL_BOT,
  
CL_INGAME
};

new 
0;

|= CL_CONNECTED CL_BOT// attrib
if( CL_CONNECTED ) { } // check
&~= CL_CONNECTED// remove 


Diegorkable 04-16-2012 14:00

Re: Do variable equal to each of these numbers?
 
Quote:

Originally Posted by claudiuhks (Post 1690567)
PHP Code:

new found_at 0, var = 15;

for( new 
= -102012i++ )
{
  if( 
== var )
  {
    
found_at i;

    break;
  }
}

// Output for found_at: 15 

Or, with bytes:

PHP Code:

enum
{
  
CL_CONNECTED 1,
  
CL_BOT,
  
CL_INGAME
};

new 
0;

|= CL_CONNECTED CL_BOT// attrib
if( CL_CONNECTED ) { } // check
&~= CL_CONNECTED// remove 


Thx :)

Exolent[jNr] 04-16-2012 14:15

Re: Do variable equal to each of these numbers?
 
Quote:

Originally Posted by claudiuhks (Post 1690567)
Or, with bytes:

PHP Code:

enum
{
  
CL_CONNECTED 1,
  
CL_BOT,
  
CL_INGAME
};

new 
0;

|= CL_CONNECTED CL_BOT// attrib
if( CL_CONNECTED ) { } // check
&~= CL_CONNECTED// remove 


Your bytes are wrong.

PHP Code:

enum (<<= 1)
{
    
CL_CONNECTED 1,
    
CL_BOT,
    
CL_INGAME


Or if you want to use the enum you made:

PHP Code:

|= (<< CL_CONNECTED) | (<< CL_BOT);
if(
& (<< CL_CONNECTED)) { }
&= ~(<< CL_CONNECTED


Diegorkable 04-16-2012 14:16

Re: Do variable equal to each of these numbers?
 
NVM GOT IT, it must be with the (1 << X), thanks exolent :)

Exolent[jNr] 04-16-2012 14:19

Re: Do variable equal to each of these numbers?
 
Quote:

Originally Posted by Diegorkable (Post 1690578)
Wait.
Then would it be true?

new item = 2 | 4 | 6
if ( 2 & item )

will it be true??

Yes, but his bytes were:
CL_CONNECTED = 1
CL_BOT = 2
CL_INGAME = 3

The problem is that 3 contains 2 and 1 (if you understand how bits work).
1 | 2 = 3
Therefore, checking CL_INGAME is checking CL_CONNECTED|CL_BOT, which is wrong.
CL_INGAME should be 4, not 3.

Diegorkable 04-16-2012 14:24

Re: Do variable equal to each of these numbers?
 
Quote:

Originally Posted by Exolent[jNr] (Post 1690581)
Yes, but his bytes were:
CL_CONNECTED = 1
CL_BOT = 2
CL_INGAME = 3

The problem is that 3 contains 2 and 1 (if you understand how bits work).
1 | 2 = 3
Therefore, checking CL_INGAME is checking CL_CONNECTED|CL_BOT, which is wrong.
CL_INGAME should be 4, not 3.

Thank you :)

fysiks 04-16-2012 21:13

Re: Do variable equal to each of these numbers?
 
Quote:

Originally Posted by Diegorkable (Post 1690549)
Hey guys,

If I want to check if a variable equal to 1 of for example 10 numbers, I think doing:

if ( var == 5 || var == 6 || var == 7 ..... )
because then it would be hell long, and if I needed to check 50 numbers?

Quote:

Originally Posted by Diegorkable (Post 1690565)
and if I want to check all 50 except 3 12 23 27 38 40 59? (giving random numbers)

These parts of your posts imply using a switch is the best way to go. I'm curious why you got to thinking about using bitwise operations. There are advantages and disadvantages to both methods. What do these numbers mean and are the cases mutually exclusive (meaning that the variable can't be both) which is what is implied by the first half of your original post.


All times are GMT -4. The time now is 07:48.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.