众所周知,Java 没有无符号类型。我必须将 C# 中的一个片段(使用 uint)转换为 Java。我的代码在这里:
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;
}
在 Java 中,我使用long
而不是,uint
但结果有时会给出负值。解决方案是使用无符号运算符。一些搜索向我展示了关于 0xFFFFFFFFL 的信息,但在截止日期即将到来时它非常复杂。希望有人帮助我解决这个问题。谢谢