 All Problems
Number of 1 Bits
easy
bit manipulation
amazon
microsoft
apple

Write a function that takes the binary representation of a positive integer and returns the number of set bits it has (also known as the Hamming weight).

Example 1:

Input: 11
Output: 3
Explanation: 11 in binary is 1011, which has 3 set bits.

Example 2:

Input: 128
Output: 1
Explanation: 128 in binary is 10000000, which has 1 set bit.

Example 3:

Input: 2147483645
Output: 30

Constraints:

  • 1 ≤ n ≤ 2³¹ − 1

Input format: A single non-negative integer.

Output format: A single integer — the count of 1 bits.

Run to check your code against the sample cases, or submit to run every case