简体   繁体   English

截断 FNV hash

[英]Trucating FNV hash

I'm using 32-bit FNV-1a hashing, but now I want to reserve one of the bits to hold useful information about the input key.我正在使用 32 位 FNV-1a 散列,但现在我想保留其中一个位来保存有关输入键的有用信息。 That is, I want to use only 31 of the 32 bits for hash and 1 bit for something else.也就是说,我只想将 32 位中的 31 位用于 hash,将 1 位用于其他东西。

Assuming FNV is well distributed for my application, is it safe to assume that dropping 1 bit this will increase collision rate by 32/31, as opposed to something dramatic?假设我的应用程序的 FNV 分布良好,是否可以安全地假设丢弃 1 位这将增加 32/31 的碰撞率,而不是戏剧性的事情?

The algo recommends XOR the discarded MSB with the LSB, but for 1-bit, that seems pointless.该算法建议将丢弃的 MSB 与 LSB 进行异或运算,但对于 1 位,这似乎毫无意义。 As such, would it matter which bit is discarded (MSB or LSB)?因此,丢弃哪个位(MSB 或 LSB)是否重要? And if not, would it matter if the LSB MSB were discard after hashing each byte (ie using a even numbered "prime") or after 32-bit hashing the entire byte-array first.如果不是,那么在散列每个字节(即使用偶数“素数”)或首先对整个字节数组进行 32 位散列之后丢弃LSB MSB 是否重要。

Removing a single bit from a 32-bit hash code will have a larger effect than a 32/31 increase in the collision rate.从 32 位 hash 代码中删除单个位将比碰撞率增加 32/31 产生更大的影响。 To see why, note that there are 2 32 possible 32-bit hashes and 2 31 possible 31-bit hashes, meaning that removing a bit from the hash cuts the numbers of possible hashes down by a factor of two - a pretty significant reduction in the number of possible hashes.要了解原因,请注意有 2 32 个可能的 32 位散列和 2 31个可能的 31 位散列,这意味着从 hash 中删除一个位会将可能的散列数量减少两倍 - 显着减少可能的哈希数。 This brings about roughly a doubling of the probability that you see a hash collision across your hashes.这会使您在哈希中看到 hash 冲突的概率大约增加一倍。

If you have a sufficiently small number of hashes that collisions are rare, then cutting out a single bit is unlikely to change much.如果您的哈希值足够少,冲突很少见,那么删除一个位不太可能改变太多。 But if collisions were already an issue, dropping a bit will roughly double the chance you see them.但是,如果碰撞已经是一个问题,那么稍微放下一点将使您看到它们的机会大约增加一倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM