Skip to content

Number of 1 Bits

EasyBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

Return how many 1 bits there are in the binary form of the non-negative integer n (this count is called the Hamming weight).

n = 11          →  3    (1011)
n = 128         →  1    (10000000)
n = 2147483647  →  31   (thirty-one 1s)

Constraints

  • 0 <= n <= 2^31 - 1