按位与在Javascript中有64位整数中有、整数、Javascript

2023-09-07 17:54:09 作者:一脸的美人痣

我要寻找的进行按位和64位整数的JavaScript的方式。

JavaScript的会投它的所有double值到符号的32位整数做位操作(details这里)。

解决方案

Java脚本重新presents所有号码为64位的双击precision IEEE 754浮点数(见的的ECMAScript规范,8.5节。)所有的正整数,达到2 ^ 53可连接codeD precisely。较大的整数让他们至少显著位裁剪。显然本机号码数据类型不能precisely重新present 64位int

下面说明了这一点。虽然JavaScript的出现的,以便能够解析进制数再presenting 64位数字,底层数字再presentation未持有64位。尝试在浏览器中执行以下操作:

 < HTML>
  < HEAD>
    < SCRIPT LANGUAGE =JavaScript的>
      功能显示precisionLimits(){
        的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>
  < /头>
  <身体的onload =显示precisionLimits()>
    &其中p为H.;(2 ^ 50 + 1) - (2 ^ 50)=&其中;跨度的id =R50>&所述; /跨度>&所述; / P>
    &其中p为H.;(2 ^ 51 + 1) - (2 ^ 51)=&其中;跨度的id =R51>&所述; /跨度>&所述; / P>
    &其中p为H.;(2 ^ 52 + 1) - (2 ^ 52)=&其中;跨度的id =R52>&所述; /跨度>&所述; / P>
    &其中p为H.;(2 ^ 53 + 1) - (2 ^ 53)=&其中;跨度的id =R53>&所述; /跨度>&所述; / P>
    &其中p为H.;(2 ^ 54 + 1) - (2 ^ 54)=&其中;跨度的id =R54>&所述; /跨度>&所述; / P>
  < /身体GT;
< / HTML>
 

在Firefox,Chrome和IE浏览器,我发现了以下内容。如果数字被储存在其全部64位的辉煌,其结果应该是1对所有substractions。相反,你可以看到2 ^ 53 + 1和2 ^ 53之间的差异将会丢失。

 (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
 

所以,你该怎么办?

javascript事件循环机制你知道吗

如果您选择重新present 64位整数作为两个32位的数字,然后应用按位与很简单,只要将2位与的,低,高的32位字。

例如:

  VAR一个= [0x0000ffff,为0xffff0000]。
变种B = [0x00ffff00,0x00ffff00]。
变种C =〔一个[0]&安培; B [0],A [1]&安培; B〔1]];

document.body.innerHTML = C [0]的ToString(16)+:+ C [1]的ToString(16);
 

让你:

  FF00:FF0000
 

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

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

解决方案

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 javascipt -- the native number data type clearly can't precisely represent a 64-bit int.

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>

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?

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'.

For example:

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);

gets you:

ff00:ff0000

 
精彩推荐
图片推荐