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 = 11Output:
3Explanation:
The binary of 11 is 1011, which has three 1 bits.
Input:
n = 128Output:
1Explanation:
The binary of 128 is 10000000, which has one 1 bit.
Input:
n = 2147483645Output:
30Explanation:
The binary has 30 set bits.
Constraints
- •
1 ≤ n ≤ 2³¹ - 1