Bit Manipulation
Use bitwise operators (AND, OR, XOR, shifts, masks) to compute results in constant space and near-constant time.
- • XOR to cancel pairs or find a unique element
- • Bit masks to represent subsets or state
- • Counting or toggling individual bits
- • Power-of-two and parity checks
- • Logic is clearer with normal arithmetic
- • Values exceed the integer width you control
- • Signed shifts and the sign bit
- • Operator precedence of & | ^ versus comparisons
- • Assuming 32 versus 64-bit width
Key Invariant
XOR cancels equal bits, AND masks them, OR sets them — pick the operator that isolates the bit you care about