Checking the BITS of an int(SOLVED)
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]
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.
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
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
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:
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 }}
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 >?
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
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);
Quote:Original post by dbzprogrammerQuote: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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement