doucang2871 2014-07-22 15:28
浏览 186
已采纳

Go:此哈希函数的范围是0到32位吗?

I'm trying to write my own hash function that uses a 30-bit hash.

Here is some code for a FNVa 32-bit hash.

func fnva32(data string) uint32 {
    var hash uint32 = 2166136261
    for _, c := range data {
        hash ^= uint32(c)
        hash *= 16777619 
    }
    return hash
}

Now here is my code that converts lowercase letters a-z into a 30-bit hash:

func id(s string) uint {
    var id uint
    var power uint = 1
    for _, c := range s {
        id+=(uint(c)-96)*power
        power*=26
    }
    return id%1073741824
}

That specifically limits my hash function to a maximum of 30-bit because I'm using a modulus against that number. But how is that FNVa32 hash limited to 32-bits? They are not using a modulus. How does it not generate a number larger than that?

Also you probably notice that I'm not using prime numbers. I tried some prime numbers but it increased the collisions. Currently I'm getting 291 collisions and FNVa32 is getting 76 collisions, from hashing 600,000 (real) words.

My question is... what is making FNVa32 limit to 32-bit, and how would I change it to be 30-bit instead?

  • 写回答

1条回答 默认 最新

  • duanbo19834 2014-07-22 15:32
    关注

    The return type of the fnva32 function is uint32 so there is no way it could return an answer with more bits. Also, the calculation uses a uint32 variable internally.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100