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

339
Views
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 answers
Answer question

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 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!