Access individual bits in a char c++
You would use the binary operators |
(or), &
(and) and ^
(xor) to set them. To set the third bit of variable a
, you would type, for instance:
a = a | 0x4
// c++ 14
a = a | 0b0100
Note that 4’s binary representation is 0100
If you want access bit N
:
Get: (INPUT >> N) & 1;
Set: INPUT |= 1 << N;
Unset: INPUT &= ~(1 << N);
Toggle: INPUT ^= 1 << N;