Number of 1 Bits
n & (n β 1) clears the lowest set bit of n and keeps every other bit. So each loop pass removes exactly one 1 bit and skips the zeros entirely. The number of passes is the number of 1 bits.
The loop is correct because each pass removes exactly one 1 bit and no other bit changes. So when n reaches 0, the count equals the number of 1 bits that were in the original n.
Problem
Given a positive integer n, count how many of its bits are 1 when it is written in binary. This count is also called the Hamming weight.
Follow-up: if this function is called many times, how would you speed it up?
A positive integer n.
The number of 1 bits in the binary form of n.
11 is 1011 in binary. It has three 1 bits.
128 is 10000000. Only one bit is set.
The binary form is 31 digits long, and only one of them (the second from the end) is a 0. So there are 30 ones.
- β’ 1 β€ n β€ 2^31 β 1
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
To count the 1 bits, how can I look at one bit at a time?
- 2.
Which operation tests the lowest bit of a number? Which one removes it?
- 3.
Does the loop need 32 steps every time, or can it depend on the number itself?
- 4.
Does a number with only two 1 bits need to look at all of its 0 bits?
- 5.
What does
n β 1change in the bits ofn? Try it on a small number first.
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Check each of the 32 bit positions and count the 1s | O(32) | O(1) |
| Optimal | n & (n β 1) removes the lowest 1 bit; count the removals until n is 0 | O(k) | O(1) |
Why does this belong to this pattern?
- βnumber of set bitsβThe answer is a count of 1 bits, so each 1 bit must be counted once.
- βbinary representationβWork on the bits directly with `&` and shifts, not on the decimal digits.
- βpositive integer, up to 2^31 β 1βThe width is fixed at 32 bits, so the bit tricks never run past the end.
This is the anchor for the counting sub-pattern. The key move is n & (n β 1): it removes the lowest 1 bit in one step. Do not confuse it with n & 1, which only tests the lowest bit. Power of Two uses the same move to check for a single 1 bit.
Don't make this mistake
This is the pattern-confusion trap. n & 1 only looks at the bottom bit, and the wrong loop checks it before removing a bit. On n = 182 = 10110110 the bottom bit is 0, and clearing a 1 bit never makes it 1. So the wrong loop only ever sees 0s and returns 0, not 5.
n + 1 does not clear the lowest 1 bit. For n = 182, 182 & 183 is still 182, so n never changes and the loop never ends.
This works for positive n, but it always takes one step per bit position. It is also a trap for negative numbers: >> on a signed int copies the sign bit down, so -8 >> 1 is -4, then -2, then -1, and it never reaches 0. LeetCode's constraints say n is positive, but the unsigned copy avoids the trap.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.