Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于位运算的C语言散列码算法_C_Algorithm_Hash_Bit Manipulation_Bitwise Operators - Fatal编程技术网

基于位运算的C语言散列码算法

基于位运算的C语言散列码算法,c,algorithm,hash,bit-manipulation,bitwise-operators,C,Algorithm,Hash,Bit Manipulation,Bitwise Operators,所以我为这个算法做了一个散列码函数: 对于每个字符,将当前位向左旋转三位 添加每个字符的值, 将结果与当前值异或 以下是我目前掌握的代码: 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++){

所以我为这个算法做了一个散列码函数: 对于每个字符,将当前位向左旋转三位 添加每个字符的值, 将结果与当前值异或 以下是我目前掌握的代码:

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;  
}
unsigned int hash_函数(const char*k){
无符号整数电流=0;
无符号整数rot=0;
int i=0;
int r=0;
对于(i=0;irot=(rot&1(1>31 |)(rot这最初只是一个评论,但事实证明这是你问题的答案

在这一行:

rot = ((rot & 1 (1 << 31)) >> 31 | (rot << 1);
去掉这两件事:

rot = (rot & (1 << 31)) >> 31 | (rot << 1);

我想你的意思是把它放在这里(rot不需要
int r=0
。我对这个答案投了更高的票,因为它解决了让我好奇的问题——OP为什么使用内部循环?非常感谢,我没有意识到你可以不用循环就这样做。这实际上是我做的一个复制粘贴错误,我正在使用一个检测错误的编译器。但无论如何,非常感谢感谢捐款
rot = (rot & (1 << 31)) >> 31 | (rot << 1);
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;  
}