Quote:
Originally Posted by Empowers
PHP Code:
#define AddFlag(%1,%2) ( %1 |= ( 1 << (%2-1) ) ) #define RemoveFlag(%1,%2) ( %1 &= ~( 1 << (%2-1) ) ) #define CheckFlag(%1,%2) ( %1 & ( 1 << (%2-1) ) )
but is this better?
PHP Code:
#define AddFlag(%1,%2) ( %1 |= ( 1 << (%2 % 32) ) ) #define RemoveFlag(%1,%2) ( %1 &= ~( 1 << (%2 % 32) ) ) #define CheckFlag(%1,%2) ( %1 & ( 1 << (%2 % 32) ) )
|
They both do the same thing to prevent a bit-shift of 32 bits. There is no performance change or any other benefit for using either. For a shift of 1-32, -1 will shift 0-31. Using % for a shift of 1-32, it will shift 1-31 normally but when 32 is passed it will shift 0.
The -1 method:
Code:
AddFlag(_,1) = 1 << 0
AddFlag(_,31) = 1 << 30
AddFlag(_,32) = 1 << 31
The modulus method (%):
Code:
AddFlag(_,1) = 1 << 1
AddFlag(_,31) = 1 << 31
AddFlag(_,32) = 1 << 0
__________________