How to check the #N bit of a s32/u32 number?
How to check the #N bit of a s32/u32 number?
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)
s32 Snum = 0xAA (1010 1010)
u32 Unum = 0x55 (0101 0101)
in general you could do it this way:
or in your case simply (as far as your bitNr is 4):
Code: Select all
bool isBitSet = Snum & (2 ^ bitNr);Code: Select all
bool is4thBitSet = Snum & 8;while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:

http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
bitplane got it...MasterGod wrote:By '^' you mean power or Xor?
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));while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:

http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
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
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++...
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
Code: Select all
2^0 = 2
2^1 = 3
2^2 = 0
2^3 = 1
Travis
right, that's why I also included this snippet:Halifax wrote:I wouldn't use powers. They are very costly. I would just use the & method. Why? Because it is the fastest.
Code: Select all
bool is4thBitSet = Snum & 8;just to satisfy youvitek 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++...
Code: Select all
bool isBitSet = Snum & pow(2, (bitNr - 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);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:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:

http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
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".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 !?!?!
If it was the other way around we'd just say "squared"
Thanks, I guess forgetting counting the first bit as 0 got me confused
So, between those answers the most efficient will be the first?
(Considering the first bit is counted as 1)
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:"..
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 ;
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:"..
-
hybrid
- Admin
- Posts: 14143
- Joined: Wed Apr 19, 2006 9:20 pm
- Location: Oldenburg(Oldb), Germany
- Contact:
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.
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.
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:
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...
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?
Code: Select all
bool is4thBitSet = Snum & 8; // test the 4th bit (index 3)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
}@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:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:

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:
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.
And if you don't know the number (i.e. it uses a variable) that one is still the fastest.
