# Checking the BITS of an int(SOLVED)

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

## 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 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 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 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 on other sites
Quote:
 Original post by dawidjoubertOkay 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 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 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 on other sites
Quote:
Original post by dbzprogrammer
Quote:
 Original post by dawidjoubertOkay 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 on other sites
Quote:
 10000000 is 111000000 is 311100000 is 7Left 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 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.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013547
×