为什么要乘以异或在许多GetHash code实现之前的黄金? [英] Why multiply by a prime before xoring in many GetHashCode Implementations?

查看:275
本文介绍了为什么要乘以异或在许多GetHash code实现之前的黄金?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,乘以大量异或应有助于严重分散操作数之前,但为什么要乘数是一个素?

  

相关:
  <一href="http://stackoverflow.com/questions/1145217/why-should-hash-functions-use-a-prime-number-modulus">Why应哈希函数使用一个素数模?

     

关闭,但并不完全是重复的:
  <一href="http://stackoverflow.com/questions/299304/why-does-javas-hash$c$c-in-string-use-31-as-a-multiplier">Why没有Java的&rsquo的;?的哈希code()在字符串中使用31作为乘数

解决方案

有一个的好文章在计算寿命博客,讨论这个话题的细节。它最初被张贴到Java哈希code()的问题,我的问题联系到的响应。根据文章:

  

素数是唯一的编号。它们是独特的,一个主要的与任何其它数目的产品具有可独特(不作为唯一作为道素本身)由于其优越的可用于在它的事实的最佳机会。此属性用于在哈希函数。

     

给出一个字符串塞缪尔,就可以生成由多重的组成部分数字或字母每一个独特的哈希值与一个素数和增加他们。这就是为什么素数被使用。

     

不过使用的素数是一个古老的技术。这里的关键要明白,只要你能产生足够唯一密钥就可以转移到其他散列技术了。去这里的更多关于这个话题有关哈希没有质数的。

I understand that multiplication by a large number before xoring should help with badly distributed operands but why should the multiplier be a prime?

Related:
Why should hash functions use a prime number modulus?

Close, but not quite a Duplicate:
Why does Java’s hashCode() in String use 31 as a multiplier?

解决方案

There's a good article on the Computing Life blog that discusses this topic in detail. It was originally posted as a response to the Java hashCode() question I linked to in the question. According to the article:

Primes are unique numbers. They are unique in that, the product of a prime with any other number has the best chance of being unique (not as unique as the prime itself of-course) due to the fact that a prime is used to compose it. This property is used in hashing functions.

Given a string "Samuel", you can generate a unique hash by multiply each of the constituent digits or letters with a prime number and adding them up. This is why primes are used.

However using primes is an old technique. The key here to understand that as long as you can generate a sufficiently unique key you can move to other hashing techniques too. Go here for more on this topic about hashes without primes.

这篇关于为什么要乘以异或在许多GetHash code实现之前的黄金?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆