Number of 1 Bits
EasyBit Manipulation
Given a non-negative integer n, return the number of 1 bits in its binary representation (also called its Hamming weight).
For example, 13 is 1101 in binary, which has three 1 bits.
Example 1
- Input:
- n = 45
- Output:
- 4
- Explanation
- 45 in binary is
101101, which has four 1 bits.
Example 2
- Input:
- n = 2147483648
- Output:
- 1
- Explanation
- 2147483648 is 231: a single 1 followed by 31 zeros.
Example 3
- Input:
- n = 4294967295
- Output:
- 32
- Explanation
- 4294967295 is 232 - 1, so all 32 bits are 1.
Constraints
0 <= n <= 2^32 - 1