Skip to main content

Number of 1 Bits

EasyBit Manipulation

Write a function that takes the binary representation of a positive integer and returns the number of set bits (1s) it has (also known as the Hamming weight).

Example:

Input: n = 11
Output: 3
Explanation: The binary representation of 11 is 1011, which has three set bits.

Constraints:

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