Skip to main content
PatternBit manipulationPatternsAlgorithms

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.

Fin·Reviewed by Leo Kwan·Apr 3, 2026·6 min read
StrongYes tip

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.

Diagram
Rendering diagram...

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.

PYTHON
def singleNumber(nums): result = 0 for n in nums: result ^= n return result

Solves Single Number (LC 136) in O(n) time, O(1) space. No hash map needed.

2. Bit Check / Set / Clear / Toggle

PYTHON
(n >> i) & 1 # is bit i set? n | (1 << i) # set bit i n & ~(1 << i) # clear bit i n ^ (1 << i) # toggle bit i

These 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:

PYTHON
def hammingWeight(n): count = 0 while n: n &= (n - 1) count += 1 return count

Only 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 QuestionLC #Trick UsedWhat They're Testing
"Find the unique element"136XOR all elementsDo you know XOR cancels pairs?
"Count set bits"191n & (n-1)Do you know this trick vs checking all 32 bits?
"Reverse the bits of a 32-bit integer"190Bit-by-bit shiftCan you build a result one bit at a time?
"Find the missing number in [0, n]"268XOR indices with valuesCan you apply XOR beyond the basic case?
"Two unique numbers in an array of pairs"260XOR + n & (-n) to partitionCan you chain two tricks together?
"Is n a power of two?"231n & (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 (110 in 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

  1. Single Number (LC 136) -- XOR basics
  2. Number of 1 Bits (LC 191) -- n & (n-1) trick
  3. Reverse Bits (LC 190) -- bit-by-bit construction
  4. Missing Number (LC 268) -- XOR with indices
  5. Power of Two (LC 231) -- single-bit check
  6. Counting Bits (LC 338) -- DP + lowest set bit

Practice Bit Manipulation.

Explain your thinking like you're in the interview.

Practice with Fin or Coco

Sources

Source note

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.