Number of 1 Bits
Easy · bit manipulation
Every non-negative integer can be written in binary, as a row of 0 and 1 digits (bits). Given a non-negative integer n, return how many of its bits are 1.
For example, 13 is 1101 in binary, so it has three bits equal to 1.
Examples
Example 1
Input: n = 11
Output: 3
11 is 1011 in binary, which has three 1 bits.
Example 2
Input: n = 128
Output: 1
128 is 10000000 in binary, which has a single 1 bit.
Example 3
Input: n = 4294967295
Output: 32
4294967295 is 2^32 - 1, which is thirty-two 1 bits in a row.
Constraints
- 0 <= n <= 2^32 - 1 (n fits in 32 bits)