...

Bitwise Operations


Basic bit manipulation:

bit position
76543210

is n-th bit set:

if (x & (1<<n)) {
  //n-th bit is set
}

set n-th bit:

y = x | (1<<n)

clear n-th bit:

y = x & ~(1<<n)

toggle n-th bit:

y = x ^ (1<<n)

Resources: