Count Bits
easySave
Bit ManipulationMath
Given a non-negative integer n, write a function to return the number of '1' bits it has, also known as the Hamming weight. This function is often referred to as the bit count function.
Example 1
Input
[ 11 ]
Output
3
Explanation. The binary representation of 11 is '1011', which has three '1' bits.
Example 2
Input
[ 128 ]
Output
1
Explanation. The binary representation of 128 is '10000000', which has one '1' bit.
Example 3
Input
[ 255 ]
Output
8
Explanation. The binary representation of 255 is '11111111', which has eight '1' bits.
Follow-up: Could you solve it without using any loop/statements (like a built-in function in your chosen language)? How does the performance change?
Constraints:
The integer `n` is non-negative.
- Accepted
- 2/2
- Acceptance Rate
- 100.0%
- Views
- 1