LC 191
LeetCode 191 · Easy

Number of 1 Bits

Count the set bits in an unsigned integer.


Try it

Step through the core mechanic. The simulator below runs the bit manipulation shape this problem is built on.

Walk the pattern

No dedicated step-through for this one yet. The shape is Bit manipulation — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.

The approach

Repeatedly clear the lowest set bit with n &= n−1 and count the iterations. This loops once per set bit rather than over all 32 positions.

AspectValue
PatternBit manipulation
Recognise it byPopulation count.
Time complexityO(set bits)
Space complexityO(1)
DifficultyEasy

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.