|
Veteran Member
Join Date: Dec 2008
Location: Portugal
|
04-19-2009
, 15:33
Re: [INFO] Bitsums and Operators
|
#13
|
Quote:
Originally Posted by Bugsy
This is correct, since a 4 byte cell has 32 bits:
1 = 0000 0000 0000 0000 0000 0000 0000 000 1
Doing a left shift of 32 will push the bit out of the bitfield:
1 << 32 = 1 0000 0000 0000 0000 0000 0000 0000 0000
You can make it work to hold player indexes (1-32) by subtracting 1 from the left-shift value so you will always shift 1 less (0-31 instead of 1-32). I used bit-fields in my Admin Hierarchy plugin instead of using arrays of 33 cells. The bit-field method is also nice because you can quickly check if any admins or bots are on the server with a simple !g_Bot or !g_Admin.
I cannot get the profiler to work so I'm not sure which way is ultimately better. The bit-field is better to conserve memory as you are using 1 cell (4 bytes) vs. a 33 cell array (132 bytes) as a storage mechanism for player indexes. On the other hand, the array method may be less CPU expensive but it will obviously utilize more memory.
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) ) )
new g_Admin; new g_Bot;
public client_putinserver(id) { if ( is_user_admin( id ) ) { //An admin connected, add a bit to the g_Admin bitfield for this players id AddFlag( g_Admin , id ); }
if ( is_user_bot( id ) ) AddFlag( g_Bot , id ); }
public client_disconnect(id) { if ( CheckFlag( g_Bot , id ) ) { //bot disconnected RemoveFlag( g_Bot , id ); } }
public YourFunction(id) { if ( !CheckFlag( g_Admin , id ) ) { //You are not an admin }
if ( !g_Admin ) //There are no admins online! }
|
Bugsy you can also do this: (i don't know how to put a % in a macro)
1 << value % 32
__________________
|
|
|
|