Javascript 中的位与 64 位整数

bitwise AND in Javascript with a 64 bit integer(Javascript 中的位与 64 位整数)
本文介绍了Javascript 中的位与 64 位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一种在 JavaScript 中对 64 位整数执行按位与的方法.

I am looking for a way of performing a bitwise AND on a 64 bit integer in JavaScript.

JavaScript 会将其所有双精度值转换为带符号的 32 位整数以执行按位运算 (这里有详细信息).

JavaScript will cast all of its double values into signed 32-bit integers to do the bitwise operations (details here).

推荐答案

Javascript 将所有数字表示为 64 位 双精度 IEEE 754 浮点数(参见 ECMAscript 规范,第 8.5 节.)可以精确编码最大为 2^53 的所有正整数.较大的整数会被剪掉它们的最低有效位.这就留下了一个问题,即如何在 Javascript 中表示 64 位整数——本机数字数据类型显然不能精确地表示 64 位 int.

Javascript represents all numbers as 64-bit double precision IEEE 754 floating point numbers (see the ECMAscript spec, section 8.5.) All positive integers up to 2^53 can be encoded precisely. Larger integers get their least significant bits clipped. This leaves the question of how can you even represent a 64-bit integer in Javascript -- the native number data type clearly can't precisely represent a 64-bit int.

以下说明了这一点.尽管 javascript 似乎 能够解析表示 64 位数字的十六进制数字,但底层数字表示不包含 64 位.在浏览器中尝试以下操作:

The following illustrates this. Although javascript appears to be able to parse hexadecimal numbers representing 64-bit numbers, the underlying numeric representation does not hold 64 bits. Try the following in your browser:

<html>
  <head>
    <script language="javascript">
      function showPrecisionLimits() {
        document.getElementById("r50").innerHTML = 0x0004000000000001 - 0x0004000000000000;
        document.getElementById("r51").innerHTML = 0x0008000000000001 - 0x0008000000000000;
        document.getElementById("r52").innerHTML = 0x0010000000000001 - 0x0010000000000000;
        document.getElementById("r53").innerHTML = 0x0020000000000001 - 0x0020000000000000;
        document.getElementById("r54").innerHTML = 0x0040000000000001 - 0x0040000000000000;
      }
    </script>
  </head>
  <body onload="showPrecisionLimits()">
    <p>(2^50+1) - (2^50) = <span id="r50"></span></p>
    <p>(2^51+1) - (2^51) = <span id="r51"></span></p>
    <p>(2^52+1) - (2^52) = <span id="r52"></span></p>
    <p>(2^53+1) - (2^53) = <span id="r53"></span></p>
    <p>(2^54+1) - (2^54) = <span id="r54"></span></p>
  </body>
</html>

在 Firefox、Chrome 和 IE 中,我得到以下内容.如果数字以完整的 64 位格式存储,则所有减法的结果都应该是 1.相反,您可以看到 2^53+1 和 2^53 之间的差异是如何丢失的.

In Firefox, Chrome and IE I'm getting the following. If numbers were stored in their full 64-bit glory, the result should have been 1 for all the substractions. Instead, you can see how the difference between 2^53+1 and 2^53 is lost.

(2^50+1) - (2^50) = 1
(2^51+1) - (2^51) = 1
(2^52+1) - (2^52) = 1
(2^53+1) - (2^53) = 0
(2^54+1) - (2^54) = 0

<小时>

那你能做什么?


So what can you do?

如果您选择将 64 位整数表示为两个 32 位数字,则应用按位与就像应用 2 个按位与一样简单,分别应用于低 32 位和高 32 位字".

If you choose to represent a 64-bit integer as two 32-bit numbers, then applying a bitwise AND is as simple as applying 2 bitwise AND's, to the low and high 32-bit 'words'.

例如:

var a = [ 0x0000ffff, 0xffff0000 ];
var b = [ 0x00ffff00, 0x00ffff00 ];
var c = [ a[0] & b[0], a[1] & b[1] ];

document.body.innerHTML = c[0].toString(16) + ":" + c[1].toString(16);

让你:

ff00:ff0000

这篇关于Javascript 中的位与 64 位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)