Facebook PixelNumber of 1 Bits — Coding Practice
Number of 1 BitsEasy

Number of 1 Bits

Easy 6.1k70% acceptance
Bit ManipulationDivide and Conquer

Given a non-negative integer n, return the number of 1 bits in its binary representation (also called its Hamming weight).

Example 1
Input: n = 11
Output: 3
11 in binary is 1011, which has three set bits.
Example 2
Input: n = 128
Output: 1
128 is 10000000 — a single set bit.
Example 3
Input: n = 0
Output: 0
Constraints
  • 0 ≤ n ≤ 2³¹ − 1
Asked atAppleMicrosoftAmazon
JavaScript
Loading editor…
Case 1
11
expected: 3
Case 2
128
expected: 1
Case 3
0
expected: 0
Case 4
7
expected: 3
Case 5
2147483647
expected: 31
Case 6
4294967295
expected: 32