Number of 1 Bits

EasyBit Manipulation

Given a non-negative integer n, return the number of 1 bits in its binary representation. This count is also called the Hamming weight or population count of n.

Constraints

  • 0 <= n <= 2^31 - 1

Examples

Example 1

Input: n = 13
Output: 3

13 is 1101 in binary: three 1 bits.

Example 2

Input: n = 64
Output: 1

64 is 1000000 in binary: a power of two has exactly one 1 bit.

Example 3

Input: n = 2147483647
Output: 31

2^31 - 1 is thirty-one 1 bits in a row, the largest allowed input.

JavaScript you'll use: Bitwise operators Numbers, Math and integer tricks

/**
 * @param {number} n
 * @return {number}
 */
function hammingWeight(n) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.