Access individual bits in a char c++

AnotherProgrammer picture AnotherProgrammer · Mar 2, 2012 · Viewed 32.4k times · Source

How would i go about accessing the individual bits inside a c++ type, char or any c++ other type for example.

Answer

Matt picture Matt · Mar 2, 2012

If you want access bit N:

Get: (INPUT >> N) & 1;

Set: INPUT |= 1 << N;

Unset: INPUT &= ~(1 << N);

Toggle: INPUT ^= 1 << N;