Number of 1 Bits
Table of Contents + β
Number of 1 Bits is a small question with a famous twist. Anyone can count the ones by checking each bit. But there is a clever trick that skips all the zero bits. The interviewer wants to see if you know it. So let us learn both ways.
π― The Problem
You get a number. You count how many bits in its binary form are 1. The count of 1 bits even has a name. It is called the Hamming weight.
The rules:
- A bit is a single
0or1inside the binary form of a number. - Count only the bits that are
1. - Return that count.
Let us take the number 11. In binary, 11 is 1011. Count the ones. There are three of them. So the answer is 3.
Input: n = 11 (binary 1011)Output: 3
Explanation: The binary 1011 has three bits set to 1.So the job is simple. Look at the binary form. Count the ones.
Here is the binary of 11 laid out bit by bit. The shaded bits are the ones we count.
π’ Approach 1: Check Every Bit (Brute Force)
The idea in one line: look at each bit one by one and count the ones.
The idea:
- A number has a fixed number of bits, usually 32 for an integer.
- Loop once per bit, so 32 times.
How it works:
n & 1is a bit AND that reads the lowest bit.- AND gives
1only when both bits are1, son & 1is1when the lowest bit is on. - If it is on, add one to the count.
- Shift the number right by one, so the next bit becomes the new lowest bit.
Why it is weak:
- It always loops the full bit width, even for a tiny number.
- A number with one
1bit still costs 32 steps. - The work is fixed no matter how few ones there are.
Here is the check-every-bit code:
def hamming_weight(n): count = 0 for bit in range(32): if n & (1 << bit): count += 1 return countβ‘ Approach 2: Brian Kernighanβs Trick (Best)
The idea in one line: jump straight from one 1 bit to the next, skipping every zero.
The idea:
- We use a trick named after Brian Kernighan, a computer scientist.
- The whole trick is one line:
n = n & (n - 1).
How it works:
- Subtracting one flips the lowest
1bit to0and turns the zeros below it into ones. - The AND with the original
nclears that lowest1bit. - So one move removes exactly one
1bit. - Repeat and count until
nbecomes0.
Why it is fast:
- It loops only as many times as there are ones.
- Three ones means three loops.
- It never wastes time on the zero bits.
Here is a picture of the trick removing ones from 11, which is 1011, until nothing is left.
Steps to Solve
- Set a counter to
0. - While the number is not
0, do the next steps. - Run
n = n & (n - 1), which removes the lowest1bit. - Add one to the counter for each removal.
- Stop when the number reaches
0. - Return the counter.
This Python version uses the same n & (n - 1) trick in a while loop.
def hamming_weight(n): count = 0 while n != 0: n = n & (n - 1) # clear the lowest 1 bit count += 1 # we removed one set bit return count
n = 11 # binary 1011print(hamming_weight(n))The output of the above code will be:
3Let us read the Python version slowly and see why each line is there.
def hamming_weight(n): count = 0 while n != 0: n = n & (n - 1) count += 1 return countThe line count = 0 starts our tally at nothing. We have not removed any 1 bits yet.
The line while n != 0: keeps the loop running until the number is fully empty of ones. When n becomes 0, there is nothing left to count, so we stop.
The line n = n & (n - 1) is the trick itself. Subtracting one flips the lowest 1 bit off and fills the lower zeros with ones. The AND with the old n then wipes that lowest 1 bit clean. So one set bit disappears each pass. We never touch the zero bits, which is why this is fast.
The line count += 1 records that we just removed one set bit. Since each loop removes exactly one, the count grows by exactly the number of ones.
The line return count gives back the final tally. It equals the number of 1 bits in the original number.
β±οΈ Time and Space Complexity
The check-every-bit way always does a fixed number of steps, one for each bit, so it is O(k) where k is the bit width, usually 32. The Brian Kernighan way loops only once per 1 bit. So if a number has few ones, it does little work. Both use just a counter, so the memory is O(1). The Brian Kernighan trick is the one to show off in the interview.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Check every bit | O(k) bits | O(1) |
| Brian Kernighan trick | O(set bits) | O(1) |
Tip
The line to memorize is n = n & (n - 1). It clears the lowest set bit. If you can explain why subtracting one then AND-ing does that, you have shown real understanding of how bits work.
π§© Key Takeaways
- β A 1 bit is a bit set to 1 inside the binary form of a number.
- β The simple way checks each bit with n & 1 and shifts right.
- β
Brian Kernighanβs trick
n & (n - 1)clears the lowest 1 bit in one step. - β That trick loops only once per set bit, so it skips all the zeros.
- β Both ways use O(1) memory, but the trick is faster for sparse numbers.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does the Number of 1 Bits problem ask you to count?
Why: It asks for the count of bits that are 1, also called the Hamming weight.
- 2
What does the expression n & (n - 1) do?
Why: Subtracting one then AND-ing removes the lowest set bit in a single step.
- 3
Why is the Brian Kernighan trick faster for numbers with few ones?
Why: The loop runs only as many times as there are 1 bits, so it ignores the zero bits.
- 4
What is the binary of 11 and how many 1 bits does it have?
Why: 11 in binary is 1011, which has three bits set to 1.