Last active
July 24, 2024 04:00
-
-
Save trvswgnr/8992941875549f8b67bc133f1f766950 to your computer and use it in GitHub Desktop.
various hash algorithms in c
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
#include <stdint.h> | |
uint32_t hash_djb2(unsigned char *str) { | |
uint32_t hash = 5381; | |
uint32_t c; | |
while ((c = *str++)) | |
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ | |
return hash; | |
} |
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
#include <stdint.h> | |
uint32_t hash_djb2_xor(unsigned char *str) { | |
uint32_t hash = 5381; | |
uint32_t c; | |
while ((c = *str++)) | |
hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */ | |
return hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment