← All problemsExample 1 Example 2
Problem
Number of 1 Bits
Easy- bit-manipulation
Write the non-negative integer n in binary and count the digits that are 1. Return that count. It is called the Hamming weight of n.
- Input
n = 1000- Output
6- Explanation
1000 is
1111101000in binary, which has six1digits.
- Input
n = 0- Output
0- Explanation
0has no1bits, so the count is zero.
Constraints:
0 <= n <= 2^31 - 1
Tab indents. Press Esc, then Tab to leave the editor.
Ctrl or ⌘ + Enter runs the tests.
Run your code to see every test here. Nothing is submitted or recorded.