Number of 1 Bits

Easy

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

Example 1

hammingWeight()
Input
n = 11
Output
3

Why

1011 has three set bits.

Example 2

hammingWeight()
Input
n = 128
Output
1
Constraints
0 <= n <= 2^31 - 1

Topics

Bit Manipulation

Implement hammingWeight in the editor and return the answer — printing it is never graded.