Bit Manipulation
Operators, masks, tricks and subset enumeration.
The six primitive operations on the binary representation of integers: AND, OR, XOR, NOT, left shift and right shift.
Represent a set of up to 64 booleans as one integer so that set operations become single bitwise instructions.
The four single-bit primitives: set with OR, clear with AND-NOT, toggle with XOR, test with AND on a shifted 1.
Count the 1-bits of an integer with Kernighan's loop, a byte lookup table, a hardware popcount, or a DP over all numbers up to n.
Test for powers of two with n & (n-1), isolate the lowest set bit with n & -n, and round up to the next power of two with shift-or smearing.
Enumerate every subset of n items by counting masks from 0 to 2^n - 1, and every submask of a mask with s = (s - 1) & mask.
Exploit XOR's self-cancelling property to find unpaired elements, missing numbers, swap without a temporary, and answer range-XOR queries.