Number of 1 Bits — Bit Manipulation
Count how many bits are switched on in the binary form of an unsigned machine word.
Bit Manipulation · Easy
The problem
A hardware register holds a 32-bit value, and you want to know how many of its switches are on. Given a non-negative whole number that fits in 32 bits, count how many of its binary digits are 1.
The story
A hardware register holds a 32-bit value from 0 to 4294967295, and each binary digit drives one switch. Count how many switches are on, that is, how many 1s appear in the number's binary form.
What to return
- Return the count of 1 digits in the binary form of n.
Constraints
- 0 <= n <= 4294967295 (that is 2^32 - 1)
- Your code may run at most 384 counted steps on the largest case.
Example
- Input
n = 13- Output
3