How to check the #N bit of a s32/u32 number?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

How to check the #N bit of a s32/u32 number?

Post by MasterGod »

How can I check the value of the N bit (4th for this example) of Snum and Unum.
s32 Snum = 0xAA (1010 1010)
u32 Unum = 0x55 (0101 0101)
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

in general you could do it this way:

Code: Select all

bool isBitSet = Snum & (2 ^ bitNr);
or in your case simply (as far as your bitNr is 4):

Code: Select all

bool is4thBitSet = Snum & 8;
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

By '^' you mean power or Xor?
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

#define isSet(a,b) bool(1 & (a >> b))

">>" shifts the bits over to the right, the & compares it

edit: yeah, acki meant power of.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Cool, but isSet(0xAA, 4) gets me 0 instead of 1.
Maybe it should be then:
#define isSet(a,b) bool(1 & (a >> b-1)) :?:
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No, bit places start counting at 0 :)
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

MasterGod wrote:By '^' you mean power or Xor?
bitplane got it... ;)

some binary basics:

1st bit = 2^0 = 1
2nd bit = 2^1 = 2
3rd bit = 2^2 = 4
4th bit = 2^3 = 8
and so on...

and right the bits begin at the right side and starting with index 0, so in my first post I made a little mistake,
of course it must be:

Code: Select all

bool isBitSet = Snum & (2 ^ (bitNr - 1));
but you also can see in the 2nd code snippet that I took the correct index for the 4th bit (2^3 = 8 )... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

I wouldn't use powers. They are very costly. I would just use the & method. Why? Because it is the fastest.

Code: Select all

if ((b & 0x00010000) > 0)
   cout << "Bit is set";
else
   cout << "Bit is not set";
TheQuestion = 2B || !2B
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Acki, you should probably stop writing ^ when you're talking about bitwise operations because it has a totally different meaning in that context. In C/C++...

Code: Select all

2^0 = 2 
2^1 = 3 
2^2 = 0 
2^3 = 1 
Remember that ^ is bitwise-xor. Now many of us know that you are talking about powers, but not everyone does. Also note that many people will write 2**3 to indicate 2 to the 3rd power to avoid any confusion.

Travis
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Halifax wrote:I wouldn't use powers. They are very costly. I would just use the & method. Why? Because it is the fastest.
right, that's why I also included this snippet:

Code: Select all

bool is4thBitSet = Snum & 8;
the other (1st) snippet was just to explain this... ;)

vitek wrote:Acki, you should probably stop writing ^ when you're talking about bitwise operations because it has a totally different meaning in that context. In C/C++...
just to satisfy you :lol: :

Code: Select all

bool isBitSet = Snum & pow(2, (bitNr - 1));
and just for clearness, this (bitNr - 1) is used when you name the 1st bit as 1 !!!
As mentioned earlier the 1st bit is 0 so in fact my first post wasn't an error... ;)
so this code must be used if you start counting the bits correctly at 0:

Code: Select all

bool isBitSet = Snum & pow(2, bitNr);
EDIT: oh and I mentioned another thing !!!
I always see in power of 2 for example for texture sizes !!!
But this isn't correct as I now see !!!
It must be 2 to the power of n, or am I wrong !?!?!

you know english is not my native language and so I'm always willing to learn more... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Acki wrote: I always see in power of 2 for example for texture sizes !!!
But this isn't correct as I now see !!!
It must be 2 to the power of n, or am I wrong !?!?!
by "power of two" we mean "a power with a base of 2", and by saying "must be a power of two" we mean "is a member of the set of all integer powers of base two".
If it was the other way around we'd just say "squared"
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Thanks, I guess forgetting counting the first bit as 0 got me confused :wink:

So, between those answers the most efficient will be the first?
(Considering the first bit is counted as 1)

Code: Select all

1. #define isSet(num,n) bool( 1 & (num >> n-1) )
2. bool isBitSet = ( Snum & pow(2, bitNr - 1) ) > 0 ;
And is it the same for unsigned numbers?

P.S:
I'm starting with 1 cause its more easy to think/say "the 7th bit is:" then "the bit in the 6th place is:"..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The second version is performance horror par excellence. Not only using pow (which might still be optimized by some compiler), but also the greater operator. Might take several hundred cycles in this version, while version 1 will take less than 10.
However, you could even make 1 better: Shift the mask instead of the number, then the shifting will happen at compile time (as long as n is known then, but that's more likely than num being known), which would reduce the costs to one or two cycles.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

As Halifax mentioned the fasted methode would be to use all numbers static, but you can do this only if you know what bit you have to check:

Code: Select all

bool is4thBitSet = Snum & 8; // test the 4th bit (index 3)
but if you need a more flexible methode for checking different bits you'll have to use one of the other methodes, or maybe create one fast methode for each possible bit to check... ;)

Code: Select all

switch(bitToCheck){
  case 0:
    isSet = Snum & 1;
    break;
  case 1:
    isSet = Snum & 2;
    break;
  case 2:
    isSet = Snum & 4;
    break;
  case 3:
    isSet = Snum & 8;
    break;
  case 4:
    isSet = Snum & 16;
    break;
// and so on
}
this should work much faster than shifting or powering... ;)


@bitplane: thx for the explanation !!! ;)
but as I now understand "2 to the power of n" is also correct, isn't it?
Last edited by Acki on Thu Nov 08, 2007 2:30 pm, edited 1 time in total.
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

If you know the bit number at compile time (which is the only way to use different methods) then you can still use my suggestion without further penalties. So it wouldn't make sense to create 32 defines or inline methods.
And if you don't know the number (i.e. it uses a variable) that one is still the fastest.
Post Reply