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

155
Vistas
Why do I get negative value when `or` a large number in nodejs?

I am using nodejs 16 and I have below code:

> a=Math.pow(2, 31)
2147483648
> a|a
-2147483648

As you can see the result of a|a is negative value. I expect to be the same value as a. But why it gives me negative value? What can I do to get the original value?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

What's happening here is pretty well explained on LinuxHint's JavaScript Bitwise Operators

In JavaScript numbers are stored as 64 bits; however, all bitwise operations are worked on binary numbers with 32 bits. Therefore, JavaScript converts numbers into 32 bits before the implementation of bitwise operators. After the implementation of bitwise operators, the numbers are converted back into 64 bits.

That's why the problem is present for Math.pow(2, 31) or higher numbers.

Normally, you would expect that an integer a is equal to a | a, but due to the reason above, this is not the case.

const orMatchesSelf = (a) => console.log(a == (a | a));

orMatchesSelf(1) // true 
orMatchesSelf(12314) // true
orMatchesSelf(Math.pow(2, 31) -1 ) // true
orMatchesSelf(Math.pow(2, 31)) // false
about 4 years ago · Santiago Trujillo 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