Search code examples
javahashcodebitwise-operatorsgethashcodeeffective-java

bitwise operator >>> in hashCode


I have two related questions:

  1. the bitwise operator >>> means that we are shifting the binary number by those many places while filling 0 in the Most Significant Bit. But, then why does the following operation yields the same number: 5>>>32 yields 5 and -5>>>32 yields -5. Because if the above description is correct then both these operations would have yielded 0 as the final result.

  2. In continuation to above, As per Effective Java book, we should use (int) (f ^ (f >>> 32)) (in case the field is long) while calculating the hash code (if the field is long). Why do we do that and what's the explanation


Solution

  • Answer to your first question is here why is 1>>32 == 1?

    The second question answer, in short, is that in such way the whole long value is used(not a part of it) and note that it is probably the fastest way to do this.