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)

joaquimandrade 04-20-2009 13:43

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Exolent[jNr] (Post 809946)
Ahem, andrade.
"continue" is never called because the if() statement never returns true.

Code:
array[32] = false bitsum &= ~(1<<32)

Dear Exolent you are right. So the code do not match the profile.

Emp` 04-20-2009 15:23

Re: [INFO] Bitsums and Operators
 
If I have
Code:

(1<<CSW_*)
Is there a way to get the CSW_* ?

PM 04-20-2009 16:58

Re: [INFO] Bitsums and Operators
 
If you were doing this in x86 assembly you'd use the bsr (bit search right) instruction.
In pawn I guess your best bet is looping and dividing by two (or shifting right by one) until the number becomes 0, and counting the number of divisions/shifts performed.

Arkshine 04-20-2009 17:57

Re: [INFO] Bitsums and Operators
 
Yep, I know only this way : do ++x; while ( ( bitsum /= 2 ) >= 1 )

joaquimandrade 04-20-2009 18:05

Re: [INFO] Bitsums and Operators
 
PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN    "New Plugin"
#define AUTHOR    "Unknown"
#define VERSION    "1.0"

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)    
    
register_clcmd("some","some");    
}

public 
some(id)
{
    new 
value[]  = << 20 << 13 << 29
    
    
for(new i=0;i<4;i++)
    {
        if(
value{i})
        {
            for(new 
j=0;j<8;j++)
            {            
                if(
value{i} & (<< j))
                {
                    
client_print(id,print_chat,"Has (1 << %d)",(8*(i)) + j)
                }
            }
        }
    }


or

PHP Code:

public some(id)
{
    new 
value[]  = ~(<< 29)
    
    for(new 
i=0;i<4;i++)
    {
        if(
value{i})
        {
            for(new 
j=7;j>=0;j--)
            {            
                if(
value{i} & (<< j))
                {
                    
client_print(id,print_console,"Has (1 << %d)",(8*(3-i)) + j)
                }
            }
        }
    }


or

PHP Code:

public some(id)
{
    new 
value[]  = ~(<< 3)
    
    new 
index
    
    
for(new i=3;i>=0;i--)
    {
        if(
value{i})
        {
            for(new 
j=0;j<=7;j++,index++)
            {            
                if(
value{i} & (<< j))
                {
                    
client_print(id,print_console,"Has (1 << %d)",index)
                }
            }
        }
        else
            
index += 8;
    }



Bugsy 04-20-2009 21:49

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Emp` (Post 810243)
If I have
Code:

(1<<CSW_*)
Is there a way to get the CSW_* ?

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

public Test()
{
    new 
iWeapList;
    new 
szWeap[41];

    
AddFlagiWeapList CSW_AWP );
    
AddFlagiWeapList CSW_DEAGLE );
    
AddFlagiWeapList CSW_SCOUT );
    
AddFlagiWeapList CSW_HEGRENADE );
    
    for ( new 
32 i++ )
    {
        if ( 
CheckFlagiWeapList ) )
        {
            
get_weaponnameszWeap 40 );
            
server_print("Found %s" szWeap );
        }
    }



Exolent[jNr] 04-20-2009 22:05

Re: [INFO] Bitsums and Operators
 
You should also add the '^' operator to this topic.
It switches the bit.

Code:
new bool:someBool[33]; public switchVar(client) {     someBool[client] = !someBool[client]; }
:arrow:
Code:
new someBitsum; public switchVar(client) {     someBitsum ^= (1 << (client - 1)); }

danielkza 04-20-2009 22:23

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Exolent[jNr] (Post 810450)
You should also add the '^' operator to this topic.
It switches the bit.

Code:
new bool:someBool[33];

public switchVar(client) { someBool[client] = !someBool[client];
}


:arrow:
Code:
new someBitsum;

public switchVar(client) { someBitsum ^= (1 << (client - 1));
}


It can also check if a flag is present in one, and only one, of the compared bitsums. It's not used often, but it may be useful.

Code:

new iFlag1 = (1<<0)
new iFlag2 = (1<<0)

new iResult = iFlag1 ^ iFlag2 // = 0


Bugsy 04-20-2009 22:48

Re: [INFO] Bitsums and Operators
 
http://en.wikipedia.org/wiki/Bitwise_operation

joaquimandrade 04-21-2009 03:13

Re: [INFO] Bitsums and Operators
 
Stupid trivia. Why does this function:
PHP Code:

public some(id)
{
    static const 
indexes[] = {0,0x18,0x10,0x8};
    
    new 
value[]  = !"@@ "
    
    
new index
    
    
for(new i=3;i>=0;i--)
    {
        if(
value{i})
        {
            for(new 
j=0;j<=7;j++,index++)
            {            
                if(
value{i} & (<< j))
                {
                    
client_print(id,print_console,"Has (1 << %d)",index)
                }
            }
        }
        else
            
index indexes[i];
    }


Outputs:

Code:

Has (1 << 13)
Has (1 << 22)
Has (1 << 30)

?


All times are GMT -4. The time now is 17:49.

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