Raised This Month: $106 Target: $400
 26% 

[INFO] Bitsums and Operators


Post New Thread Reply   
 
Thread Tools Display Modes
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-19-2009 , 16: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 16:23.
Bugsy is offline
Dr.G
Senior Member
Join Date: Nov 2008
Old 04-19-2009 , 16:24   Re: [INFO] Bitsums and Operators
Reply With Quote #12

well its completly nonsence to me, i would never do all that crap in my codes... But thanks for info ;)
__________________
Dr.G is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-19-2009 , 16:33   Re: [INFO] Bitsums and Operators
Reply With Quote #13

Quote:
Originally Posted by Bugsy View Post
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
__________________
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-19-2009 , 16:47   Re: [INFO] Bitsums and Operators
Reply With Quote #14

Quote:
Originally Posted by joaquimandrade View Post
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) ) ) 
__________________
Bugsy is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 04-19-2009 , 18:38   Re: [INFO] Bitsums and Operators
Reply With Quote #15

Nice Tutorial
I Will test this method
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
vittu
SuperHero Moderator
Join Date: Oct 2004
Location: L.A. County, CA
Old 04-19-2009 , 19:27   Re: [INFO] Bitsums and Operators
Reply With Quote #16

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.
vittu is offline
Send a message via AIM to vittu Send a message via MSN to vittu Send a message via Yahoo to vittu
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-19-2009 , 20:11   Re: [INFO] Bitsums and Operators
Reply With Quote #17

Quote:
Originally Posted by ot_207 View Post
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 View Post
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.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-20-2009 , 00:48   Re: [INFO] Bitsums and Operators
Reply With Quote #18

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.
__________________

Last edited by Bugsy; 04-20-2009 at 00:52.
Bugsy is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 04-20-2009 , 02:51   Re: [INFO] Bitsums and Operators
Reply With Quote #19

Quote:
Originally Posted by fysiks View Post
I see bits shifted left.
I see bits shifted right.
Fixed!
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
Empowers
BANNED
Join Date: Feb 2009
Location: Ukraine
Old 04-20-2009 , 05:27   Re: [INFO] Bitsums and Operators
Reply With Quote #20

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

Last edited by Empowers; 04-20-2009 at 05:40.
Empowers is offline
Send a message via ICQ to Empowers
Old 04-20-2009, 05:29
Empowers
This message has been deleted by Empowers. Reason: sorry :)
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:25.


Powered by vBulletin®
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Theme made by Freecode