djb2 hash algo
This commit is contained in:
parent
7b33c567e5
commit
e07b9a5eff
1 changed files with 22 additions and 0 deletions
22
djb2.c
Normal file
22
djb2.c
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue