A Primer On Bit Manipulation
The Basics
Suppose we have a single bit x.
- To set it, let
x = x | 1. - To clear it, let
x = x & 0 - To read it (i.e. not modifying any bit), read the result of
x & 1 - To toggle it, let
x = x ^ 1
Create the Mask
Suppose we have an unsigned int number of 10111011.
Suppose index start at 0.
Suppose n = 3, m = 5
To set bits, we want our interest interval to be all 1’s.
- To set the nth bit:
mask = (1 << n) # mask = 1000 x = x | mask - To set the last n bits:
mask = (1 << n) - 1 # mask = 1000 - 1 = 111 x = x | mask - To set from
mth bits tonth bits, inclusivemask = ((1 << m) - 1) - ((1 << n-1) - 1) x = x | mask - To clear the nth bit:
mask = (1 << n) - 1 x = x & mask