Bit manipulation
Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.
What you will be able to do
#- Read and write individual bits with AND, OR, XOR and shifts.
- Use XOR to find a value that appears an odd number of times.
- Count set bits with a loop and with a faster trick.
Overview
#Numbers as bits
An integer is a sequence of bits. Bitwise operators act on each bit position independently:
- AND
a & b: 1 only where both are 1. - OR
a | b: 1 where either is 1. - XOR
a ^ b: 1 where the bits differ. - NOT
~a: flips every bit. - Shift
a << kanda >> k: move bits left or right.
Reading and writing a bit
Test bit i: (a >> i) & 1. Set bit i: a | (1 << i). Clear bit i: a & ~(1 << i). Toggle bit i: a ^ (1 << i). These are the building blocks of bitmask techniques.
XOR tricks
XOR has two useful properties: x ^ x = 0 and x ^ 0 = x. So XORing every element cancels pairs and leaves the unpaired value:
function singleNumber(values) {
let result = 0;
for (const v of values) result ^= v;
return result;
}
This finds the value that appears once when every other value appears twice, in O(n) time and O(1) space.
Counting set bits
A simple loop tests each bit:
function popcount(n) {
let count = 0;
while (n) { count += n & 1; n >>= 1; }
return count;
}
A faster trick clears the lowest set bit each step with n &= n - 1, so the loop runs once per set bit rather than once per bit position.
Why it matters
Bit manipulation gives O(1) set operations and compact state. It appears in subset enumeration (a mask from 0 to 2^n - 1), permission flags and low-level optimisation. Use it when the problem is naturally about bits, and keep the code readable with named masks.
Practice next
- Find the single element that appears once when others appear twice.
- Count the set bits of every number from 0 to n.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What is 6 & 3 in binary terms?
starter.javascript// 6 is 110, 3 is 011Reveal the reference answer
reference.javascript110 AND 011 is 010, which is 2.Authored expected output: 2
AND keeps only the positions where both numbers have a 1.
Exercise 2Predict
What does singleNumber return for [4, 1, 2, 1, 2]?
starter.javascriptlet result = 0; for (const v of values) result ^= v;Reveal the reference answer
reference.javascriptThe pairs 1 ^ 1 and 2 ^ 2 cancel to 0, leaving 4 ^ 0 = 4.Authored expected output: 4
XOR cancels equal values regardless of order.
Exercise 3Implement
Implement popcount(n) using n &= n - 1 to clear the lowest set bit.
starter.javascriptfunction popcount(n) { // your code here }Reveal the reference answer
reference.javascriptfunction popcount(n) { let count = 0; while (n) { n &= n - 1; count++; } return count; }Authored expected output: 3 for popcount(13)
13 is 1101 with three set bits, and each iteration removes exactly one.
Common misconceptions
#- XOR is the same as OR. XOR is 1 only when the bits differ; OR is 1 when either is 1.
- Shifting left by k multiplies by 2^k with no limits. Fixed-width integers overflow, so high bits are lost.
- Bit tricks are always faster. They are O(1) per operation, but unreadable bit code can hide bugs.
Follow-up practice
#- dsa-from-zero-interview-reasoning-capstone
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.