c# - Unsigned operator in Java -
as know, java doesn't have unsigned types. have convert snippet in c# (uses uint) java. 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 use long
instead of uint
result gives negative values. solution using unsigned operators. searching show me 0xffffffffl quite complicate while deadline coming. wish me in problem. thanks
the code same. % operation different.
window[(int)((n & 0xffffffffl) % rolling_window)]
or 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 0 6 % 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; }
Comments
Post a Comment