AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Bit - include file (formally Boolean) (https://forums.alliedmods.net/showthread.php?t=144645)

Lulu the hero 12-06-2010 07:40

Bit - include file (formally Boolean)
 
5 Attachment(s)
Hi, everyone!

I was using some bit manipulating stocks far more often, then to copy paste them everywhere all the time. I got a handful, and put it into an include file. Hope this set of stocks/functions - gonna add more if get new ones - will help everyone, who needs bit manipulation.

The macros/stocks:
PHP Code:

#define toggle_player_flag(%1,%2)
#define toggle_flag(%1,%2)

#define clear_player_flag(%1,%2)
#define clear_flag(%1,%2)

#define set_player_flag(%1,%2)
#define set_flag(%1,%2)

#define is_player_flag_set(%1,%2)
#define is_flag_set(%1,%2)

stock num_to_binstr(numdest[], zero_fill 0)
stock num_to_hexstr(numdest[], zero_fill 0)

stock rol(integerstep 1)
stock ror(integerstep 1)

stock get_highest(integercount_type// COUNT_BIT|COUNT_NIBBLE
stock count_bits(integer) 

Last update(dd.mm.yyyy): 03.03.2011.

Exolent[jNr] 12-06-2010 17:20

Re: Boolean - include file
 
There is already an xor operator.

Code:
new bits = 1 | 2 bits = bits ^ 1 // bits = 2 bits = bits ^ 3 // bits = 2 | 3

I suggest id_to_flag() to limit to 31 instead of just subtracting 1.

Code:
#define id_to_flag(%1) (1<<(%1&31))

You could also do the same with your num_to_binstr() and also get rid of the conditional.

Code:
dest[len-i] = ((num>>i) & 1) + '0';

Lulu the hero 12-10-2010 01:33

Re: Boolean - include file
 
Updated it with your ideas, thank you very much. I left the limit to 32. A 31 sized bit array would be useless for 32 players on a server - I use these functions for player bits to avoid the following code types:

PHP Code:

new is_alive[33];

// something happened, toggle is_alive off:
is_alive[id] = 0;

// or turn it on:
is_alive[id] = 1

With this, 32 bits stored on 33*cellbits storage. What a waste. 1 of the 33 integers are used up only - or 0.5 of 33 if on a 64 bit processor.

fysiks 12-10-2010 01:37

Re: Boolean - include file
 
I don't see how your include has anything to do with booleans. Also, Bugsy has several common bit operations listed in his bitwise operations thread.

ConnorMcLeod 12-10-2010 02:01

Re: Boolean - include file
 
I think those stocks names are quite confusing.

Exolent[jNr] 12-10-2010 03:52

Re: Boolean - include file
 
Quote:

Originally Posted by Lulu the hero (Post 1367497)
Updated it with your ideas, thank you very much. I left the limit to 32. A 31 sized bit array would be useless for 32 players on a server - I use these functions for player bits to avoid the following code types:

PHP Code:

new is_alive[33];

// something happened, toggle is_alive off:
is_alive[id] = 0;

// or turn it on:
is_alive[id] = 1

With this, 32 bits stored on 33*cellbits storage. What a waste. 1 of the 33 integers are used up only - or 0.5 of 33 if on a 64 bit processor.

I don't know where you thought I said anything about an array, but I didn't.
1<<(32-1) would give the same results as 1<<(32&1) but IIRC bit operations are quicker.

Seta00 12-10-2010 11:47

Re: Boolean - include file
 
Quote:

Originally Posted by Exolent[jNr] (Post 1367548)
1<<(32-1) would give the same results as 1<<(32&1) but IIRC but operations are quicker.

I'm not quite sure about what you meant with that phrase, but AND is faster than subtraction.

abdul-rehman 12-10-2010 12:21

Re: Boolean - include file
 
Quote:

Originally Posted by Seta00 (Post 1367787)
I'm not quite sure about what you meant with that phrase, but AND is faster than subtraction.

How can you say that AND is faster then SUBTRACTION ?

Exolent[jNr] 12-10-2010 14:17

Re: Boolean - include file
 
Quote:

Originally Posted by Seta00 (Post 1367787)
I'm not quite sure about what you meant with that phrase, but AND is faster than subtraction.

That's what I meant, except I said "but operations" instead of "bit" :P

Quote:

Originally Posted by abdul-rehman (Post 1367806)
How can you say that AND is faster then SUBTRACTION ?

Because it actually is faster. He isn't just stating an opinion.

Lulu the hero 12-10-2010 23:31

Re: Boolean - include file
 
Uploaded newer version, edited the stock names, added a new stock.

What you are arguing about are CPU cycles. But as I said, I only want to stop people using 32 bit bool-s to store 1 bit. Bool-s are usually used for switches, mostly for every player. With these type of bit manipulations, this problem is solved. Every player's switch can be stored in 1 integer.


All times are GMT -4. The time now is 19:54.

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