-
-
Save papamuziko/7bb52dfbb859fdffc4bd0f95b76f71e8 to your computer and use it in GitHub Desktop.
djb2: This algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. another version of this algorithm (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why it works better than many other constants, prime or not) has never been adequately explained.
This file contains 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
/** | |
* hash_djb2 - implementation of the djb2 algorithm | |
* @str: string used to generate hash value | |
* | |
* Return: hash value | |
*/ | |
unsigned long int hash_djb2(const unsigned char *str) | |
{ | |
unsigned long int hash; | |
int c; | |
hash = 5381; | |
while ((c = *str++)) | |
{ | |
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ | |
} | |
return (hash); | |
} |
the djb2 algorithm is sleek
Thank all good here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works as expected