因此,我为此算法制作了一个哈希码函数:对于每个字符,将当前位旋转了三个位,将每个字符的值相加,然后将结果与当前值进行异或运算。这是我到目前为止的代码:
unsigned int hash_function(const char *k){
unsigned int current = 0;
unsigned int rot = 0;
int i = 0;
int r = 0;
for(i = 0; i < strlen(k); i++){
for(r = 0; r < 3; r++){
rot = ((rot & 1 (1 << 31)) >> 31 | (rot << 1);
}
rot += k[i];
current ^= rot;
rot = current;
}
return current;
}
该算法应给出“ gimme” = 477003,“ shelter” = 41540041的一些示例。但是,该算法没有给我正确的结果。我相当确定我使用的是正确的旋转操作,然后按照原样执行算法。我想知道是否有人可以指出我正确的方向。谢谢,希望我能正确格式化这个问题
我想你打算放rot = ((rot & (1 << 31)) >> 31) | (rot << 1);
。但是循环是不必要的,请rot = ((rot & (7 << 29)) >> 29) | (rot << 3);
改用。
这应该工作:
unsigned int hash_function(const char *k){
unsigned int current = 0;
unsigned int rot = 0;
int i = 0;
for (i = 0; i < strlen(k); i++){
rot = ((rot & (7 << 29)) >> 29) | (rot << 3);
rot += k[i];
current ^= rot;
rot = current;
}
return current;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句