我要一个64位的整数分成两个32位的整数:
I want to split a 64bit integer into two 32bit integers:
var bigInt = 0xffffff; var highInt = bigInt >> 8 // get the high bits 0xfff var lowInt = bigInt // cut of the first part (with &)? console.log(highInt); // 0xfff console.log(lowInt); // 0xfff // set them together again var reBigInt = (highInt << 8) + lowInt;不幸的是没有得到highInt也得到lowInt工程...可能有人给我答案,我需要怎么使用位运算符?
Unfortunately neither getting the highInt nor getting the lowInt works... Could somebody give me the answer how I need to use the bitwise operators?
关于
推荐答案修改 JavaScript的重新presents整数using IEEE双precision格式,所以没有办法,存储任意64位整数而不precision的损失除了通过定制的大整数库。对潜在的裁剪值位运算显然毫无意义。
EDIT JavaScript represents integers using IEEE double precision format, so there is no way to store arbitrary 64 bit integers without loss of precision, except through custom big integer libraries. Bitwise operations on potentially cropped values obviously make no sense.
在一般情况下,对于支持64位整数的语言:
In general, for languages that do support 64 bit integers:
的人的64位模式为 0xffffffffffffffff 。要提取的高32位,则需要32移:&GT;&GT; 32 。要使用32个1提取低32位,公正,它们:&安培;为0xffffffff 。
A 64-bit pattern of ones is 0xffffffffffffffff. To extract the upper 32 bits, you need to shift by 32: >> 32. To extract the lower 32 bit, just and them with 32 ones: & 0xffffffff.
您得到了正确的原则 - 你有多少位转移或掩盖是错误算法。
You got the principle right - your arithmetic on how many bits to shift or mask is just wrong.
更多推荐
如何64bit的整数拆分两个32位整数
发布评论