如何将 32 位 int 哈希为 10 位 int?
如何将 32 位无符号整数(0~4294967295)哈希为 10 位无符号整数(0~1023)? 最少的碰撞和速度很重要。如果方便的话,请用 C/C++ 编写示例。
抱歉,我问得不好,这不是我的作业。也许问题背景会有所帮助。我正在编写一个服务器,该服务器必须处理<来自每个客户端的 1024 个连接。每个客户端都有其独立的IP地址,存储为32位无符号整数。这就是问题的由来。
How to hash a 32bit unsigned int(0~4294967295) to 10bit unsigned int(0~1023)?
least collisions and fast are important.Please write samples in C/C++ if convenient.
Sorry, I didn't ask in a good way, this is not my homework. Maybe the question background would be helpful. I'm writing a server, this server must handle < 1024 connections from every single client. every client has its independent IP address, stored as 32bit unsigned int.That's how the question goes from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在某些条件下(例如必须对最多 2^10 个已知项进行哈希处理),不会发生冲突。为了获得更快的速度,容忍一点碰撞可能会有所帮助。更多相关信息请点击这里
http://en.wikipedia.org/wiki/Perfect_hash_function
GNU 完美哈希函数生成器 http://www.gnu.org/software/gperf/
廉价、小尺寸的密钥散列是皮尔逊哈希http://en.wikipedia.org/wiki/Pearson_hashing
Under certain conditions (like having to hash at most 2^10 known items), you can have no collisions. For more speed, tolerating a bit of collision might help. More about that here
http://en.wikipedia.org/wiki/Perfect_hash_function
GNU Perfect Hash Function Generator http://www.gnu.org/software/gperf/
A cheap, low-size key hashing is Pearson Hashing http://en.wikipedia.org/wiki/Pearson_hashing