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

[SOLVED]How to compare bitstrings(like (1<<1))


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-25-2014 , 20:37   [SOLVED]How to compare bitstrings(like (1<<1))
Reply With Quote #1

How, I can't find any documentation on them.
Edit: Thx to people who help clarify to me on bitstrings and for showing me indirectly my mistake.

Last edited by WildCard65; 03-26-2014 at 15:33.
WildCard65 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-25-2014 , 22:11   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #2

Quick rundown of bitstrings:

The major operators for bitstrings are:
Code:
| // bitwise OR, returns a combination of bits that are set on both sides (used to combine bits into a single bitstring)
& // bitwise AND, returns all bits that exist on both sides (used to check if particular bits are set)
^ // bitwise XOR, reverses all bits that exist on both sides, returns all other bits unchanged
~ // bitwise NOT, unary operator that flips all the bits of its argument
<< // arithmetic left shift, makes all bits X bits larger, lower bits are set to zero
>> // arithmetic right shift, makes all bits X bits smaller, higher bits are set to the sign bit (this is usually what you want)
>>> // logical right shift, makes all bits X bits smaller, higher bits are set to zero
You can add bits to a bitstring using | or |=, such as this:
PHP Code:
new bits 0;
bits |= (<< 0); 
You can check if a bit is set using &.
PHP Code:
if (bits & (<< 0))
{
    
// Do something if bit 1 is set

Note that checking multiple bits means you have to use the long form with ==
PHP Code:
if (bits & (<< 0<< 1) == (<< 0|<< 1))
{
    
// Do something if bits 1 and 2 are set

You can remove a bit using &= and ~
PHP Code:
bits &= ~(<< 0); // removes bit 1
// This works because ~(1 << 0) means "every bit except 1" 
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 03-26-2014 at 15:42. Reason: Adjusted all examples to use bitshifts
Powerlord is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-26-2014 , 06:43   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #3

K thx
WildCard65 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 03-26-2014 , 09:46   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #4

Quote:
Originally Posted by Powerlord View Post
Note that checking multiple bits means you have to use the long form with ==
PHP Code:
if (bits & (1|2) == (1|2))
{
    
// Do something if bits 1 and 2 are set

How This work exactly ? :/
*
(1|2) == (1|2) ??

Last edited by Bacardi; 03-26-2014 at 09:47.
Bacardi is offline
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 03-26-2014 , 09:54   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #5

& (And operator) has highest precedence

This is interpreted as:
Code:
(bits & (1|2)) == (1|2))

Last edited by h3bus; 03-26-2014 at 09:55.
h3bus is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 03-26-2014 , 09:56   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #6

Quote:
Originally Posted by Bacardi View Post
How This work exactly ? :/
*
(1|2) == (1|2) ??
(1|2|4) == (1|2) would return false, it first checks to see if both of those are set in the bits, then checks to see if that's the only bits in the bitstring. That's my guess, could be wrong.
as it would be translated to:
7 == 3

Last edited by Mitchell; 03-26-2014 at 11:44.
Mitchell is offline
Doodil
Senior Member
Join Date: Mar 2012
Old 03-26-2014 , 11:15   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #7

correct me if I'm wrong, but in other programing languages (1|2|3) does not mean "set bit 1, 2 and 3" but "set the bits that are used to represent the numbers 1, 2 and 3". And that means:

1= 001
2= 010
3= 011

and going with this logic (1|2|3) = 011 and (1|2) = 011, so (1|2|3) == (1|2) equals true.


If you want to set bit number X (starting at 0) then you have to left-shift the first bit by X:

(1<<0) = 001 //equals 1
(1<<1) = 010 //equals 2
(1<<2) = 100 //equals 4, not 3



Please correct me if this is any different in sourcemod.
Doodil is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 03-26-2014 , 11:43   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #8

Quote:
Originally Posted by Doodil View Post
correct me if I'm wrong, but in other programing languages (1|2|3)
you caught me: i meant "(1|2|4)"
Mitchell is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 03-26-2014 , 11:45   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #9

oh darn, now I get it, some how... thanks for explaining
that if statement is like
PHP Code:
if( client <= MaxClients 


*edit wait a second... need think this later

Last edited by Bacardi; 03-26-2014 at 11:46.
Bacardi is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 03-26-2014 , 12:00   Re: How to compare bitstrings(like (1<<1))
Reply With Quote #10

Quote:
Originally Posted by Bacardi View Post
oh darn, now I get it, some how... thanks for explaining
that if statement is like
PHP Code:
if( client <= MaxClients 


*edit wait a second... need think this later
It's helpful, you also learn this in subnet masking, i believe, i cant remember if that is what it's called.
http://en.wikipedia.org/wiki/Logical_bitwise_operator
Mitchell 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 23:27.


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