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

[INC] bits.inc


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 05-01-2010 , 16:34   [INC] bits.inc
Reply With Quote #1

Macro substitutions for bit manipulation
My simple library for manipulate with bits:
Big thanks Void.Less for translate

Single bits
Bit flag operation is done everywhere with such macros:

PHP Code:
#define set_bit(%1,%2)        (%1 |= (1<<%2))
#define get_bit(%1,%2)        (%1 & (1<<%2))
#define clr_bit(%1,%2)        (%1 &= ~(1<<%2)) 
Example usage with enumerated flags:
PHP Code:
enum e_flags {
  
FLAG_1,
  
FLAG_2,
  
FLAG_3,
  
FLAG_4
}

new 
flag
set_bit
(flagFLAG_2)
get_bit(flagFLAG_3
Multi-flag macros
For checking multiple bits at once:

PHP Code:
#define set_bits(%1,%2)       (%1 |= %2)
#define get_bits(%1,%2)       (%1 & %2)
#define clr_bits(%1,%2)       (%1 &= ~%2) 
Difference is that instead of bit position we use variable or constant with set of bit.

Example:

PHP Code:
// Define constants with necessary bits
#define CLIENT_FLAG_1   (1<<0)
#define CLIENT_FLAG_2   (1<<1)
#define CLIENT_FLAG_3   (1<<2)
#define CLIENT_FLAG_4   (1<<3)

// Array of flags for every player
new flag[32]

// Set two flags for player 1
set_bits(flag[1], CLIENT_FLAG_1 CLIENT_FLAG_3)

// Clear 3 flags for player 23
clr_bits(flag[23], CLIENT_FLAG_1 CLIENT_FLAG_4 CLIENT_FLAG_2
Big bit amount
These macros are designed for setting single bits in big flag arrays
PHP Code:
#define set_big_bit(%1,%2)    (%1[%2>>5] |= 1<<(%2 & 31))
#define get_big_bit(%1,%2)    (%1[%2>>5] & 1<<(%2 & 31))
#define clr_big_bit(%1,%2)    (%1[%2>>5] &= ~(1 << (%2 & 31))) 
If you need more than 32 flags in one variable, you can use string of any length as a flag container.

PHP Code:
new flags[4]
//possible 4 * 32 = 128 bits stored

set_big_bit(flags34)
get_big_bit(flags56)
clr_big_bit(flags101
Recommendations

Cerberus mainly use second type flags, and only 2 functions are used:


PHP Code:
#define CF_IS_BOT (1<<0)
#define CF_IS_HLTV (1<<1)
#define CF_IS_CONNECTED (1<<2)

new gUserFlag[33]

public 
bool:cerb_flag(idflag
    return 
bool:(get_bits(gUserFlag[id], flag))
public 
set_cerb_flag(idflagvalue)
    
value set_bits(gUserFlag[id], flag) : clr_bits(gUserFlag[id], flag)

// This simplifies setting and clearing of multiple flags
public client_connect(id) {
    
set_cerb_flag(idCF_IS_BOT CF_IS_HLTV CF_IS_CONNECTED0)
}

// You can use logical functions right away
public client_putinserver(id) {
    
set_cerb_flag(idCF_IS_BOTis_user_bot(id))
    
set_cerb_flag(idCF_IS_HLTVis_user_hltv(id))
    
set_cerb_flag(idCF_IS_CONNECTED1)

Attached Files
File Type: inc bits.inc (1.3 KB, 414 views)
__________________

Last edited by Zefir; 05-24-2010 at 09:59.
Zefir is offline
Send a message via ICQ to Zefir
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 05-01-2010 , 16:43   Re: [INC] bits.inc
Reply With Quote #2

Why is this in "Module Coding"?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 05-01-2010 , 16:56   Re: [INC] bits.inc
Reply With Quote #3

Hm..., and where?

Please, you prposal
__________________
Zefir is offline
Send a message via ICQ to Zefir
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-01-2010 , 16:58   Re: [INC] bits.inc
Reply With Quote #4

It's obviously not a module.

Code Snippets/Tutorials
__________________
fysiks is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 05-01-2010 , 17:00   Re: [INC] bits.inc
Reply With Quote #5

Moved to its proper place.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 05-01-2010 , 17:02   Re: [INC] bits.inc
Reply With Quote #6

Thanks
__________________
Zefir is offline
Send a message via ICQ to Zefir
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 05-01-2010 , 17:15   Re: [INC] bits.inc
Reply With Quote #7

From the snippet on bits and operators, a guy posted this for ***_big_bit :

#define SetEntBits(%1,%2) %1[%2>>5] |= 1<<(%2 & 31)
#define ClearEntBits(%1,%2) %1[%2>>5] &= ~( 1 << (%2 & 31) )
#define GetEntBits(%1,%2) %1[%2>>5] & 1<<(%2 & 31)

Maybe >>5 is faster than /32 i don't know.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 05-01-2010 , 17:29   Re: [INC] bits.inc
Reply With Quote #8

ConnorMcLeod, big thanks.
fixed...
__________________
Zefir is offline
Send a message via ICQ to Zefir
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 10:17.


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