gpt4 book ai didi

javascript - 如何将 64 位整数拆分为两个 32 位整数

转载 作者:数据小太阳 更新时间:2023-10-29 04:10:46 35 4
gpt4 key购买 nike

我想将一个 64 位整数拆分为两个 32 位整数:

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 都不起作用...有人可以告诉我如何使用按位运算符吗?

问候

最佳答案

编辑 JavaScript 表示整数 using IEEE double precision format ,所以没有办法在不损失精度的情况下存储任意 64 位整数,除非通过自定义大整数库。对潜在裁剪值的按位运算显然没有意义。


一般来说,对于支持 64 位整数的语言:

1 的 64 位模式是 0xffffffffffffffff。要提取高 32 位,需要移动 32 位:>> 32。要提取低 32 位,只需将它们与 32 个一起:& 0xffffffff

您的原则是正确的 - 您关于要移位或屏蔽多少位的算法是错误的。

关于javascript - 如何将 64 位整数拆分为两个 32 位整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14731743/

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