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

Bit - include file (formally Boolean)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Lulu the hero
Senior Member
Join Date: Oct 2009
Location: Budapest, Hungary
Old 12-06-2010 , 07:40   Bit - include file (formally Boolean)
Reply With Quote #1

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.
Attached Files
File Type: inc bit.inc (3.1 KB, 364 views)

Last edited by Lulu the hero; 03-04-2011 at 11:48.
Lulu the hero is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-06-2010 , 17:20   Re: Boolean - include file
Reply With Quote #2

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';
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Lulu the hero
Senior Member
Join Date: Oct 2009
Location: Budapest, Hungary
Old 12-10-2010 , 01:33   Re: Boolean - include file
Reply With Quote #3

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.
Lulu the hero is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-10-2010 , 01:37   Re: Boolean - include file
Reply With Quote #4

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.
__________________
fysiks is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 12-10-2010 , 02:01   Re: Boolean - include file
Reply With Quote #5

I think those stocks names are quite confusing.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-10-2010 , 03:52   Re: Boolean - include file
Reply With Quote #6

Quote:
Originally Posted by Lulu the hero View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 12-10-2010 at 14:18.
Exolent[jNr] is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 12-10-2010 , 11:47   Re: Boolean - include file
Reply With Quote #7

Quote:
Originally Posted by Exolent[jNr] View Post
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.
Seta00 is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 12-10-2010 , 12:21   Re: Boolean - include file
Reply With Quote #8

Quote:
Originally Posted by Seta00 View Post
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 ?
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-10-2010 , 14:17   Re: Boolean - include file
Reply With Quote #9

Quote:
Originally Posted by Seta00 View Post
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"

Quote:
Originally Posted by abdul-rehman View Post
How can you say that AND is faster then SUBTRACTION ?
Because it actually is faster. He isn't just stating an opinion.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 12-10-2010 at 14:28.
Exolent[jNr] is offline
Lulu the hero
Senior Member
Join Date: Oct 2009
Location: Budapest, Hungary
Old 12-10-2010 , 23:31   Re: Boolean - include file
Reply With Quote #10

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.
Lulu the hero 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 10:15.


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