Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualServant of the Lord

Posted 02 June 2014 - 07:45 PM

When you AND two values together, every bit that wasn't 1 in *both* values is set to zero. So if you want to check if a certain bit is set, you AND that one bit to a value, and then see if the result is 0 (the bit wasn't present), or if the result is the same what you AND'd.

 

if( (10110 & 10000) == 10000 )

if( (value & mask) == mask )

 

You can drop the " == mask " part because, in C++, non-zero integers automaticly convert to 'true':

if(value & mask) //If the result is 00000, then the statement evaluates to false, otherwise it evaluates to true.

 

 

Hodgeman's func does the same thing, but also uses an index to create the mask by shifting the value 00000001 over N places to become (for example) 00010000.


#2Servant of the Lord

Posted 02 June 2014 - 07:45 PM

When you AND two values together, every bit that wasn't 1 in *both* values is set to zero. So if you want to check if a certain bit is set, you AND that one bit to a value, and then see if the result is 0 (the bit wasn't present), or if the result is the same what you AND'd.

 

if( (10110 & 10000) == 10000 )

if( (value & mask) == mask )

 

Because, in C++, non-zero integers automaticly convert to 'true', you can drop the == mask part:

if(value & mask) //If the result is 00000, then the statement evaluates to false, otherwise it evaluates to true.

 

 

Hodgeman's func does the same thing, but also uses an index to create the mask by shifting the value 00000001 over N places to become (for example) 00010000.


#1Servant of the Lord

Posted 02 June 2014 - 07:42 PM

When you AND two values together, every bit that wasn't 1 in *both* values is set to zero. So if you want to check if a certain bit is set, you AND that one bit to a value, and then see if the result is 0 (the bit wasn't present), or if the result is the same what you AND'd.

 

if( (10110 & 10000) == 10000 )

if( (value & mask) == mask )

 

Hodgeman's func does the same thing, but also uses an index to create the mask by shifting the value 00000001 over N places to become (for example) 00010000.


PARTNERS