Javascript 中的按位 AND 与 64 位整数

编程入门 行业动态 更新时间:2024-10-25 22:28:58
本文介绍了Javascript 中的按位 AND 与 64 位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 位整数.

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 位数字,那么应用按位 AND 就像将 2 个按位 AND 应用到低位和高位 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 中的按位 AND 与 64 位整数

本文发布于:2023-11-30 05:41:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!