Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I am trying to generate a hash code from two integer inputs. The approach outlined in

Combining Java hashcodes into a "master" hashcodeCombining Java hashcodes into a "master" hashcode

seems to work well for many input values. However, when one of the input integers is int.MinValue, the behavior seems less than ideal. Specifically I observe

int.MinValue * 1013 == int.MinValue int.MinValue * 1009 == int.MinValue 

but

int.MinValue * 2 == 0 int.MinValue * 20 == 0 

All of this is in an unchecked context.

I would naively (and wrongly) assume that int.MinValue * (something other than 1 or 0) would yield a new bit pattern different than int.MinValue or 0.

Questions

  1. Why does multiplying int.MinValue by these constants yield int.MinValue (2 cases) or 0 (2 cases)?
  2. Does the behavior of int.MinValue indicate a flaw in the hash algorithm?

I am trying to generate a hash code from two integer inputs. The approach outlined in

Combining Java hashcodes into a "master" hashcode

seems to work well for many input values. However, when one of the input integers is int.MinValue, the behavior seems less than ideal. Specifically I observe

int.MinValue * 1013 == int.MinValue int.MinValue * 1009 == int.MinValue 

but

int.MinValue * 2 == 0 int.MinValue * 20 == 0 

All of this is in an unchecked context.

I would naively (and wrongly) assume that int.MinValue * (something other than 1 or 0) would yield a new bit pattern different than int.MinValue or 0.

Questions

  1. Why does multiplying int.MinValue by these constants yield int.MinValue (2 cases) or 0 (2 cases)?
  2. Does the behavior of int.MinValue indicate a flaw in the hash algorithm?

I am trying to generate a hash code from two integer inputs. The approach outlined in

Combining Java hashcodes into a "master" hashcode

seems to work well for many input values. However, when one of the input integers is int.MinValue, the behavior seems less than ideal. Specifically I observe

int.MinValue * 1013 == int.MinValue int.MinValue * 1009 == int.MinValue 

but

int.MinValue * 2 == 0 int.MinValue * 20 == 0 

All of this is in an unchecked context.

I would naively (and wrongly) assume that int.MinValue * (something other than 1 or 0) would yield a new bit pattern different than int.MinValue or 0.

Questions

  1. Why does multiplying int.MinValue by these constants yield int.MinValue (2 cases) or 0 (2 cases)?
  2. Does the behavior of int.MinValue indicate a flaw in the hash algorithm?
Code formatting
Source Link
Stefan Steinegger
  • 64.8k
  • 17
  • 132
  • 193

I am trying to generate a hash code from two integer inputs. The approach outlined in

Combining Java hashcodes into a "master" hashcode

seems to work well for many input values. However, when one of the input integers is int.MinValueint.MinValue, the behavior seems less than ideal. Specifically I observe

int.MinValue * 1013 == int.MinValue

int.MinValue * 1009 == int.MinValue

int.MinValue * 1013 == int.MinValue int.MinValue * 1009 == int.MinValue 

but

int.MinValue * 2 == 0

int.MinValue * 20 == 0

int.MinValue * 2 == 0 int.MinValue * 20 == 0 

All of this is in an unchecked context.

I would naively (and wrongly) assume that int.MinValue * (something other than 1 or 0)int.MinValue * (something other than 1 or 0) would yield a new bit pattern different than int.MinValueint.MinValue or 00.

Questions

  1. Why does multiplying int.MinValueint.MinValue by these constants yield int.MinValueint.MinValue (2 cases) or 00 (2 cases)?
  2. Does the behavior of int.MinValueint.MinValue indicate a flaw in the hash algorithm?

I am trying to generate a hash code from two integer inputs. The approach outlined in

Combining Java hashcodes into a "master" hashcode

seems to work well for many input values. However, when one of the input integers is int.MinValue, the behavior seems less than ideal. Specifically I observe

int.MinValue * 1013 == int.MinValue

int.MinValue * 1009 == int.MinValue

but

int.MinValue * 2 == 0

int.MinValue * 20 == 0

All of this is in an unchecked context.

I would naively (and wrongly) assume that int.MinValue * (something other than 1 or 0) would yield a new bit pattern different than int.MinValue or 0.

Questions

  1. Why does multiplying int.MinValue by these constants yield int.MinValue (2 cases) or 0 (2 cases)?
  2. Does the behavior of int.MinValue indicate a flaw in the hash algorithm?

I am trying to generate a hash code from two integer inputs. The approach outlined in

Combining Java hashcodes into a "master" hashcode

seems to work well for many input values. However, when one of the input integers is int.MinValue, the behavior seems less than ideal. Specifically I observe

int.MinValue * 1013 == int.MinValue int.MinValue * 1009 == int.MinValue 

but

int.MinValue * 2 == 0 int.MinValue * 20 == 0 

All of this is in an unchecked context.

I would naively (and wrongly) assume that int.MinValue * (something other than 1 or 0) would yield a new bit pattern different than int.MinValue or 0.

Questions

  1. Why does multiplying int.MinValue by these constants yield int.MinValue (2 cases) or 0 (2 cases)?
  2. Does the behavior of int.MinValue indicate a flaw in the hash algorithm?
edited tags
Link
Alexei Levenkov
  • 101.1k
  • 15
  • 138
  • 189
Source Link
Eric J.
  • 150.7k
  • 65
  • 353
  • 563
Loading