21 lines
341 B
C
21 lines
341 B
C
#include <stdio.h>
|
|
|
|
/*
|
|
* Simple hash function djb2, as in Daniel J. Berenstein.
|
|
*/
|
|
|
|
unsigned long djb2(const char *str) {
|
|
unsigned long h = 5381;
|
|
int c;
|
|
|
|
while ((c = *str++)) h = ((h << 5) + h) + c; // h = h * 33 + c;
|
|
|
|
return h;
|
|
}
|
|
|
|
int main(void) {
|
|
char *a = "ABC";
|
|
printf("Hash: %ld\n", djb2(a));
|
|
|
|
return 0;
|
|
}
|