Why are XOR often used in java hashCode() but another bitwise operators are used rarely?
问题 I often see code like int hashCode(){ return a^b; } Why XOR? 回答1: Of all bit-operations XOR has the best bit shuffling properties. This truth-table explains why: A B AND 0 0 0 0 1 0 1 0 0 1 1 1 A B OR 0 0 0 0 1 1 1 0 1 1 1 1 A B XOR 0 0 0 0 1 1 1 0 1 1 1 0 As you can see for AND and OR do a poor job at mixing bits. OR will on average produce 3/4 one-bits. AND on the other hand will produce on average 3/4 null-bits. Only XOR has an even one-bit vs. null-bit distribution. That makes it so