long unsigned 的位移位操作 - 关闭一个问题
Bitshift operation with long unsigned - Off by one issue
我在 c# 中有一个简单的位移位操作,我正在移植到 JS。你可以在这里看到它:https://dotnetfiddle.net/Au62NB
//C# code
ulong v = 2630423132685782527;
UInt32 v1 = (UInt32)(v >> 32);
UInt32 v0 = (UInt32)((v << 32) >> 32);
Console.WriteLine("v1:v0 " + v1 +" : " + v0); //v1:v0 612443111 : 280284671
在 NodeJS 中,值 v0 总是减 1 (280284672
)。我已经尝试了所有我能在 google、BigInt、npm 的大整数库等上找到的东西。有人有想法吗?
let v = BigInt('2630423132685782527');
let v1 = v >> BigInt(32)
let v0 = (v << BigInt(32)) >> BigInt(32);
v0 = Number(v0) >>> 0; //not even sure why I have to do this, something with converting signed/unsigned.
console.log("v1:v0", Number(v1), Number(v0)); //"v1:v0", 612443111, 280284672
JavaScript 的 Number
对大于 253 - 1 的数字没有概念(2 ** 53 - 1 = 9007199254740991
又名 Number.MAX_SAFE_INTEGER
).
要用任意 更大的值表示 有符号整数 使用 BigInt (在 10.4 之前的 Safari 和 IE 和 Node 中不可用)
还值得一读:BigInt/asUintN
The BigInt.asUintN static method is used to wrap a BigInt value to an unsigned integer between 0 and 2width-1.
const v = 2630423132685782527n;
const v1 = v >> 32n;
const v0 = (v << 32n) >> 32n;
const UintN_v0 = BigInt.asUintN(32, v0);
console.log(`v1:${v1} v0:${UintN_v0}`); // v1:612443111 v0:280284671
我在 c# 中有一个简单的位移位操作,我正在移植到 JS。你可以在这里看到它:https://dotnetfiddle.net/Au62NB
//C# code
ulong v = 2630423132685782527;
UInt32 v1 = (UInt32)(v >> 32);
UInt32 v0 = (UInt32)((v << 32) >> 32);
Console.WriteLine("v1:v0 " + v1 +" : " + v0); //v1:v0 612443111 : 280284671
在 NodeJS 中,值 v0 总是减 1 (280284672
)。我已经尝试了所有我能在 google、BigInt、npm 的大整数库等上找到的东西。有人有想法吗?
let v = BigInt('2630423132685782527');
let v1 = v >> BigInt(32)
let v0 = (v << BigInt(32)) >> BigInt(32);
v0 = Number(v0) >>> 0; //not even sure why I have to do this, something with converting signed/unsigned.
console.log("v1:v0", Number(v1), Number(v0)); //"v1:v0", 612443111, 280284672
JavaScript 的 Number
对大于 253 - 1 的数字没有概念(2 ** 53 - 1 = 9007199254740991
又名 Number.MAX_SAFE_INTEGER
).
要用任意 更大的值表示 有符号整数 使用 BigInt (在 10.4 之前的 Safari 和 IE 和 Node 中不可用)
还值得一读:BigInt/asUintN
The BigInt.asUintN static method is used to wrap a BigInt value to an unsigned integer between 0 and 2width-1.
const v = 2630423132685782527n;
const v1 = v >> 32n;
const v0 = (v << 32n) >> 32n;
const UintN_v0 = BigInt.asUintN(32, v0);
console.log(`v1:${v1} v0:${UintN_v0}`); // v1:612443111 v0:280284671