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

333
Vistas
How is multiplying by odd does not lose information in overflow?

The standard approach to calculate a hashcode is to multiply with 31.
Bloch states:

The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, information would be lost, as multiplication by 2 is same as shifting

I am not sure I understand this. If the multiplication overflows value is lost regardless of even/odd right?
In the following example I am not sure what is the difference:

int number = 2000000000;  
System.out.println(Integer.toBinaryString(number));  
number*=2;   
System.out.println(Integer.toBinaryString(number));    
1110111001101011001010000000000  
11101110011010110010100000000000  

and

int number = 2000000000;  
System.out.println(Integer.toBinaryString(number));  
number*=3;  
System.out.println(Integer.toBinaryString(number));  
1110111001101011001010000000000  
1100101101000001011110000000000  
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Even numbers always forget at least the most significant bit. The msb only affects the result if the multiplier is odd, because only when shifted left by 0 steps does it not immediately disappear. That is also what was meant by "information would be lost, as multiplication by 2 is same as shifting". In your example you don't run into much visible trouble because the result does not overflow, 2000000000 = 0x77359400 clearly does not have the top bit set (it wraps to negative as a signed integer but that's irrelevant, the sign bit is a normal bit that carries a bit of information, shifting something into it doesn't destroy information), working backwards it is ambiguous whether it was 0x77359400 before shifting or 0xF7359400. I hope this is sufficient "intuitive justification".

The real proof that only odd multipliers preserve all information is that it is precisely odd numbers that have multiplicative inverses modulo a power of two. x has a multiplicative inverse modulo m iff gcd(x, m) == 1, given that m is 2k that condition holds only for odd numbers: it cannot hold for even numbers because they'd have a factor of 2 in common, and since an odd number has no factors of 2 but 2k only has factors of 2, they never share any factors.

The fact that there is an inverse inv(x) means you can write number * x * inv(x) = number so no information could have been lost in the first multiplication.

As a concrete example, inv(3) = 0xaaaaaaab so:

2000000000 * 3 = 0x65a0bc00
0x65a0bc00 * 0xaaaaaaab = 2000000000
over 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