Count the number of set bits (1-bits) in the binary representation of the non-negative integer n. This is sometimes called the Hamming weight or population count.
One line: a non-negative integer n.
One line: the number of set bits in n.
Example 1
Input
11
Expected
3
Explanation
11 is 1011 in binary, which has three set bits.
Example 2
Input
0
Expected
0
Explanation
0 has no set bits.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →