2Brute2Best
πŸ”₯0

Number of 1 Bits

EasyΒ·LeetCode #191Basic Anchor
LeetCode
30-Second Mental ModelBit Counting β€” clear the lowest set bit
Complexity:O(32)β†’O(k)
Key Insight

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.

Core Validity Invariant

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.

01

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?

Input

A positive integer n.

Output

The number of 1 bits in the binary form of n.

Example 1
Input: n = 11
Output: 3

11 is 1011 in binary. It has three 1 bits.

Example 2
Input: n = 128
Output: 1

128 is 10000000. Only one bit is set.

Example 3
Input: n = 2147483645
Output: 30

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.

Constraints
  • β€’ 1 ≀ n ≀ 2^31 βˆ’ 1
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck β€” each hint is counted.

  1. 1.

    To count the 1 bits, how can I look at one bit at a time?

  2. 2.

    Which operation tests the lowest bit of a number? Which one removes it?

  3. 3.

    Does the loop need 32 steps every time, or can it depend on the number itself?

  4. 4.

    Does a number with only two 1 bits need to look at all of its 0 bits?

  5. 5.

    What does n βˆ’ 1 change in the bits of n? Try it on a small number first.

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceCheck each of the 32 bit positions and count the 1sO(32)O(1)
Optimaln & (n βˆ’ 1) removes the lowest 1 bit; count the removals until n is 0O(k)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • β€œ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.
count the 1 bits+n & (n βˆ’ 1) clears the lowest 1 bit+loop until n is 0β†’Bit Counting β€” clear the lowest set bit

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.

10

Don't make this mistake

Confusing n & 1 with n & (n βˆ’ 1)
βœ— while (n != 0) { count += n & 1; // tests the lowest bit n = n & (n - 1); // removes the lowest 1 bit }
βœ“ while (n != 0) { n = n & (n - 1); count++; }

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.

Typo: n + 1 instead of n βˆ’ 1
βœ— n = n & (n + 1);
βœ“ n = n & (n - 1);

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.

Shifting right instead of removing the lowest bit, with a negative n
βœ— while (n != 0) { count += n & 1; n >>= 1; }
βœ“ uint32_t x = (uint32_t)n; while (x != 0) { x &= x - 1; count++; }

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.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.