I have the following method written:
public static int hash2(String key, int tableSize) {
int hashVal = 0;
for(int i=0; i<key.length();i++) {
hashVal = 37 * hashVal + key.charAt(i);
}
System.out.println(hashVal);
hashVal %= tableSize;
if(hashVal < 0){
hashVal += tableSize;
}
return hashVal;
}
I have been tasked with rewriting the for loop without using any multiplication or division. My only tools are addition and shifting of 16-bit binary numbers.
I realize that I need to somehow multiply hashVal by 37, and then add key.charAt(i) to this value. I have tried that multiple ways:
for(int i=0; i<key.length();i++) {
hashVal2 = hashVal2<<19 - hashVal2;
hashVal2 += key.charAt(i);
}
or
for(int i=0; i<key.length();i++) {
hashVal2 = hashVal2<<18 + hashVal2;
hashVal2 += key.charAt(i);
}
or
for(int i=0; i<key.length();i++) {
for(int j=0; j<37;j++) {
hashVal2 += hashVal2;
}
hashVal2 += key.charAt(i);
}
But none of these end up returning the same values for hashVal (or hashVal2) as the original method. Am I misunderstanding bit shifting, or is something with the loop the culprit? Not sure what else to try.
Multiplying by 37 is the same as adding certain powers of 2:
This tells you how to shift, since:
Finally, x * 2i == (x << i) for all i. So to multiply x by 37, you can compute
The rest of the exercise should be fairly straightforward.