Count Set Bits

Easy~10 min

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

For example, the integer 11 in binary is 1011, which has three 1 bits.

Examples

Example 1
Input: n = 11
Output: 3
Explanation: 11 in binary is 1011, which has three 1-bits.
Example 2
Input: n = 128
Output: 1
Explanation: 128 in binary is 10000000, which has one 1-bit.
Example 3
Input: n = 255
Output: 8
Explanation: 255 in binary is 11111111, which has eight 1-bits.

Constraints

  • 0 <= n <= 2^31 - 1
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run