Raised This Month: $ Target: $400
 0% 

Do variable equal to each of these numbers?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 04-16-2012 , 13:25   Do variable equal to each of these numbers?
Reply With Quote #1

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
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive
Diegorkable is offline
Devil259
Veteran Member
Join Date: Dec 2009
Location: France (59)
Old 04-16-2012 , 13:42   Re: Do variable equal to each of these numbers?
Reply With Quote #2

if( 1 <= var <= 50 )

This is the same as

if( var == 1 || var == 2 || var == 3 || ... || var == 50 )
__________________
You can do anything you set your mind to, man.

Devil259 is offline
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 04-16-2012 , 13:55   Re: Do variable equal to each of these numbers?
Reply With Quote #3

Quote:
Originally Posted by Devil259 View Post
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?
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive

Last edited by Diegorkable; 04-16-2012 at 13:59.
Diegorkable is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 04-16-2012 , 13:58   Re: Do variable equal to each of these numbers?
Reply With Quote #4

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 
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 04-16-2012 , 14:00   Re: Do variable equal to each of these numbers?
Reply With Quote #5

Quote:
Originally Posted by claudiuhks View Post
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
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive
Diegorkable is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-16-2012 , 14:15   Re: Do variable equal to each of these numbers?
Reply With Quote #6

Quote:
Originally Posted by claudiuhks View Post
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
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 04-16-2012 , 14:16   Re: Do variable equal to each of these numbers?
Reply With Quote #7

NVM GOT IT, it must be with the (1 << X), thanks exolent
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive

Last edited by Diegorkable; 04-16-2012 at 14:23.
Diegorkable is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-16-2012 , 14:19   Re: Do variable equal to each of these numbers?
Reply With Quote #8

Quote:
Originally Posted by Diegorkable View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 04-16-2012 , 14:24   Re: Do variable equal to each of these numbers?
Reply With Quote #9

Quote:
Originally Posted by Exolent[jNr] View Post
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
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive
Diegorkable is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-16-2012 , 21:13   Re: Do variable equal to each of these numbers?
Reply With Quote #10

Quote:
Originally Posted by Diegorkable View Post
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 View Post
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.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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