Jump to content
  • Advertisement
Sign in to follow this  
dawidjoubert

Checking the BITS of an int(SOLVED)

This topic is 4569 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
Advertisement
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
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

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
yes the question is simple which way is the byte orderd, im assuming the logical way with the first bit representing 1 and the second bit representing 2

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!