Skip to content

Instantly share code, notes, and snippets.

@i-e-b
Created October 6, 2022 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i-e-b/f748117fd573b2423b64df6233f3c07e to your computer and use it in GitHub Desktop.
Save i-e-b/f748117fd573b2423b64df6233f3c07e to your computer and use it in GitHub Desktop.
Low bias 32 bit hash in C#
public static uint DataHash(byte[] data, uint len)
{
var hash = len;
for (uint i = 0; i < len; i++)
{
hash += data[i];
hash ^= hash >> 16;
hash *= 0x7feb352d;
hash ^= hash >> 15;
hash *= 0x846ca68b;
hash ^= hash >> 16;
}
hash ^= len;
hash ^= hash >> 16;
hash *= 0x7feb352d;
hash ^= hash >> 15;
hash *= 0x846ca68b;
hash ^= hash >> 16;
hash += len;
// never return zero value
return hash == 0 ? 0x800800 : hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment