gpt4 book ai didi

JavaScript 的异或结果与 Java 的结果不同

转载 作者:搜寻专家 更新时间:2023-11-01 03:20:40 25 4
gpt4 key购买 nike

解决方案

我的内部转换逻辑有一个错误。

原始问题

我需要用Java和JavaScript实现一个算法,Java实现和计算结果是引用。但是,当对“负”值调用 XOR 运算符时(我知道 Java 和 JavaScript 使用 2 的补码)会导致 Java 的结果为正,而 JavaScript 结果为负,如下面的输出所示:

Java output:
hash A: 16777619
hash B: 637696617
hash A: 637696613
hash B: 988196095
hash A: 988196062
hash B: -1759370886
hash A: 1759370917 <-- here the JavaScript implementation behaves different
hash B: -1169850945

JavaScript output:

hash A: 16777619
hash B: 637696617
hash A: 637696613
hash B: 988196095
hash A: 988196062
hash B: -1759370886
hash A: -1759370843 <-- this result should be equal to the Java result
hash B: -1883572545

下面是Java源码:

private static final int FNV_PRIME = 0x1000193;
private static final int FNV_COMPRESS = 0xFFFF;
...

public long getHash(int inputNumber)
{
int hash = FNVCalculator.FNV_PRIME;
ByteBuffer intToByteArrayConverter = ByteBuffer.allocate(4);
intToByteArrayConverter.putInt(inputNumber);
byte[] inputValues = intToByteArrayConverter.array();

// inputValues.length is always equal to 4
for (byte processCounter = (byte) 0; processCounter < inputValues.length; processCounter++)
{
hash ^= inputValues[processCounter];
System.out.println("hash A: " + hash);
hash *= FNV_PRIME;
System.out.println("hash B: " + hash);
}
return (hash & FNVCalculator.FNV_COMPRESS);
}

以下片段显示了 JavaScript 代码:

var Constants =
{
FNV_PRIME: parseInt("1000193", 16),
FNV_COMPRESS: parseInt("FFFF", 16),
BYTE_ARRAY_LENGTH: 4,
...
};
Object.freeze(Constants);

var hash = Constants.FNV_PRIME;

for (var counter = 0; counter < Constants.BYTE_ARRAY_LENGTH; counter++)
{
hash ^= inputNumberArray[counter];
console.log("hash A: " + hash);
// mutltiply the hash with the 32 bit FNV prime number: 2^24 + 2^8 + 0x93
// source: https://github.com/wiedi/node-fnv/blob/master/fnv.js
hash += ((hash << 24) + (hash << 8) + (hash << 7) + (hash << 4) + (hash << 1));
hash |= 0;
console.log("hash B: " + hash);
}

return (hash & Constants.FNV_COMPRESS);

带有数字的数组在 Java 和 JavaScript 版本中是相等的,如下所示(所有数字均为十进制数):

Java version:
inputValues[0]: 0
inputValues[1]: 12
inputValues[2]: 33
inputValues[3]: -33
JavaScript version:
inputNumberArray[0]: 0
inputNumberArray[1]: 12
inputNumberArray[2]: 33
inputNumberArray[3]: -33

我已经尝试用整数数组替换字节数组,但没有帮助。我正在使用 WebKit 的 JavaScriptCore 引擎。

最佳答案

看到这些值,我怀疑当您将 223 转换为一系列字节时,Java 是对 223 进行符号扩展,而 Javascript 不是。 223 = 0xDF = 0xFFFFFFDF 符号扩展时....

在 Java 和 JavaScript 之间移植时要注意的事项。

移位运算符在 Javascript 中仅对 32 位值进行运算。JavaScript 在内部将所有 数字表示为 64 位 float ,而不像 Java 那样区分 float 和整数。更重要的是,JavaScript 没有 int 或 float 大小,例如没有 byte、int 或 long 类型。

由于上述陈述和不同语言表示数字的方式不同,始终存在脱钩的风险。

关于JavaScript 的异或结果与 Java 的结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31411709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com