
Bit Manipulation Pattern — the interview skill nobody practices
Demystify bitwise operations -- XOR tricks, bit counting, and the handful of patterns that cover 90% of interview bit problems.
There are exactly 5 bit tricks that cover 90% of interview bit problems. Memorize these, and bit manipulation goes from "I'll skip this" to "free points."
Bit manipulation is writing code that operates on individual binary digits instead of whole numbers. Interview rounds test whether you recognize when a problem collapses to XOR, a mask, or a single bit check. The tricks are small; the recognition is the rep.
Bit manipulation is the pattern everyone skips — until it shows up in an interview and they freeze. Trace enough candidates through bit problems and the split is always the same: people who know the five tricks solve them in three minutes, and people who do not know them burn fifteen minutes reinventing XOR from scratch. CP-Algorithms' bit manipulation reference covers the formal derivations if you want the math, but for interviews you just need the five patterns below.
The 5 Tricks
1. to Find the Unique Element
a ^ a = 0, a ^ 0 = a, and XOR is commutative/associative. XOR-ing all elements cancels duplicates. Florian's XOR Trick writeup derives why this works from first principles — the key insight is that you can remove all pairs of duplicated values from a sequence of XOR operations without affecting the result.
def singleNumber(nums):
result = 0
for n in nums:
result ^= n
return resultSolves Single Number (LC 136) in O(n) time, O(1) space. No hash map needed.
2. Bit Check / Set / Clear / Toggle
(n >> i) & 1 # is bit i set?
n | (1 << i) # set bit i
n & ~(1 << i) # clear bit i
n ^ (1 << i) # toggle bit iThese four operations are the building blocks of everything else.
3. n & (n - 1) -- Clear the Lowest Set Bit
Removes the rightmost 1-bit. This is Brian Kernighan's algorithm — GeeksforGeeks explains it well: "subtracting 1 from a number flips all the bits after the rightmost set bit including the rightmost set bit itself." The beauty is that the loop runs only as many times as there are set bits:
def hammingWeight(n):
count = 0
while n:
n &= (n - 1)
count += 1
return countOnly loops as many times as there are 1-bits.
4. n & (-n) -- Isolate the Lowest Set Bit
Returns a number with only the lowest set bit of n. This works because of two's complement representation — -n flips all bits and adds one, so the AND isolates exactly the bit where n and -n agree. Key to Single Number III (LC 260) where you partition elements by that bit to separate two unique numbers.
5. Power of Two Check
A power of two has exactly one bit set: n > 0 and (n & (n - 1)) == 0. Done.
When Interviewers Ask This
Bit problems test whether you know the tricks. If you do, they're fast. If you don't, you're stuck.
| Interview Question | LC # | Trick Used | What They're Testing |
|---|---|---|---|
| "Find the unique element" | 136 | XOR all elements | Do you know XOR cancels pairs? |
| "Count set bits" | 191 | n & (n-1) | Do you know this trick vs checking all 32 bits? |
| "Reverse the bits of a 32-bit integer" | 190 | Bit-by-bit shift | Can you build a result one bit at a time? |
| "Find the missing number in [0, n]" | 268 | XOR indices with values | Can you apply XOR beyond the basic case? |
| "Two unique numbers in an array of pairs" | 260 | XOR + n & (-n) to partition | Can you chain two tricks together? |
| "Is n a power of two?" | 231 | n & (n-1) | One-liner awareness |
Most bit problems are easy or medium. They rarely appear as the hard problem in a loop. But I have watched candidates lose fifteen minutes on Single Number because they reached for a hash map instead of XOR — and then run out of time on the harder problem that actually mattered. As Educative's bit manipulation guide puts it, these techniques come up often enough in interviews that not knowing them is a real liability.
Recognition Signals
- The problem asks to find something "without extra space" -- bit tricks give O(1) space
- The problem involves powers of two, unique elements, or toggling states
- The interviewer says "can you do this in O(1) space?"
- Constraints mention 32-bit integers or operations explicitly
Common Mistakes
- Using a hash map when XOR works. If duplicates appear in pairs and you need the unique element, XOR is the move. A hash map works but misses the point — and the interviewer knows you missed it.
- Not tracing through a small example. If you are unsure about a bit operation, trace
n = 6(110in binary) through the expression on paper. Takes ten seconds and saves you from the silent stare when the interviewer asks "are you sure about that?" I tell every candidate: trace before you commit. - Overthinking. Bit problems reward pattern recognition, not creativity. Know the 5 tricks, apply the right one.
Practice Problems
- Single Number (LC 136) -- XOR basics
- Number of 1 Bits (LC 191) --
n & (n-1)trick - Reverse Bits (LC 190) -- bit-by-bit construction
- Missing Number (LC 268) -- XOR with indices
- Power of Two (LC 231) -- single-bit check
- Counting Bits (LC 338) -- DP + lowest set bit
Practice Bit Manipulation.
Explain your thinking like you're in the interview.
Sources
- NeetCode Bit Manipulation Roadmap — curated problem order
- LeetCode Bit Manipulation Tag — 180+ problems sorted by frequency
- Hacker's Delight (Henry S. Warren) — the definitive reference on bit tricks, if you want to go deeper
- Stanford Bit Twiddling Hacks — the canonical compendium of bit manipulation recipes
Fin and Coco are StrongYes editorial personas from the Council of Ternary Vertices — a trinary-star animal civilization that studies Earth's coding-interview process. Anecdotes map animal-universe experience to human interview mechanics; they are NEVER human-career claims. External citations link to public primary sources.
StrongYes editorial guide distilled from interview coaching notes, pattern walkthroughs, and curated prep plans used across the StrongYes library.
Reviewed by Leo Kwan on Apr 12, 2026.
Practice Bit manipulation.
Reading builds recognition. Explaining builds recall. Run these problems with Fin or Coco.