由网友(感情依旧何在)分享简介:谁能告诉我,为什么数5381被用于DJB散列函数?DJB Hash函数是H(0)= 5381 H(ⅰ)= 33 * H(I-1)^海峡[I] 一个C程序:无符号整型DJBHash(字符*海峡,无符号整型LEN){无符号整型哈希= 5381;无符号整型I = 0;对于(i = 0; I< LEN; STR +...![深入理解完美哈希](https://p.xsw88.cn/allimgs/daicuo/20230910/1851.png)
谁能告诉我,为什么数5381被用于DJB散列函数?
DJB Hash函数是
H(0)= 5381
H(ⅰ)= 33 * H(I-1)^海峡[I]
一个C程序:
无符号整型DJBHash(字符*海峡,无符号整型LEN)
{
无符号整型哈希= 5381;
无符号整型I = 0;
对于(i = 0; I< LEN; STR ++,我++)
{
散列=((散列&其中;小于5)+散列)+(* STR);
}
返回哈希;
}
解决方案
5381只是一个数字,在测试中,造成的更少的碰撞和更好的雪崩。你会发现魔术常量几乎在每个哈希算法中。
![深入理解完美哈希](https://p.xsw88.cn/allimgs/daicuo/20230910/1851.png)
Can anyone tell me why the number 5381 is used in DJB hash function ?
DJB Hash function is
h(0) = 5381
h(i) = 33 * h(i-1) ^ str[i]
A c program:
unsigned int DJBHash(char* str, unsigned int len)
{
unsigned int hash = 5381;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) + hash) + (*str);
}
return hash;
}
解决方案
5381 is just a number that, in testing, resulted in fewer collisions and better avalanching. You'll find "magic constants" in just about every hash algo.
相关推荐
最新文章