Blind 75
71 / 75Free practice

Number of 1 Bits

Easy · Bit manipulation

Jump to code

Count the set bits in an unsigned 32-bit integer.

Examples

Example 1

n = 11

Output: 3

Example 2

n = 128

Output: 1

Constraints

  • 0 <= n <= 2^32 - 1; treat the input as an unsigned 32-bit integer.
Need a hint?

Subtracting one changes the lowest set bit and the bits beneath it.

Real bit manipulation interview questions

Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  11
]
Expected
3
Case 2
Input
[
  128
]
Expected
1
Case 3
Input
[
  2147483645
]
Expected
30
Case 4
Input
[
  0
]
Expected
0
Case 5
Input
[
  1
]
Expected
1

Drafts are saved per language in this browser when storage is available.