Back to problems

Number of 1 Bits

Algorithm · Medium

Given a positive integer n, determine how many bits with value 1 appear in its binary representation. This quantity is often called the population count or Hamming weight. Examples Example 1 Explanation: The binary form of 13 is 1101, which contains three 1‑bits. Example 2 Explanation: 64 in binary is 1000000; it contains exactly one 1‑bit. Example 3 Explanation: The binary representation is 31 bits long, and all bits except the second‑least significant are set to 1.…

Checking your access…