djb2 hash algo

This commit is contained in:
Imbus 2025-06-17 21:48:07 +02:00
parent 7b33c567e5
commit e07b9a5eff

22
djb2.c Normal file
View file

@ -0,0 +1,22 @@
#include <stdio.h>
unsigned long djb2(const char *str) {
unsigned long h = 5381;
int c;
while ((c = *str++)) {
h = ((h << 5) + h) + c;
// Essentially equal to:
// h = h * 33 + c;
}
return h;
}
int main(void) {
char *a = "ABC";
printf("Hash: %ld\n", djb2(a));
return 0;
}