Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
Node JS function that takes an unsigned integer and returns wrong number of '1' bits

This Node JS function takes input as signed 2's complement and any particular reason why it returns wrong number of '1' bits Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.

Here is function which should return the output as 31 whereas it returns 1

            var hammingWeight = function(n) {
                let count = 0;
                while (n !== 0) {
                  n = n & (n - 1);
                  count++;
                }
              
                return count;
              };

            console.log(hammingWeight(11111111111111111111111111111101))

Here is console output

            PS C:\VSB-PRO> node Fibo.js
            1

any reason what could have gone wrong in the javascript code,your help is highly appreciated

Regards,

Carolyn

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you call hammingWeight(101), you are not using the binary string 1012 (4+1 = 5), but the decimal number 10110 (one hundred and one). Try instead hammingWeight(0b101).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!