Low order hashes

Steven Schveighoffer schveiguy at gmail.com
Wed Jan 2 15:00:50 UTC 2019


On 1/1/19 2:40 PM, Michelle Long wrote:
> I need to hash a few strings to a byte, short, or int. hashOf works for 
> int only on x86.
> 
> What would be a nice way to accomplish this? Collisions are not good but 
> not catastrophic. Mainly I need a unique ID to emulate enums. I might 
> just chop off the extra bits or mash them up somehow and it will 
> probably work but hoping for something more solid.
> 

Not from experience, but just thinking out loud, you could take the int 
and xor the parts together.

in other words:

auto x = hashOf(y);
ushort realHash = (x ^ (x >> 16)) & 0xffff;

ubyte would be 4 terms, but still not too bad.

-Steve


More information about the Digitalmars-d-learn mailing list