Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Javascript represents all numbers as 64-bit <a href="http://en.wikipedia.org/wiki/IEEE_754-1985#Double-precision_64_bit" rel="noreferrer">double precision IEEE 754 floating point numbers</a> (see the <a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf" rel="noreferrer">ECMAscript spec</a>, 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.</p> <p>The following illustrates this. Although javascript <em>appears</em> 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:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script language="javascript"&gt; 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; } &lt;/script&gt; &lt;/head&gt; &lt;body onload="showPrecisionLimits()"&gt; &lt;p&gt;(2^50+1) - (2^50) = &lt;span id="r50"&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;(2^51+1) - (2^51) = &lt;span id="r51"&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;(2^52+1) - (2^52) = &lt;span id="r52"&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;(2^53+1) - (2^53) = &lt;span id="r53"&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;(2^54+1) - (2^54) = &lt;span id="r54"&gt;&lt;/span&gt;&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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.</p> <pre><code>(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 </code></pre> <hr> <p>So what can you do?</p> <p>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'.</p> <p>For example:</p> <pre><code>var a = [ 0x0000ffff, 0xffff0000 ]; var b = [ 0x00ffff00, 0x00ffff00 ]; var c = [ a[0] &amp; b[0], a[1] &amp; b[1] ]; document.body.innerHTML = c[0].toString(16) + ":" + c[1].toString(16); </code></pre> <p>gets you:</p> <pre><code>ff00:ff0000 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload