Raised This Month: $51 Target: $400
 12% 

[INFO] Bitsums and Operators


Post New Thread Reply   
 
Thread Tools Display Modes
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 05-16-2009 , 14:50   Re: [INFO] Bitsums and Operators
Reply With Quote #51

Quote:
Originally Posted by joaquimandrade View Post
If someone is interested on knowing how to use bit storage for values bigger than 31:

PHP Code:
save(x)
{
    
holder[32] |= << (32);
}
 
exists(x)
{
    return 
holder[32] & << (32);
}
 
remove(x)
{
    
holder[32] &= ~(<< (32))

It's a memory tradeoff.
Nice! I was thinking on adding this in the tutorial.
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
Old 05-16-2009, 15:03
joaquimandrade
This message has been deleted by joaquimandrade. Reason: not
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-16-2009 , 15:11   Re: [INFO] Bitsums and Operators
Reply With Quote #52

Quote:
Originally Posted by ot_207 View Post
Nice! I was thinking on adding this in the tutorial.
You understood its purpose?
__________________
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-16-2009 , 20:38   Re: [INFO] Bitsums and Operators
Reply With Quote #53

*Revised, fixed 'Loose Indentation' warnings*

I put a little plugin together just to test it out. Here it is for anyone interested.

PHP Code:
#include <amxmodx>
#include <amxmisc>

#define PLUGIN "> 32 Bit Flag Example"
#define VERSION "1.0"
#define AUTHOR "bugsy"

//Max bit we want to manipulate
#define MAX_BIT    515

//Allocate an array large enough to manipulate our MAX_BITS bit
new iBitsumArray[ ( MAX_BIT 32 ) + ];

//Macros to manipulate the bit
#define SetBit(%1,%2)       ( %1[%2/32] |= (1 << (%2 % 32)) )
#define ClearBit(%1,%2)    ( %1[%2/32] &= ~(1 << (%2 % 32)) )
#define CheckBit(%1,%2)     ( %1[%2/32] & (1 << (%2 % 32)) )

public plugin_init() 
{
    
register_pluginPLUGIN VERSION AUTHOR );
    
register_concmd"test" "cmdTest" );
}

public 
cmdTest(id)
{
    
//Show max bit we can manipulate
    
server_print"Max Bit = %d" MAX_BIT );
    
    
//Show the size of our bitsum array
    
server_print"Bitsum array size = %d" sizeof iBitsumArray );
    
    
//Check out if our max is ok (no errors)
    
if ( !CheckBitiBitsumArray MAX_BIT ) ) server_print"%d bit is not set" MAX_BIT );
    
    
//Check if 512 or 515 are set
    
if ( !CheckBitiBitsumArray 512 ) ) server_print"512 bit is not set" );
        
    if ( !
CheckBitiBitsumArray 515 ) ) server_print"515 bit is not set" );
    
    
//Set 512 and 515
    
SetBitiBitsumArray 512 );
    
SetBitiBitsumArray 515 );
    
    
//Check them again
    
if ( CheckBitiBitsumArray 512 ) ) server_print"512 bit is set" );
    if ( 
CheckBitiBitsumArray 515 ) ) server_print"515 bit is set" );
    
    
//Remove 512 and 515
    
ClearBitiBitsumArray 512 );
    
ClearBitiBitsumArray 515 );
    
    
//Check again
    
if ( !CheckBitiBitsumArray 512 ) ) server_print"512 bit is not set" );
    if ( !
CheckBitiBitsumArray 515 ) ) server_print"515 bit is not set" );

__________________

Last edited by Bugsy; 05-21-2009 at 09:03.
Bugsy is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-16-2009 , 20:55   Re: [INFO] Bitsums and Operators
Reply With Quote #54

Nice. Connor is also using this storage way in a plugin:

http://forums.alliedmods.net/showthread.php?t=90242

In fact it was him who gave me the idea to do that.
__________________
joaquimandrade is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 05-18-2009 , 04:41   Re: [INFO] Bitsums and Operators
Reply With Quote #55

Quote:
Originally Posted by joaquimandrade View Post
You understood its purpose?
When I did the tutorial I thought about that, I just forgot to post it .

Quote:
Originally Posted by Bugsy View Post
I put a little plugin together just to test it out. Here it is for anyone interested.
Thanks Bugsy!
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 05-22-2009 , 15:26   Re: [INFO] Bitsums and Operators
Reply With Quote #56

I found a use for the example on bits being pushed more than 32, and I used it to make my plugin.
This is a really handy tutorial!
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 05-22-2009 , 19:02   Re: [INFO] Bitsums and Operators
Reply With Quote #57

Quote:
Originally Posted by joaquimandrade View Post
In fact it was him who gave me the idea to do that.
In fact, you stole my ideas since the begining of the creation of the universe.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-22-2009 , 22:31   Re: [INFO] Bitsums and Operators
Reply With Quote #58

Quote:
Originally Posted by ConnorMcLeod View Post
In fact, you stole my ideas since the begining of the creation of the universe.
traitor
__________________
joaquimandrade is offline
Damizean
SourceMod Donor
Join Date: Mar 2009
Old 05-24-2009 , 06:25   Re: [INFO] Bitsums and Operators
Reply With Quote #59

Actually, since 32 is a power of two value, you can use bit displacements and ands for doing those operations, it's way cheaper than doing divisions and modulos, and this rule always applies to power of two values:
PHP Code:
holder[>> 5] |= << (31); 

Last edited by Damizean; 05-24-2009 at 06:28.
Damizean is offline
Send a message via AIM to Damizean Send a message via MSN to Damizean
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-24-2009 , 09:55   Re: [INFO] Bitsums and Operators
Reply With Quote #60

Quote:
Originally Posted by Damizean View Post
Actually, since 32 is a power of two value, you can use bit displacements and ands for doing those operations, it's way cheaper than doing divisions and modulos, and this rule always applies to power of two values:
PHP Code:
holder[>> 5] |= << (31); 
Tested and working, nice find Damizean.

Here are the macros, all tested with my above "> 32 Bit Flag Example" plugin
PHP Code:
#define SetBit(%1,%2)          ( %1[%2>>5] |= (1 << (%2 & 31)) )
#define ClearBit(%1,%2)        ( %1[%2>>5] &= ~(1 << (%2 & 31)) )
#define CheckBit(%1,%2)        ( %1[%2>>5] & (1 << (%2 & 31)) ) 
Code:
Max Bit = 515
Bitsum array size = 17
515 bit is not set
512 bit is not set
515 bit is not set
512 bit is set
515 bit is set
512 bit is not set
515 bit is not set
__________________
Bugsy is offline
Reply



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 12:50.


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