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

Quote:
Originally Posted by joaquimandrade View Post
Bitsums can't be used to replace is_user_alive like you have it, because 1<<32 is out of the bits that a cell can hold.
This is correct, since a 4 byte cell has 32 bits:
1 = 0000 0000 0000 0000 0000 0000 0000 0001

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_adminid ) )
    {
        
//An admin connected, add a bit to the g_Admin bitfield for this players id
        
AddFlagg_Admin id );
    }

    if ( 
is_user_botid ) )
        
AddFlagg_Bot id );
}

public 
client_disconnect(id)
{
    if ( 
CheckFlagg_Bot id ) )
    {
        
//bot disconnected
        
RemoveFlagg_Bot id );
    }
}

public 
YourFunction(id)
{
    if ( !
CheckFlagg_Admin id ) )
    {
        
//You are not an admin
    
}

    if ( !
g_Admin 
        
//There are no admins online!

__________________

Last edited by Bugsy; 04-19-2009 at 15:23.
Bugsy is offline