The Hamming Weight

MediumAcc. 92.4%
+25 XP 10

Counting the Charge

In bitwise computation, the Hamming weight is the count of symbols that are different from zero. For integers, this is the count of 1s in their binary form.

The Assignment

Your function receives a parameter num.

  1. Initialize count to 0.
  2. Use a while loop as long as num is greater than 0.
  3. In each step:
    • Check if the number is odd (num % 2 !== 0). If yes, increment count.
    • Update num by dividing by 2 and rounding down.
  4. Print the final count.

01EXAMPLE 1

Inputnum = 7
Output3

Explanation: 7 is 111 in binary (three 1s).

Constraints

  • Manual bit-counting loop.
  • Do not use toString(2) or bitwise operators.
MathLoopsBinary
JavaScript
Loading...
3 Hidden

Input Arguments

num7

Expected Output

3

Click RUN to test your solution