www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Low order hashes

reply Michelle Long <HappyDance321 gmail.com> writes:
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.
Jan 01
parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jan 02