Sign in to follow this  

Checking the BITS of an int(SOLVED)

This topic is 4376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there i have an int called step, how can i check the individual bits. I want to start at 16(or bit no5) and then check downwards with 8 next and then 4 and then 2. So basically if (step has bit 5 checked) else if (Step has bit 4 checked) else if (Step has bit 2 checked) else default; How is this done, assuming that step is an unsigned int [Edited by - dawidjoubert on December 19, 2005 5:37:36 AM]

Share this post


Link to post
Share on other sites
You can use a bitwise and with the appropriate bitmask. ex: (step & 0x01) checks for the first bit. (step & 0x02) checks for the second bit. (step & 0x04) checks for the fourth bit, and so on.

Share this post


Link to post
Share on other sites
I usually define those at the top of a header file and include them, so instead of doing 0x08 I can just put BIT3.

You're looking for something like:


void myfunction(unsigned int integer)
{
if(integer & 0x16)
{
//do something
}
else if(integer & 0x08)
{
//do something
}
else if(integer & 0x04)
{
//do something
}
else if(integer & 0x02)
{
//do something
}
else if(integer & 0x01)
{
//do something
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by dawidjoubert
Okay and is the bit order from left to right or right to left.

Oh and is this faster than a < or > check?

in the case of a byte is 1 the first bit or 128?


128 64 32 16 8 4 2 1


10000000 is 1
11000000 is 3
11100000 is 7

Left to right, right being the greatest, left being the least. What do you mean < or >?

Share this post


Link to post
Share on other sites
You can do like dbzprogrammer said, or you can just shift based on the index of the bit you are checking...


bool bitSet[32]; //or whatever

// Check each bit...
for(int index = 0; index < 32; index++)
bitSet[index] = myInt && (1 << index);



Share this post


Link to post
Share on other sites
Quote:
Original post by dbzprogrammer
Quote:
Original post by dawidjoubert
Okay and is the bit order from left to right or right to left.

Oh and is this faster than a < or > check?

in the case of a byte is 1 the first bit or 128?


128 64 32 16 8 4 2 1


10000000 is 1
11000000 is 3
11100000 is 7

Left to right, right being the greatest, left being the least. What do you mean < or >?


if (step > 8) // Do stuff

However im assuming a > 8 check is simple a check bit no 4

Share this post


Link to post
Share on other sites
Quote:

10000000 is 1
11000000 is 3
11100000 is 7

Left to right, right being the greatest, left being the least. What do you mean < or >?


That depends on whether you are using a little-endian or big-endian machine.

EDIT: Sorry, misunderstood... the order of the BYTES that compose the int depends on the endianness...

Each byte is the opposite of what's posted above...

00000001 is 1
00000010 is 2
00000100 is 4 ... etc...

In other words...
1 << 1 is 2,
1 << 2 is 4,
1 << 3 is 8, etc.

Share this post


Link to post
Share on other sites

bool bitSet[32]; //or whatever

// Check each bit...
for(int index = 0; index < 32; index++)
bitSet[index] = myInt & (1 << index); //& instead of &&



Would be more correct. Yes, that's how the logical order would go. Don't worry about other machienes right now.

Share this post


Link to post
Share on other sites
well it doesnt really matter which way it goes only importnat factor is which statement is true

if (step & 0x01) Print The number is Odd
or
if (step & 0x01) Print The number is > 128

Or in english does 0x01 check the bit under the ones column or under the largest column (128 in the case of a byte)

Share this post


Link to post
Share on other sites
Quote:
Original post by dawidjoubert
Quote:
Original post by dbzprogrammer
Quote:
Original post by dawidjoubert
Okay and is the bit order from left to right or right to left.

Oh and is this faster than a < or > check?

in the case of a byte is 1 the first bit or 128?


128 64 32 16 8 4 2 1


10000000 is 1
11000000 is 3
11100000 is 7

Left to right, right being the greatest, left being the least. What do you mean < or >?


if (step > 8) // Do stuff

However im assuming a > 8 check is simple a check bit no 4



I don't believe you can do that, and if you could, I think it would be if(step > 0x08). Perhaps if(step >> 0x08)?

Share this post


Link to post
Share on other sites
Quote:
Original post by smitty1276
Quote:
Would be more correct. Yes, that's how the logical order would go. Don't worry about other machienes right now.


Oops... yeah. Don't use the logical and.


Okay this whole thing is not working for me, i simple wanted to make a faster version of this

if (step > 8) step = 16;
else if (step > 4) step = 8;
else if (step > 2) step = 4;
else if (step > 1) step = 2;
else step = 1;

However replacing it with
if (step & 0x05) step = 16;
else if (step & 0x04) step = 8;
else if (step & 0x03) step = 4;
else if (step & 0x02) step = 2;
else step = 1;

Simple didnt work for me.

The thing is im doing that check about 16K times a frame and thought i could bring the FPS rate up by moving to a more effective method.

Perhaps at the end of the day ill end up doing it in ASM

Share this post


Link to post
Share on other sites
Quote:
Original post by dawidjoubert
Quote:
Original post by smitty1276
Quote:
Would be more correct. Yes, that's how the logical order would go. Don't worry about other machienes right now.


Oops... yeah. Don't use the logical and.


Okay this whole thing is not working for me, i simple wanted to make a faster version of this

if (step > 8) step = 16;
else if (step > 4) step = 8;
else if (step > 2) step = 4;
else if (step > 1) step = 2;
else step = 1;

However replacing it with
if (step & 0x05) step = 16;
else if (step & 0x04) step = 8;
else if (step & 0x03) step = 4;
else if (step & 0x02) step = 2;
else step = 1;

Simple didnt work for me.

The thing is im doing that check about 16K times a frame and thought i could bring the FPS rate up by moving to a more effective method.

Perhaps at the end of the day ill end up doing it in ASM


I don't know how the speed comparison would work out on that, sorry. and that wouldn't work, becaues 0x05 would be actually be 0x10.

Share this post


Link to post
Share on other sites
Quote:
Okay this whole thing is not working for me, i simple wanted to make a faster version of this

if (step > 8) step = 16;
else if (step > 4) step = 8;
else if (step > 2) step = 4;
else if (step > 1) step = 2;
else step = 1;


If your just wanting to check for a bit being set your doing it wrong with the "step & 0x03"... that is checking for 2 bits being checked. "step & (i < 2)" will check to see if the 3rd bit is checked.

This is the same thing... maybe this is what you meant. You need to make sure you are using powers of 2 (which is what the "1 << i" does).

if (step & 0x01) //first bit is set...
else if (step & 0x02) //2nd bit is set
else if (step & 0x04) //3rd bit is set...
...etc...




It looks like you are going the other way.... the 0x05 at the top should be, as dbz said, 0x10 (or "(1 << 5)").

Share this post


Link to post
Share on other sites
Furthermore, if you want an actual, functionally identical, alternative to this (with greater than sign):

if (step > 8) step = 16;
else if (step > 4) step = 8;
else if (step > 2) step = 4;
else if (step > 1) step = 2;
else step = 1;


You would need to make some small changes...


for (int i = 4; i >= 0; i++)
{
if (step > (1 << i))
{
step = (1 << (i+1));
break;
}
step = 1;
}



At this point though, what's the point. It's probably going to compile to the same thing regardless.

Share this post


Link to post
Share on other sites
I dunno whats the point really... Perhaps to test my own intelligence..which seem to have failed.

Anyways thanks guys i got it all sorted with the > checks.

So is there anyways to make a computer run infinitely fast without investing a cent? .... i didnt think so, bugger!

Share this post


Link to post
Share on other sites
Just thought I might mention this:
Quote:
Original post by dawidjoubertOh and is this faster than a < or > check?

Both of them would assemble down to one instruction, CMP or AND (IIRC). Generally, one instruction takes the same amount of time to execute (well, maybe a few hundred-billionths of a second difference) no matter which it is. (I think there are some which actually perform multiple instructions, but that's off topic.) For that matter your compiler or even CPU might optimize 'x > 8' into 'x & 8' or vice-versa.

Also, just to clarify what's already been said: Bits are numbered from right to left starting with 0. So 00000001 has bit 0 set, 00000010 has bit 1 set, and so on. The correct way to check a bit is to use the & (AND) operator to mask it out. For example (x & 4) checks bit 2, because the number 4 (00000100) only has bit 2 set. (x & 5) checks bits 2 and 0, since 5 (00000101) has bits 2 and 0 set.

Share this post


Link to post
Share on other sites

This topic is 4376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this