Raised This Month: $ Target: $400
 0% 

help with bit


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PawnBegg
Junior Member
Join Date: Apr 2022
Old 04-12-2022 , 16:02   help with bit
Reply With Quote #1

Hello, i cant find info about ->
Code:
#define get_bit(%2,%1) (%1 & (1<<(%2&31)))
#define set_bit(%2,%1) (%1 |= (1<<(%2&31)))
#define rem_bit(%2,%1) (%1 &= ~(1 <<(%2&31)))
what these definitions mean ? Can some one help me to understand what does rem_bit mean in this code?
Code:
public csgo_reset_data()
{
	for (new i = 1; i <= MAX_PLAYERS; i++) rem_bit(i, loaded);

	nvault_prune(operations, 0, get_systime() + 1);
}

public client_disconnected(id)
	rem_bit(id, loaded);
Thanks.
PawnBegg is offline
zXCaptainXz
Member
Join Date: May 2017
Old 04-12-2022 , 17:07   Re: help with bit
Reply With Quote #2

Without getting into too much detail on how it works, you can compare the int "loaded" to a boolean array of size 32. So say you want to check if a player is connected, you can do it like this, note that the bool method is in comments

Code:
new Connected //new bool:Connected[33]

#define get_bit(%2,%1) (%1 & (1<<(%2&31)))
#define set_bit(%2,%1) (%1 |= (1<<(%2&31)))
#define rem_bit(%2,%1) (%1 &= ~(1 <<(%2&31)))

public client_connect(id)
{
	set_bit(id, Connected) // Connected[id] = true;
}

public client_disconnect(id)
{
	rem_bit(id, Connected) // Connected[id] = false;
}

public IsClientConnected(id)
{
	if(get_bit(id, Connected)) //if(Connected[id])
	{
		//Player is connected
	}
	else
	{
		//Player is not connected
	}
}
zXCaptainXz is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 04-12-2022 , 22:29   Re: help with bit
Reply With Quote #3

Bugsy has some good explanation on that https://forums.alliedmods.net/showthread.php?t=139916

rem_bit is unseting whatever value is stored on the "loaded" variable out of the "i" variable, which doesn't make much sense, as far as I'm concerned it should be the inverse, ram_bit(loaded, i).
__________________









Last edited by CrazY.; 04-12-2022 at 22:30.
CrazY. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-12-2022 , 23:34   Re: help with bit
Reply With Quote #4

These macros are designed to store booleans/bit-flags for each player, allowing you to pass their player ID and the bit-field variable. It's pretty straightforward.

Suppose you wanted to record each player that was slayed, clearing the flag if they are revived.
PHP Code:
#define get_bit(%2,%1) (%1 & (1<<(%2&31)))
#define set_bit(%2,%1) (%1 |= (1<<(%2&31)))
#define rem_bit(%2,%1) (%1 &= ~(1 <<(%2&31)))

new WasPlayerSlayed;

SlayFuncid )
{
    
//slay( id );

    
set_bitid WasPlayerSlayed );
}

RevivePlayerid )
{
    
//revive( id );

    
rem_bitid WasPlayerSlayed );
}

bool:WasPlayerSlayedAndNotRevivedid )
{
    return !!
get_bitid WasPlayerSlayed );

If csgo_reset_data() should set all bits in loaded to 0, just do loaded = 0

Please ask a more specific question if you need help, but the above posts should help you understand what each macro does.
__________________

Last edited by Bugsy; 04-12-2022 at 23:38.
Bugsy is offline
PawnBegg
Junior Member
Join Date: Apr 2022
Old 04-13-2022 , 04:19   Re: help with bit
Reply With Quote #5

THANKS YOU GUYS, a little bit clear now ;)
PawnBegg is offline
Reply


Thread Tools
Display Modes

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 21:20.


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