I was wondering how to calculate the hash code for hand-written strings. I understand that in Java, you can do something like this:
string m = "what do you say that you say?"; Whatever long = me.hashCode ();
It's all good and dandy, but I was wondering how to do it with hand. I know that the given formula for calculating the hash code of the string is something like this:
S0x31 ^ (n-1) + S1x31 ^ (n-2) + .... S (N-2) x 31 + s (n -1)
Where S is the letter in the string indicates, and n is the length of the string. Using 16 bit Unicode, I will be computed as the character before the string:
87 X (31 ^ 34)
However If I can not imagine adding all the characters in such a large number, then, to compute the lowest order 32 bit result, what will I do? Which is equal to the tallest-957986661 and how am I not counting it? Take a look at
source code of java.lang.String
.
/ ** * Returns a hash code for this string. * * & Lt; Code & gt; String & lt; / Code & gt; For the hash code object, please use the * & lt; Block & gt; & Lt; Pre & gt; * S [0] * 31 ^ (n-1) + s [1] * 31 ^ (N -2) + ... + s [n-1] * < / Pre & gt; & Lt; / Blockquote & gt; * & Lt; Code & gt; Int & lt; / Code & gt; Arithmetic, where & lt; Code & gt; S [i] & lt; / Code & gt; String * and lieutenant; I & gt; I & lt; / I & gt; Th letter, & lt; Code & gt; N & lt; / Code & gt; * Length of the string, and & lt; Code & gt; ^ & Lt; / Code & gt; The loss signal * (The hash value of the empty string is zero.) * * Assume a hash code value for this object. * / Public Ink Hashod () {int h = hash; Int len = count; If (H == 0 & lane> 0) {int off = offset; Four val [] = value; {H = 31 * h + val [off ++] for (int i = 0; i & lt; len; i ++); } Hash = h; } Returns H; }
Comments
Post a Comment