Bit manipulation questions become easier when you know the identity behind the trick. Do not memorize the symbol only; memorize the reason it works.
Core Bit Identities
| Identity | Meaning | Practice |
|---|---|---|
| x ^ x = 0 | Pairs cancel | Single Number |
n & (n | Removes lowest set bit | Number of 1 Bits |
| n & -n | Isolates rightmost set bit | Single Number III |
| xor 1..n repeats by n % 4 | Constant-time prefix XOR | XOR From 1 to N |
FAQs
Are bit tricks language-specific?
The identity is language-independent, but signed integer behavior can differ. Be careful with negative values.
What is the best first bit problem?
Start with Single Number because XOR cancellation is the most reusable idea.