89. Number of 1 Bits
Easy · Bit Manipulation
Write a function that takes a positive integer and returns the number of 1-bits present in its binary representation.
For example, the number 5 has binary representation 101, which contains two 1-bits. The number 9 has binary representation 1001, which also contains two 1-bits.
Examples
Example 1 Input: 5 Output: 2 Explanation: 5 in binary is 101, which has two 1-bits.
Example 2 Input: 9 Output: 2 Explanation: 9 in binary is 1001, which has two 1-bits.
Constraints
- Standard input/output constraints apply