As we know the recommended way to create a hash code out of a long value in Java is
(int) (this.longValue()^(this.longValue()>>>32))
However the other possible way to create a hash code out of a long value is simply
(int) this.longValue()
If we're considering all the longs from Long.MIN_VALUE
to Long.MAX_VALUE
together, the function
(int) this.longValue()
will basically generate following values:
the range will be
[Integer.MIN_VALUE; Integer.MAX_VALUE]
I'm not sure 100% but values would probably be evenly distributed
So if we're not talking about special cases like storing two ints in one long and treat upper and lower bytes separately - why are we end up using bitwise operations instead of simply taking int part?
Is there are any guidelines? Or maybe there are some underlying theory?
The similar question already has been asked in Bit-shifting in Effective Java hashCode() implementation, but quote from there:
You could just take the bottom 32 bits - but then that means changes in only the top 32 bits would be ignored, which wouldn't make it a very good hash.
So I'm particularly interested why taking the bottom 32 bits is 'wouldn't make it a very good hash'.