Javascript哈希算法(Javascript hashing algorithm)

我试图学习如何在JavaScript中做一些基本的哈希处理,并且我遇到了以下算法:

var hash = 0; for (i = 0; i < this.length; i++) { char = str.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; }

我真的不明白它是如何工作的,我希望你能帮助我。 特别是我不明白(hash<<5)-hash和hash = hash & hash 。 谢谢您的回复。

注意:对于任何寻找源代码的人来说,这是Java的String.hashCode()的实现: http ://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method

I'm trying to learn how to do some basic hashing in Javascript and I've come across the following algorithm:

var hash = 0; for (i = 0; i < this.length; i++) { char = str.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; }

I don't really understand how it works and I was hoping you could help me out. In particular I don't understand (hash<<5)-hash and hash = hash & hash. Thank you for your replies.

Note: For anyone looking for the source, it's an implementation of Java’s String.hashCode(): http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method

最满意答案

这一步

hash = ((hash << 5) - hash) + char;

是有效的:

hash = ((hash * 32) - hash) + char;

然后,

hash = hash & hash;

只会在数字溢出整数范围(32位或31位)时才会更改该值。 (我不会那样做,但这是一个风格问题。)

在该代码中,应声明变量“i”和“char”:

var hash = 0, i, char;

The step

hash = ((hash << 5) - hash) + char;

is effectively:

hash = ((hash * 32) - hash) + char;

Then,

hash = hash & hash;

will only change the value if the number has overflowed the integer range (32 bits, or maybe 31). (I wouldn't do it that way but it's a matter of style.)

In that code, the variables "i" and "char" should be declared:

var hash = 0, i, char;Javascript哈希算法(Javascript hashing algorithm)

我试图学习如何在JavaScript中做一些基本的哈希处理,并且我遇到了以下算法:

var hash = 0; for (i = 0; i < this.length; i++) { char = str.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; }

我真的不明白它是如何工作的,我希望你能帮助我。 特别是我不明白(hash<<5)-hash和hash = hash & hash 。 谢谢您的回复。

注意:对于任何寻找源代码的人来说,这是Java的String.hashCode()的实现: http ://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method

I'm trying to learn how to do some basic hashing in Javascript and I've come across the following algorithm:

var hash = 0; for (i = 0; i < this.length; i++) { char = str.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; }

I don't really understand how it works and I was hoping you could help me out. In particular I don't understand (hash<<5)-hash and hash = hash & hash. Thank you for your replies.

Note: For anyone looking for the source, it's an implementation of Java’s String.hashCode(): http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method

最满意答案

这一步

hash = ((hash << 5) - hash) + char;

是有效的:

hash = ((hash * 32) - hash) + char;

然后,

hash = hash & hash;

只会在数字溢出整数范围(32位或31位)时才会更改该值。 (我不会那样做,但这是一个风格问题。)

在该代码中,应声明变量“i”和“char”:

var hash = 0, i, char;

The step

hash = ((hash << 5) - hash) + char;

is effectively:

hash = ((hash * 32) - hash) + char;

Then,

hash = hash & hash;

will only change the value if the number has overflowed the integer range (32 bits, or maybe 31). (I wouldn't do it that way but it's a matter of style.)

In that code, the variables "i" and "char" should be declared:

var hash = 0, i, char;