title: How to work with double bit codes and doubleToLongBits() in Java?
body: <p>I was able to get the binary code of double numbers:</p>
  
  <pre><code>Double d = 1.5E12;
  long l = Double.doubleToLongBits(d);
  String bin = Long.toBinaryString(l);
  System.out.println(bin);
  System.out.println(bin.length());
  </code></pre>
  
  <p>However, the resulting code doesn't behave like a double bitcode, it doesn't make any sense:</p>
  
  <pre><code>1.5E12   --&gt; 100001001110101110100111110111101111001100000000000000000000000 (length: 63)
  -1.5E12  --&gt; 1100001001110101110100111110111101111001100000000000000000000000 (length: 64)
  1.5E-12  --&gt; 11110101111010011000110110011001000001110001001101111100011010 (length: 62)
  -1.5E-12 --&gt; 1011110101111010011000110110011001000001110001001101111100011010 (length: 64)
  </code></pre>
  
  <p>I tried to understand those number by grouping them:</p>
  
  <pre><code>1.5E12   --&gt;     10000100111  0101110100111110111101111001100000000000000000000000
  -1.5E12  --&gt; 1   10000100111  0101110100111110111101111001100000000000000000000000
  1.5E-12  --&gt;   (0)1111010111  1010011000110110011001000001110001001101111100011010
  -1.5E-12 --&gt; 1   01111010111  1010011000110110011001000001110001001101111100011010
  </code></pre>
  
  <p>First of all it adds/removes a bit for the minus signs (that's a bad thing...speaking of parsers). But even more the exponent shows very dubious numbers: E.g. 10000100111 is supposed 1035 (= 1023 + 12) instead of 1063 (1063 - 1023 = 40!), and (0)1111010111 is supposed to be 1011 (= 1023 - 12) instead of 983 (983 - 1023 = -40!).</p>
  
  <p>Does anyone know how to read this double bit code? I.e. how to get the right exponent and the mantisse out of above bit codes?</p>
  
  <p>(An example: How to get the value 1.5E12 back again out of the bit code? 100001001110101110100111110111101111001100000000000000000000000 --?--> 1.5E12)</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Using the mask from the Java API I ended up to get the values like this:</p>
  
  <pre><code>static final long SIGN = 0x8000000000000000L;
  static final long EXPN = 0x7ff0000000000000L;
  static final long SGNF = 0x000fffffffffffffL;
  
  Double d = ...;
  long lng = Double.doubleToLongBits(d);
  String bin = Long.toBinaryString(lng);
  long sign = (lng &amp; SIGN) &gt;&gt;&gt; (bin.length()-1);
  long expn = (lng &amp; EXPN) &gt;&gt;&gt; 52;
  long sgnf = lng &amp; SGNF;
  </code></pre>
  
  <p>And I could easily print it:</p>
  
  <pre><code>System.out.println("sign-bin: "+Long.toBinaryString(sign));
  System.out.println("expn-bin: "+Long.toBinaryString(expn));
  System.out.println("sgnf-bin: "+Long.toBinaryString(sgnf));
  System.out.println("sign-string: "+Long.toString(sign));
  System.out.println("expn-string: "+Long.toString(expn));
  System.out.println("sgnf-string: "+Long.toString(sgnf));
  </code></pre>
  
  <p>With the Double 1.5E12 I got this result:</p>
  
  <pre><code>sign-bin: 0
  expn-bin: 10000100111
  sgnf-bin: 101110100111110111101111001100000000000000000000000
  sign-string: 0
  expn-string: 1063
  sgnf-string: 1640400372629504
  </code></pre>
  
  <p>Do you know how to get the "real" decimal values out of them? (E.g. 1640400372629504 --> 1.5 and 1063 --> E12)</p>