为什么哈希函数使用5381? [英] Why in hash function use 5381?
问题描述
我发现很多哈希函数使用5381,例如:
static constmap_hash hash(char * pchData,int iLen)
{
unsigned char cBuf;
constmap_hash ulHashId;
ulHashId = 5381;
while(iLen> 0)
{
cBuf = * pchData ++ - ''A'';
if(cBuf< =''Z'' - ''A'')
{
cBuf + =''a'' - ''A'';
}
ulHashId =(( ulHashId<<<< 5)+ ulHashId)^ cBuf;
- iLen;
}
返回ulHashId;
}
谁能告诉我为什么要用5381?
谢谢!!
< blockquote> hr******@sina.com (anguo)写道:
我发现在很多哈希函数中使用5381,例如:
[...]
谁能告诉我为什么要使用5381?
这个网页暗示它是Dan选择的随机数
Bernstein:< http://www.cs.yorku.ca/~oz/hash.html>。大概是
其他人只使用相同的号码。
-
int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz 。$ \\
\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\
); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \\ \\
);}返回0;}
Ben Pfaff写道:hr ****** @ sina.com (anguo)写道:我发现很多哈希函数使用5381,例如:[...]任何人都可以告诉我为什么使用5381?
这个网页暗示它是一个随机数被选中作者:Dan
Bernstein:< http://www.cs.yorku.ca/~oz/hash.html>。大概是
其他人都使用了相同的号码。
除了它不是那么随机看:5381 = 012405。看起来好像
喜欢更深层次的东西。然而,001/010/100/000/101提出了关于最后八位数选择的问题。
-
Martin Ambuhl
Ben Pfaff写道:
-
int main( void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\
\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\
); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \
);}返回0;}
huh只是另一个C黑客 :-)
Ben.c :(在函数main中)
Ben.c:9:18:变量i初始化为任意无符号整数类型,
期望int:sizeof(p)/ 2
Ben.c:10:9:函数strchr影子外部声明
外部声明由本地声明遮蔽。
strchr的规范:
[function(char *,int)返回char *]
Ben.c:11:7:函数putchar阴影外部声明
putchar规范:
[function(int)returns int]
Ben.c:12:10:测试表达式而不是布尔值,键入char:* q
测试表达式类型不是布尔值。
Ben.c:14:12:定义之前使用的变量strchr
使用的rvalue在某些执行时可能无法初始化为值
路径。
Ben.c:17:7:定义之前使用的变量putchar
Ben.c:17:7:返回值(类型int )忽略:putchar(p [i])
不使用函数调用返回的结果。
干杯:)
i find in many hash function use 5381,for exampla:
static constmap_hash hash(char *pchData, int iLen)
{
unsigned char cBuf;
constmap_hash ulHashId;
ulHashId = 5381;
while (iLen > 0)
{
cBuf = *pchData++ - ''A'';
if (cBuf <= ''Z'' - ''A'')
{
cBuf += ''a'' - ''A'';
}
ulHashId = ((ulHashId << 5) + ulHashId) ^ cBuf;
--iLen;
}
return ulHashId;
}
Can anyone tell me why use 5381 ?
thanks!!
hr******@sina.com (anguo) writes:
i find in many hash function use 5381,for exampla:
[...]
Can anyone tell me why use 5381 ?
This webpage implies that it is a random number selected by Dan
Bernstein: <http://www.cs.yorku.ca/~oz/hash.html>. Presumably
everyone else just used the same number.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Ben Pfaff wrote:hr******@sina.com (anguo) writes:i find in many hash function use 5381,for exampla: [...]Can anyone tell me why use 5381 ?
This webpage implies that it is a random number selected by Dan
Bernstein: <http://www.cs.yorku.ca/~oz/hash.html>. Presumably
everyone else just used the same number.
Except that it is not so very random looking: 5381 = 012405. This looks
like something deeper is involved. The 001/010/100/000/101 raises the
question about the choice of the last octal digit, though.
--
Martin Ambuhl
Ben Pfaff wrote:
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
huh "Just another C hacker" :-)
Ben.c: (in function main)
Ben.c:9:18: Variable i initialized to type arbitrary unsigned integral type,
expects int: sizeof(p) / 2
Ben.c:10:9: Function strchr shadows outer declaration
An outer declaration is shadowed by the local declaration.
Specification of strchr:
[function (char *, int) returns char *]
Ben.c:11:7: Function putchar shadows outer declaration
Specification of putchar:
[function (int) returns int]
Ben.c:12:10: Test expression for while not boolean, type char: *q
Test expression type is not boolean.
Ben.c:14:12: Variable strchr used before definition
An rvalue is used that may not be initialized to a value on some execution
path.
Ben.c:17:7: Variable putchar used before definition
Ben.c:17:7: Return value (type int) ignored: putchar(p[i])
Result returned by function call is not used.
Cheers :)
这篇关于为什么哈希函数使用5381?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!