java.lang.Double.longBitsToDouble function in JavaScript
<p>I have a BinaryStream class in JavasScipt that reads bytes from an array downloaded via XMLHHttpRequest and has the function next() which returns an unsigned byte (technically an integer). I need to read a double from the stream which is basically the same as <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/io/DataInput.html#readDouble%28%29" rel="nofollow">DataStream.readDouble()</a> from Java which uses the method <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Double.html#longBitsToDouble%28long%29" rel="nofollow">Double.longBitsToDouble(long)</a>. I can't figure out how the longBitsToDouble method works.</p>

<p>This is the the code I have:</p>

<pre><code>var bits = stream.nextLong();
if (bits == 0x7ff0000000000000)
    this.variables = [Infinity];
else if (bits == 0xfff0000000000000)
    this.variables = [-Infinity];
else if (bits &gt;= 0x7ff0000000000001 &amp;&amp; bits &lt;= 0x7fffffffffffffff || bits &gt;= 0xfff0000000000001 &amp;&amp; bits &lt;= 0xfff0000000000001)
this.variables = [NaN];
else
{
    var s = ((bits &gt;&gt; 63) == 0) ? 1 : -1;
    var e = ((bits &gt;&gt; 52) &amp; 0x7ff);
    this.variables = [(e == 0) ? (bits &amp; 0xfffffffffffff) &lt;&lt; 1 : (bits &amp; 0xfffffffffffff) | 0x10000000000000];
    // This must be incorrect because it returns a number many times higher than it should
}
console.log(this.variables[0]);
</code></pre>
