View Single Post
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-20-2009 , 11:11   Re: [INFO] Bitsums and Operators
Reply With Quote #30

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

Last edited by Bugsy; 04-20-2009 at 11:51.
Bugsy is offline