Think of a panel of light switches. Each binary digit (bit) of a number is one switch. A 1 means the switch is on. A 0 means it is off.
Given a positive whole number n, count how many of its bits are 1. This count is also called the number of set bits, or the Hamming weight.
Examples
Example 1:
Input: n = 22
Output: 3
Explanation: 22 in binary is 10110. Three of the five switches are on.
Example 2:
Input: n = 1000
Output: 6
Explanation: 1000 in binary is 1111101000. It has six 1s.
Example 3:
Input: n = 1048575
Output: 20
Explanation: This number is 2^20 - 1. In binary it is twenty 1s in a row.