Skip to content

Instantly share code, notes, and snippets.

@trvswgnr
Last active July 24, 2024 04:00
Show Gist options
  • Save trvswgnr/8992941875549f8b67bc133f1f766950 to your computer and use it in GitHub Desktop.
Save trvswgnr/8992941875549f8b67bc133f1f766950 to your computer and use it in GitHub Desktop.
various hash algorithms in c
#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;
}
#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