• <legend id='be1Ai'><style id='be1Ai'><dir id='be1Ai'><q id='be1Ai'></q></dir></style></legend>
  • <small id='be1Ai'></small><noframes id='be1Ai'>

    • <bdo id='be1Ai'></bdo><ul id='be1Ai'></ul>

  • <i id='be1Ai'><tr id='be1Ai'><dt id='be1Ai'><q id='be1Ai'><span id='be1Ai'><b id='be1Ai'><form id='be1Ai'><ins id='be1Ai'></ins><ul id='be1Ai'></ul><sub id='be1Ai'></sub></form><legend id='be1Ai'></legend><bdo id='be1Ai'><pre id='be1Ai'><center id='be1Ai'></center></pre></bdo></b><th id='be1Ai'></th></span></q></dt></tr></i><div id='be1Ai'><tfoot id='be1Ai'></tfoot><dl id='be1Ai'><fieldset id='be1Ai'></fieldset></dl></div>

    1. <tfoot id='be1Ai'></tfoot>

        Javascript 中的位与 64 位整数

        时间:2023-09-07
        <i id='R9f2c'><tr id='R9f2c'><dt id='R9f2c'><q id='R9f2c'><span id='R9f2c'><b id='R9f2c'><form id='R9f2c'><ins id='R9f2c'></ins><ul id='R9f2c'></ul><sub id='R9f2c'></sub></form><legend id='R9f2c'></legend><bdo id='R9f2c'><pre id='R9f2c'><center id='R9f2c'></center></pre></bdo></b><th id='R9f2c'></th></span></q></dt></tr></i><div id='R9f2c'><tfoot id='R9f2c'></tfoot><dl id='R9f2c'><fieldset id='R9f2c'></fieldset></dl></div>

      1. <legend id='R9f2c'><style id='R9f2c'><dir id='R9f2c'><q id='R9f2c'></q></dir></style></legend>

          <small id='R9f2c'></small><noframes id='R9f2c'>

              <tbody id='R9f2c'></tbody>

            <tfoot id='R9f2c'></tfoot>
                <bdo id='R9f2c'></bdo><ul id='R9f2c'></ul>
                  本文介绍了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 位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:x|0 如何对 JavaScript 中的数字进行下限? 下一篇:将数字四舍五入到最接近的 3 的倍数

                  相关文章

                  最新文章

                1. <i id='hQlgT'><tr id='hQlgT'><dt id='hQlgT'><q id='hQlgT'><span id='hQlgT'><b id='hQlgT'><form id='hQlgT'><ins id='hQlgT'></ins><ul id='hQlgT'></ul><sub id='hQlgT'></sub></form><legend id='hQlgT'></legend><bdo id='hQlgT'><pre id='hQlgT'><center id='hQlgT'></center></pre></bdo></b><th id='hQlgT'></th></span></q></dt></tr></i><div id='hQlgT'><tfoot id='hQlgT'></tfoot><dl id='hQlgT'><fieldset id='hQlgT'></fieldset></dl></div>

                  <small id='hQlgT'></small><noframes id='hQlgT'>

                  <legend id='hQlgT'><style id='hQlgT'><dir id='hQlgT'><q id='hQlgT'></q></dir></style></legend>
                    <tfoot id='hQlgT'></tfoot>

                      <bdo id='hQlgT'></bdo><ul id='hQlgT'></ul>