Count the 1 bits of a number
Standard · Write code · about 8 min
Return how many bits are 1 in the binary form of n. Check one bit at a time.
Example 1
Input:
[13]
Output:
3
13 is 1101.
Example 2
Input:
[0]
Output:
0
Constraints
- 0 ≤ n ≤ 1,000,000,000
Learn the idea first: Bits, bytes and binary numbers, a visual lesson.
More Bits, bytes and binary numbers problems
- Write a number in binary Standard
- Add two binary strings Challenge