Description

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

Examples

Input:n = 11
Output:3
Explanation:

The binary of 11 is 1011, which has three 1 bits.

Input:n = 128
Output:1
Explanation:

The binary of 128 is 10000000, which has one 1 bit.

Input:n = 2147483645
Output:30
Explanation:

The binary has 30 set bits.

Constraints

  • 1 ≤ n ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!