Home
DSA Patterns
Bit Manipulation
Number of 1 Bits
Number of 1 Bits (LeetCode 191)
Number of 1 Bits (LeetCode 191): count the set bits of an integer by clearing the lowest one with n & (n - 1) until nothing is left.
Problem Understanding
Number of 1 Bits: given a positive integer n, return how many 1s its binary form has (its Hamming weight).
Example: n = 11 is 1011 in binary, so the answer is 3.
Attempt 1: Check Every Bit
Test the last bit with n & 1, add it to the count, shift n right by one, and repeat until n is 0. That works, but it spends a round on every position up to the highest set bit, including all the zeros.
The Intuition: Jump Straight to the Next 1
Look at what subtracting 1 does in binary: the lowest 1 becomes 0, and every 0 below it becomes 1. Everything above stays the same.
n = 1 0 1 1 0 0n - 1 = 1 0 1 0 1 1n & (n - 1) = 1 0 1 0 0 0
So n & (n - 1) is n with its lowest 1 removed. Repeat until n is 0 and count the rounds: one round per 1 bit, and the zeros are skipped entirely.
Number of 1 Bits Visualization
BIT 0 IS ON THE RIGHT
START
n
7
6
5
4
3
2
1
0
n − 1
n & (n − 1)
n
11
count
0
n = 11 = 1011 in binary. count = 0
Next
n & (n − 1) = 1011 & 1010 = 1010: the lowest 1, at bit 0, is cleared. count = 1
n
Three rows: n, n - 1 with the bits it flips filled in, and n & (n - 1). The lowest set bit of n is marked; in the result it is gone and nothing else has changed.
Number of 1 Bits Dry Run
Input: n = 11
n |
n - 1 |
n & (n - 1) |
count |
|---|---|---|---|
| 1011 | 1010 | 1010 | 1 |
| 1010 | 1001 | 1000 | 2 |
| 1000 | 0111 | 0000 | 3 |
Three rounds for three set bits: the answer is 3.
Number of 1 Bits LeetCode Solution
Clear the lowest set bit until n is 0, counting the rounds.
Number of 1 Bits Code
1var hammingWeight = function(n) {2 let count = 0;3 while (n !== 0) {4 n &= n - 1;5 count++;6 }7 return count;8};
Number of 1 Bits in Python, Java, C++, TypeScript and C
Number of 1 Bits in Python
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
while n:
n &= n - 1
count += 1
return countNumber of 1 Bits in Java
public class Solution {
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
n &= n - 1;
count++;
}
return count;
}
}Number of 1 Bits in C++
class Solution {
public:
int hammingWeight(uint32_t n) {
int count = 0;
while (n) {
n &= n - 1;
count++;
}
return count;
}
};Number of 1 Bits in TypeScript
function hammingWeight(n: number): number {
let count = 0;
while (n !== 0) {
n &= n - 1;
count++;
}
return count;
}Number of 1 Bits in C
int hammingWeight(uint32_t n) {
int count = 0;
while (n) {
n &= n - 1;
count++;
}
return count;
}Edge Cases & Common Mistakes
n = 0: the loop never runs; the answer is0.- All 32 bits set: in Java, an
intwith the top bit set is negative;n != 0still works becausen & (n - 1)clears bits regardless of sign. - Using
n > 0as the loop condition in Java: a negativenwould never enter the loop. Usen != 0. - Python integers: they are unbounded, so a negative input would loop forever; LeetCode's inputs are non-negative.
Summary
n & (n - 1) removes the lowest set bit. Repeating it until n is 0 counts the 1s in one round per set bit. The same identity tests powers of two and drives the Counting Bits DP.
More practice: DSA interview questions with answers · patterns cheat sheet
