Reputation: 16325
Is there an algorithm that will yield the same hash for two numbers, no matter what order they are in?
For example, hashing 3268
and 2642
should yield the same result as hashing 2642
and 3268
.
Is this possible?
Upvotes: 3
Views: 3014
Reputation: 67378
Of course, XOR does that.
3268^2642 == 2642^3268
There's a lot more (addition, multiplication, basically any commutative operation), but XOR is usually used for hashing anyway (because it's easy to "unhash").
Upvotes: 5
Reputation: 23332
Hash the two numbers separately (using an integer-to-integer hash of your choice), and then either add or xor the results.
Upvotes: 4