25. Number of 1 Bits
easyAsked at VercelCount the number of set bits in a 32-bit integer (Hamming weight). Vercel uses this as the gateway to bit tricks they reach for in compact feature-flag masks and quick set-difference operations.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-Q4)— Vercel runtime screen warm-up.
- LeetCode Discuss (2026-Q1)— Mentioned in Vercel prep notes.
Problem
Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
Constraints
The input must be a binary string of length 32.
Examples
Example 1
n = 000000000000000000000000000010113Example 2
n = 000000000000000000000000100000001Approaches
1. Loop and mask each bit
Iterate 32 times, mask the low bit, accumulate, shift right.
- Time
- O(32)
- Space
- O(1)
function hammingWeight(n) {
let count = 0;
for (let i = 0; i < 32; i++) {
count += n & 1;
n >>>= 1;
}
return count;
}Tradeoff: Always 32 iterations regardless of how many bits are set. The Brian Kernighan trick is faster when bits are sparse.
2. Brian Kernighan trick (optimal)
n & (n - 1) clears the lowest set bit. Repeat until n is 0; the iteration count is the bit count.
- Time
- O(popcount)
- Space
- O(1)
function hammingWeight(n) {
let count = 0;
while (n !== 0) {
n &= n - 1;
count++;
}
return count;
}Tradeoff: Only iterates once per set bit. For sparse inputs (typical in flag masks) this is significantly faster than the 32-iteration version.
Vercel-specific tips
Vercel grades for the Kernighan trick. Bonus signal: explaining WHY `n & (n - 1)` clears the lowest set bit (subtracting 1 flips all trailing zeros and the lowest 1; the AND zeros them all out). Connect it to popcount instructions in modern CPUs.
Common mistakes
- Using `>>` instead of `>>>` — sign-extends, infinite loop for high-bit-set inputs.
- Storing the count in a bit field and confusing yourself.
- Looping 31 times — off-by-one.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Hamming distance between two numbers (LC 461) — XOR then popcount.
- Counting bits for [0..n] in O(n) (LC 338).
- Single number variants that use bit counting (LC 137).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does `n & (n - 1)` clear the lowest set bit?
Subtracting 1 flips all the trailing zero bits to 1 and the lowest 1-bit to 0. ANDing the result with n keeps only the bits that were 1 in both, which is everything ABOVE the lowest 1-bit.
Could I use a lookup table?
Yes — precompute popcount for 0..255, then process 4 bytes. Useful for hot paths. CPUs also have a `popcnt` instruction; in JavaScript you'd just trust the engine.
Practice these live with InterviewChamp.AI
Drill Number of 1 Bits and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →