【发布时间】:2016-09-15 16:58:21
【问题描述】:
我有一个 C 语言项目,我需要为 void 指针创建一个合适的哈希函数,该指针可能包含字母数字字符、整数或只是普通的 ol' 字符。
我需要使用多项式哈希函数,而不是乘以常数,我应该使用部分和的循环移位固定位数。 在这个页面here ,有java代码(我假设这是java,因为使用了字符串):
static int hashCode(String s) {
int h = 0;
for (int i = 0; i < s.length(); i++) {
h = (h << 5) | (h >>> 27); // 5-bit cyclic shift of the running sum
h += (int) s.charAt(i); // add in next character
}
return h;
}
下面的这条线到底在做什么?
h = (h << 5) | (h >>> 27); // 5-bit cyclic shift of the running sum
是的,评论说 5 位循环移位,但 <<、| 和 >>> 操作数在这方面如何工作?我以前从未见过或使用过它们。
【问题讨论】:
-
您进行了哪些研究来尝试了解这些运营商*的工作方式?
-
(h >>> 27)你确定吗? -
@AlterMann 你为什么要问,它似乎完全合法?总而言之,它是 ROTL 5。
-
@MarkoTopolnik,在 C 中无效,这就是我问的原因
-
@AlterMann 直接来自链接页面。