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

[TUT] Bits, Bit-fields, and Bit-wise Operators


Post New Thread Reply   
 
Thread Tools Display Modes
MeRcyLeZZ
Veteran Member
Join Date: Dec 2007
Old 08-06-2011 , 21:22   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #61

Any ideas why this won't compile?
Code:
#include <amxmodx> #define flag_unset(%1,%2) (%1 &= ~( 1 << ( %2 & 31 ) ) ) new g_IsAdmin, g_IsZombie public client_disconnect(id) {     flag_unset(g_IsAdmin, id)     flag_unset(g_IsZombie, id) }
EDIT: Solved, just add a semicolon at the end of the macro:
Code:
#define flag_unset(%1,%2) (%1 &= ~( 1 << ( %2 & 31 ) ) );
__________________

Last edited by MeRcyLeZZ; 08-06-2011 at 21:25. Reason: solved
MeRcyLeZZ is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-06-2011 , 22:37   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #62

Just remove the parenthesis from around the whole expression and it will work.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
vitorrossi
Senior Member
Join Date: Apr 2012
Location: NY, USA
Old 11-15-2012 , 04:46   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #63

PHP Code:
new g_bfAlive;

#define UserAlive(%1)                g_bfAlive |= (1 << (%1 & 31))
#define NotAlive(%1)                g_bfAlive &= ~(1 << (%1 & 31))
#define isAlive(%1)                    g_bfAlive & (1 << (%1 & 31)) 
PHP Code:
if(!isAlive(id))
{ ... 
The if statement gives me tag mismatch. But if I use

PHP Code:
if(isAlive(id) == 0)
{ ... 
Then it works. Why?

Last edited by vitorrossi; 11-15-2012 at 04:47.
vitorrossi is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 11-15-2012 , 04:59   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #64

Add () in the macro.

#define UserAlive(%1) (g_bfAlive |= (1 << (%1 & 31)))
#define NotAlive(%1) (g_bfAlive &= ~(1 << (%1 & 31)))
#define isAlive(%1) (g_bfAlive & (1 << (%1 & 31)))
__________________
Arkshine is offline
vitorrossi
Senior Member
Join Date: Apr 2012
Location: NY, USA
Old 11-15-2012 , 05:09   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #65

Wow, that was simple. Thank you
vitorrossi is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 05-11-2014 , 15:23   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #66

If I have this:
Code:
#define SetKnife(%1,%2)         (g_iKnifeBit[ %1 ] |= (1<<%2)) #define RemoveKnife(%1,%2)      (g_iKnifeBit[ %1 ] &= ~(1<<%2)) #define HasKnife(%1,%2)     (g_iKnifeBit[ %1 ] & (1<<%2))

Can anyone write me a macro that will give me the next set bit value and start again from the beginning if it reaches the end?

Examples (red is the one I start from, green is the one I want to get)

0110 0101 0000 0101 0010 1000 0100 0000
0110 0101 0000 0101 0010 1000 0100 0000
__________________
Currently busy working on a very large scale anime database project.

Last edited by Backstabnoob; 05-11-2014 at 15:24.
Backstabnoob is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-11-2014 , 15:52   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #67

I'm rusty but I think this will work, there are probably better ways to do it maybe using bit masks, but this is a basic way of accomplishing it. I don't have anything on this machine to test it with.

iBitField is the variable holding your bits ... %1 in macros
iCurrentBit is the starting bit (red) ... %2 in macros

returns -1 on no found bits or 0-31 (green) if one is found.

Edit: Just to make sure we are on the same page, I am going in the order of 1<<0 to 1<<31, which is (in single byte form):
8765 4321 in that order.

PHP Code:
GetNextBitiBitField iCurrentBit )
{
    
//iCurrentBit is 13 or 1 << 13

    
new iFoundBit = -1;

    if ( 
iCurrentBit 31 )  
    {
        
//check bits 1 << [14-31]
        
for ( = ( iCurrentBit ) ; 32 i++ )
        {
            if ( 
iBitField & ( << ) )
            {
                
iFoundBit i;
                break;
            }
        }

        if ( 
iFoundBit == -)
        {
            
//check bits 1 << [0-12]
            
for ( iCurrentBit i++ )
            {
                if ( 
iBitField & ( << ) )
                {
                    
iFoundBit i;
                    break;
                }
            }
        }
    }
    else if ( 
iCurrentBit == )
    {
        
//check bits 1 << [1-31]
        
for ( 32 i++ )
        {
            if ( 
iBitField & ( << ) )
            {
                
iFoundBit i;
                break;
            }
        }
    } 

    return 
iFoundBit;

__________________

Last edited by Bugsy; 05-16-2014 at 21:49.
Bugsy is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 05-11-2014 , 17:37   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #68

Thanks, although I figured if the first cycle's done it should always be the first knife because that's the default one which the user always has:

PHP Code:
new iNew
for( new g_iCurrentKnifeiPlayer ] + 1MAX_KNIVES++ )
{
    if( 
HasKnifeiPlayer) )
    {
        
iNew i
    
}
}
// just a little check to make sure the user doesn't keep switching through knives if he only has one
if( iNew != g_iCurrentKnifeiPlayer ] )
{
    
g_iCurrentKnifeiPlayer ] = iNew

Still good to see you, where have you been? o.O
__________________
Currently busy working on a very large scale anime database project.

Last edited by Backstabnoob; 05-11-2014 at 17:38.
Backstabnoob is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-16-2014 , 21:34   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #69

Quote:
Originally Posted by Backstabnoob View Post
Still good to see you, where have you been? o.O
I just don't spend much time behind a computer anymore. I live with my gf now so my time is spent elsewhere. I try to stop in occasionally but I don't have the time to write plugins. I do miss these boards though.
__________________
Bugsy is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 05-20-2014 , 06:25   Re: [TUT] Bits, Bit-fields, and Bit-wise Operators
Reply With Quote #70

Quote:
Originally Posted by RemaKe` View Post
Great tutorial!

Just a question:
PHP Code:
new g_IsAdmin//Only need a single cell of memory

public client_putinserverid )
{
    if ( 
is_user_adminid ) ) 
            
g_IsAdmin |= ( << ( id 31 ) );


Why is "id & 31" required if the result is "id" anyways?
For example, if you want an integral value to never exceed 255, you can do the next thing:

PHP Code:
write_byte(Value 255); 
So, if Value is 256 then Value equals to 0.
If value is 1000 then Value equals to 232.
__________________

Last edited by claudiuhks; 05-20-2014 at 06:28.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
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 19:57.


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