我目前正在尝试实现一个哈希表/trie,但是当我将参数传递给 murmurhash2 时,它会返回一个数字,但我得到 unsigned int 溢出的运行时错误:
test.c:53:12:运行时错误:无符号整数溢出:24930 * 1540483477 不能用“无符号整数”类型表示
test.c:60:4: 运行时错误:无符号整数溢出:2950274797 * 1540483477 不能用“无符号整数”类型表示 6265
我在第 53 行和第 60 行放了一堆星星(*)
我不确定我是否传递了一些错误的参数。任何帮助将不胜感激!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed );
int main(void)
{
const char* s= "aa";
unsigned int number= MurmurHash2 (s, (int)strlen(s), 1) % 10000;
printf("%u\n", number);
}
unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed )
{
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
const unsigned int m = 0x5bd1e995;
const int r = 24;
// Initialize the hash to a 'random' value
unsigned int h = seed ^ len;
// Mix 4 bytes at a time into the hash
const unsigned char * data = (const unsigned char *)key;
while(len >= 4)
{
unsigned int k = *(unsigned int *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
// Handle the last few bytes of the input array
switch(len)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m; ************************************************
};
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m; **************************************
h ^= h >> 15;
return h;
}