AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [INFO] Bitsums and Operators (https://forums.alliedmods.net/showthread.php?t=90507)

Bugsy 04-19-2009 15:07

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by joaquimandrade (Post 809439)
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!



Dr.G 04-19-2009 15:24

Re: [INFO] Bitsums and Operators
 
well its completly nonsence to me, i would never do all that crap in my codes... But thanks for info ;)

joaquimandrade 04-19-2009 15:33

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Bugsy (Post 809597)
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!




Bugsy you can also do this: (i don't know how to put a % in a macro)

1 << value % 32

Bugsy 04-19-2009 15:47

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by joaquimandrade (Post 809613)
Bugsy you can also do this: (i don't know how to put a % in a macro)

1 << value % 32

Yes, either way works. Your method using modulus will make the 0 bitshift occur at 32 instead of 1 like my -1 method.

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) ) ) 


AntiBots 04-19-2009 17:38

Re: [INFO] Bitsums and Operators
 
Nice Tutorial :D
I Will test this method :D

vittu 04-19-2009 18:27

Re: [INFO] Bitsums and Operators
 
While I do use bitwise operation I will never take it to the degree that you are proposing. I'd rather prefer legibility over whatever minimal degree of memory this would save (which is probably negligible anyway).

And while I do appreciate the write up, I think it's a bit complex for beginners or casual scripters to understand.

fysiks 04-19-2009 19:11

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by ot_207 (Post 809152)
The 2 operators have a special meaning!
"<< x" means that a position of a number is moved by x points to right! Example:
PHP Code:

"00001100" << "00011000" 


I see bits shifted left.


Quote:

Originally Posted by ot_207 (Post 809152)
The >> operator does the same thing like << but moves x points to left! When using integer variables the numbers disappear.
PHP Code:

"00000111" >> "00000000" 


I see bits shifted right.

Bugsy 04-19-2009 23:48

Re: [INFO] Bitsums and Operators
 
I want to see a profile done on the correctly written bit-field code since the one done by Empowers is incorrect and may be skewing the results (1 << 32); I really want to see any performance difference in array vs. bit operators. The tut has to also be corrected so people don't attempt to use ( 1 << id ) for storing player id's; perhaps add my AddFlag, RemoveFlag, and CheckFlag macros too. :)

ot_207 04-20-2009 01:51

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by fysiks (Post 809714)
I see bits shifted left.
I see bits shifted right.

Fixed! :)

Empowers 04-20-2009 04:27

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by joaquimandrade (Post 809461)
Your it's wrong because you are doing a continue instead of let the looping run. So you are calling 10 times the command and just giving 10 comparisons.

This A result from your code, I profiled it.. Read more carefully my post ;)

And I can't undersatand what u a saying about countinue. It let the loop run just makes i++.

and break stops the loop.




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) ) ) 

Wow that great! Thx
+k :)



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) ) ) 



And I have a question..
For example a have bitsum var.. 1230 and 4000 bit A true..

So should I loop thougth all to find them?? Or maybe there is better way


All times are GMT -4. The time now is 22:03.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.