Created
October 6, 2022 09:36
-
-
Save i-e-b/f748117fd573b2423b64df6233f3c07e to your computer and use it in GitHub Desktop.
Low bias 32 bit hash in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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