I'm trying to know the states of different bits over a char
Let's say I've:
char a = 11 //00001011 in binary
How can I retrieve bit number 5 which is currently 0 and cast it to a bool variable? And how can I set it?
I'm trying to know the states of different bits over a char
Let's say I've:
char a = 11 //00001011 in binary
How can I retrieve bit number 5 which is currently 0 and cast it to a bool variable? And how can I set it?
You can use some bitwise operators:
bool fifthBitSet = a & 0x10;
Or, more generally:
bool nThBitSet = a & ( 0x1 << n );
Missed the setting part:
a |= 0x1 << n; // set n'th bit
0x8 however you count it.As you've noted:
char a = 11 // == 00001011 in binary
Since you said the bit number 5 is clear right now, let's *define the position of the bits as you're talking about them:
pos 8 7 6 5 4 3 2 1
value [0][0][0][0][1][0][1][1]
You want to test the 5th bit, that means you need a mask that accesses the fifth bit, here's some masks:
your number: 00001011
0x1 mask: 00000001
0x2 mask: 00000010
0x4 mask: 00000100
0x8 mask: 00001000
0x10 mask: 00010000 <-- So that's the one we want, the 5th bit
To set your bit you need a bit-wise OR (|)
a |= 0x10; // 00001011
// | 00010000 because 1 | anything = 1
// ----------
// 00011011 The value is set!
To test the bit we can use the bit-wise AND (&)
bool something = a & 0x10; // 00001011
// & 00010000 because only 1 & 1 = 1
// ----------
// 00000000 something will be 0 (false)
*it was pointed out that this is a 1-based indexing as opposed to the more typical 0-based indexing. Which is true. Based on the description I chose to read the question as "bit number 5" meaning typical (human) based counting of 1, 2, 3, 4, etc out of 8-bits.
If you'd rather use a 0-based indexing, this is no problem, just "shift" the same logic by one, the mask for the "5th" bit in 0-based indexing is 0x20
When talking about bits it's good to note which is the least significant bit, and 0 or 1 based to be totally clear.
You mention bit number 5, I believe you're looking at the 5th least-significant bit.
You've also asked about how to set a bit at a specific position. For that, use bitwise OR - |:
a = a | 0x10;
As has already been answered, test the bit using bitwise AND - &:
bool isSet = a & 0x10;
Often you'll use a positional flag like 0x10, but the generalizations given using bit-shifts are very useful, especially if you wrap these into a function:
int pos = 5;
// set bit 5:
a = a | (0x1 << pos);
// test bit 5:
bool isSet = a & (0x1 << pos);
For more information, the wikipedia article on bitwise operation is pretty good.
Well, you can use this set of functions to get and set the bit states, I suppose:
bool getBit(int data, int bitNumber) {
int flag = 1 << bitNumber - 1;
return (bool)(data & flag);
}
void setBit(int& data, int bitNumber) {
int flag = 1 << bitNumber - 1;
data |= flag;
}
...
int main() {
int a = 11;
cout << getBit(a, 5); // false
setBit(a, 5);
cout << getBit(a, 5); // true
}
Still I wonder why do you need to use this, and not bitsets. )
You can use bitwise AND to know the states.
unsigned int number = 11;
AND the number with the value in which the bit position you want to check alone is set and the remaining bits to 0.
Suppose I want to check bit 0, then
if(a&0x00000001)
puts(" 01h bit position is 1")
else
puts(" 01h bit position is 0")