as we all know, Java doesn't have unsigned types. I have to convert a snippet in C# (uses uint) into Java. My code here:
private const int ROLLING_WINDOW = 7;
private const int HASH_PRIME = 0x01000193;
private unit h1, h2, h3, n;
private byte[] window;
//...
private uint roll_hash(byte c)
{
h2 -= h1;
h2 += (uint)ROLLING_WINDOW * c;
h1 += c;
h1 -= window[n % ROLLING_WINDOW];
window[n % ROLLING_WINDOW] = c;
n++;
h3 = (h3 << 5);
h3 ^= c;
return h1 + h2 + h3;
}
private static uint sum_hash(byte c, uint h)
{
h *= HASH_PRIME;
h ^= c;
return h;
}
In Java I use long instead of uint but the result sometimes gives negative values. Solution is using unsigned operators.
Some searching show me about 0xFFFFFFFFL but it is quite complicate while deadline is coming. Wish anybody help me in this problem.
Thanks
The code is almost exactly the same. Only the % operation is different.
window[(int)((n & 0xFFFFFFFFL) % ROLLING_WINDOW)]
or you could write
window[n]
and
if(++n == ROLLING_WINDOW) n = 0;
In more detail
private int roll_hash(byte c)
{
h2 -= h1; // same
h2 += ROLLING_WINDOW * c; // same, remove (uint)
h1 += c; // same
h1 -= window[n];
window[n] = c;
if(++n == ROLLING_WINDOW) n = 0; // limit n to 0 to 6 so % is not required.
h3 = (h3 << 5); // same
h3 ^= c; // same
return h1 + h2 + h3; // same
}
private static int sum_hash(byte c, int h)
{
h *= HASH_PRIME; // same
h ^= c; // same
return h;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With