Raised This Month: $32 Target: $400
 8% 

Solved Bitwise Operation


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 10-16-2018 , 09:10   Bitwise Operation
Reply With Quote #1

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

Last edited by ThatKidWhoGames; 10-16-2018 at 12:12.
ThatKidWhoGames is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 10-16-2018 , 10:22   Re: Bitwise Operation
Reply With Quote #2

Just use arrays, it's okay.
__________________
klippy is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 10-16-2018 , 11:07   Re: Bitwise Operation
Reply With Quote #3

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.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 10-16-2018 , 12:11   Re: Bitwise Operation
Reply With Quote #4

Quote:
Originally Posted by KliPPy View Post
Just use arrays, it's okay.
Quote:
Originally Posted by nosoop View Post
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!

Last edited by ThatKidWhoGames; 10-16-2018 at 12:12.
ThatKidWhoGames 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 19:41.


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