【发布时间】:2021-10-22 23:12:35
【问题描述】:
unsigned long hash(char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash % NUM_BUCKETS;
}
使用此代码,当您在函数中输入 20 个字母(例如 zzzzzzzzzzzzzzzzzzzzzzzzzzzz)时,您会得到一个巨大数字的输出。如果限制为仅 32 位,long 如何保存数字?
【问题讨论】:
-
you get an output of a huge number您如何准确地检查输出的大小?printf(what here?)? -
这能回答你的问题吗? djb2 Hash Function