Skip to content
Decoder

Number of 1 Bits

Easy

Problem

Return how many 1 bits there are in the binary form of the non-negative integer n (this count is called the Hamming weight).

Example 1

Input
n = 11
Output
3
Why
1011

Example 2

Input
n = 128
Output
1
Why
10000000

Example 3

Input
n = 2147483647
Output
31
Why
thirty-one 1s

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.