Re: hash
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
---910079544-787365260-1238005541=:5144
Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-15; FORMAT=flowed
Content-Transfer-Encoding: 8BIT
Content-ID: <alpine.DEB.1.10.0903251825441.5144@urchin.earth.li>
On Wed, 25 Mar 2009, bob@coolgroups.com wrote:
On Mar 25, 4:49?am, Thomas Pornin <por...@bolet.org> wrote:
According to b...@coolgroups.com <b...@coolgroups.com>:
What are some popular ways of turning a string into a hash value?
String.hashCode() is the way to go
Any ideas where they got the magic number 31 that is being used in the
formula?
Dan Bernstein.
Seriously. Daniel J. Bernstein invented a very popular hash function,
known as djb, which is just like the java string hash, but with a
multiplier of 33 instead of 31. 33 and 31 work similarly, in that
multiplying a binary number by them is like shifting it left five bits and
then either adding or subtracting the original; that gives you five bits
of diffusion, but keeps some influence of the original value over the
bottom bit of the hash (does that make sense?).
I don't know why Sun went with 31 over 33.
Also, on machines which don't have a single-cycle multiply (there were
RISC machines which didn't have a hardware multiply - you had to do shifts
and adds; do they still exist?), it's quick, because it's just a shift and
a subtract.
tom
--
NTK now entirely filled with google links -- NTK
---910079544-787365260-1238005541=:5144--