我有以下 Java 代码:
long num = 3482085570325547757;
// Discard all but the highest priority 31 bits
int result = (int) (num >>> 33); // Returns 405368112
我正在尝试在 Javascript 中做同样的事情(使用https://github.com/oracle/graaljs引擎),但是,它没有给我与 Java 代码相同的结果:
const num = 3482085570325547757;
// Discard all but the highest priority 31 bits
const result = num >>> 33; // Returns 1281508608
我认为这可能与 GraalJS 在内部将数字存储为整数和双精度数的方式有关?我也尝试使用下面的方法显式转换为 int ,但它也给了我与 Java 代码不同的结果:
const Long = Java.type('java.lang.Long');
const String = Java.type('java.lang.String');
const BigInteger = Java.type('java.math.BigInteger');
const num = 3482085570325547757;
// Discard all but the highest priority 31 bits
const result = BigInteger.valueOf(num >>> 33).intValue(); // Returns 1281508608