Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

271
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda