Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing function for four unsigned integers (C++)

Tags:

c++

integer

hash

I'm writing a program right now which produces four unsigned 32-bit integers as output from a certain function. I'm wanting to hash these four integers, so I can compare the output of this function to future outputs.

I'm having trouble writing a decent hashing function though. When I originally wrote this code, I threw in a simple addition of each of the four integers, which I knew would not suffice. I've tried several other techniques, such as shifting and adding, to no avail. I get a hash, but it's of poor quality, and the function generate a ton of collisions.

The hash output can be either a 32-bit or 64-bit integer. The function in question generates many billions of hashes, so collisions are a real problem here, and I'm willing to use a larger variable to ensure that there are as few collisions as possible.

Can anyone help me figure out how to write a quality hash function?

like image 657
jakogut Avatar asked Nov 30 '09 06:11

jakogut


1 Answers

Why don't you store the four integers in a suitable data structure and compare them all? The benefit of hashing them in this case appears dubious to me, unless storage is a problem.

If storage is the issue, you can use one of the hash functions analyzed here.

like image 76
Vinko Vrsalovic Avatar answered Oct 28 '22 15:10

Vinko Vrsalovic