AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved Bitwise Operation (https://forums.alliedmods.net/showthread.php?t=311400)

ThatKidWhoGames 10-16-2018 09:10

Bitwise Operation
 
Hey all!

So, I am trying to learn how to use bitwise operation as a replacement for booleans (ex. bool g_bEnabled[MAXPLAYERS+1]) (performance optimization).

Here is something I have coded:
PHP Code:

#include <sourcemod>

int g_iClients;

public 
void OnPluginStart()
{
    
RegAdminCmd("sm_testbit"Command_CallbackADMFLAG_ROOT);

    if (
GetClientCount(true))
    {
        for (
int i 1<= MaxClientsi++)
        {
            if (
IsClientInGame(i) && !IsFakeClient(i))
            {
                
OnClientPostAdminCheck(i);
            }
        }
    }
}

public 
void OnClientPostAdminCheck(int client)
{
    
g_iClients |= client;
}

public 
void OnClientDisconnect(int client)
{
    
g_iClients &= ~client;
}

public 
Action Command_Callback(int clientint args)
{
    
ReplyToCommand(client"[SM] g_iClients: %s."g_iClients client "Found" "Not found");
    return 
Plugin_Handled;


I have 2 questions:
1.) Did I add and remove the client indexes correctly with the g_iClients variable?
2.) Would I use:
Code:

g_iClients & client
Or:
Code:

g_iClients & (1 << client)
To check if the client exists in the variable?

Thanks,
Grant

klippy 10-16-2018 10:22

Re: Bitwise Operation
 
Just use arrays, it's okay.

nosoop 10-16-2018 11:07

Re: Bitwise Operation
 
You're or-ing multiple values with shared bits (look at a binary chart). You'd probably want to do (1 << client) every time you add / remove / check for a flag, but otherwise you're using the correct bitwise operations.

Really though, I've also considered doing this before. Just use an array for client indices. Pawn cells are 32-bit values, so your code would be broken on client indices greater than 31 (considering the first client index is 1 you're already not using the rightmost bit). Even TF2 has a high client iindex of 33 (34?) with SourceTV and/or Replay present and a raised maxplayers value.

ThatKidWhoGames 10-16-2018 12:11

Re: Bitwise Operation
 
Quote:

Originally Posted by KliPPy (Post 2619942)
Just use arrays, it's okay.

Quote:

Originally Posted by nosoop (Post 2619945)
You're or-ing multiple values with shared bits (look at a binary chart). You'd probably want to do (1 << client) every time you add / remove / check for a flag, but otherwise you're using the correct bitwise operations.

Really though, I've also considered doing this before. Just use an array for client indices. Pawn cells are 32-bit values, so your code would be broken on client indices greater than 31 (considering the first client index is 1 you're already not using the rightmost bit). Even TF2 has a high client iindex of 33 (34?) with SourceTV and/or Replay present and a raised maxplayers value.

Thanks guys!


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

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