Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
Number.toString(2) is omitting 0's from end in javascript?

I am trying to reverse the 32-bit unsigned integer by converting it to a string first but the toString(2) function is committing the zero's from the end and because of it the output I am getting is wrong.

MyCode:

var reverseBits = function(n) {
    let reverserN=(n>>>0).toString(2).split('').reverse().join('');
    console.log(reverserN)
    
    return parseInt(reverserN,2)
};

Output:

Your input 00000010100101000001111010011100

stdout 00111001011110000010100101 //0's omitted from the end

Output 15065253 (00000000111001011110000010100101)

Expected 964176192 (00111001011110000010100101000000)

And, if I try to use BigInt then it is giving me n at the end. Like this 00111001011110000010100101n.

Why 0's are omitted? And how can I stop it from omitting 0's from the end?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

By adding padEnd(32,0) after join('') works. Which simply added 0's at the end to the resulting string so that it reaches a given length.

Mycode:

var reverseBits = function(n) {
    let reverserN=(n>>>0).toString(2).split('').reverse().join('').padEnd(32,0);
    console.log(reverserN);
    return parseInt(reverserN,2)
};

Output:

00111001011110000010100101000000 //6 0's at the end of the string

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda