Skip to main content
CodeOath
← All problems

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.

Example 1
Input
n = 1000
Output
6
Explanation

1000 is 1111101000 in binary, which has six 1 digits.

Example 2
Input
n = 0
Output
0
Explanation

0 has no 1 bits, so the count is zero.

Constraints:

  • 0 <= n <= 2^31 - 1

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.