The Java Program: TestDouble.java
1 // TestDouble.java -- illustrate 64 bit IEEE 754 floating point
2
3 class TestDouble {
4
5 // convert 64 bits to char array of '0' or '1'
6 public static char [] toChars (long l) {
7 char [] s = new char [64];
8 for (int i=0; i<64; i++) {
9 s[64-i-1] = (l%2==1) ? '1' : '0';
10 l >>>= 1; // shift right logical
11 }
12 return (s);
13 }
14
15 // print representation of IEEE 754 double precision number
16 public static void printBits (double d) {
17 char [] bits = toChars (Double.doubleToLongBits (d));
18 System.out.println ("d = " + d);
19 System.out.println ("sign bit = " + new String(bits, 0, 1));
20 System.out.println ("exponent (11 bits) = " + new String(bits, 1,11));
21 System.out.println ("mantissa (52 bits) = " + new String(bits,12,52));
22 System.out.println ();
23 }
24
25 public static void main (String args[]) {
26 printBits (Double.NaN);
27 printBits (Double.POSITIVE_INFINITY);
28 printBits (Double.NEGATIVE_INFINITY);
29 printBits (0.0D);
30 printBits (-0.0D);
31 printBits (1.0D);
32 printBits (-1.0D);
33 printBits (Double.MAX_VALUE); // Largest value (positive)
34 printBits (-Double.MAX_VALUE); // Smallest value (negative)
35 printBits (Double.MIN_VALUE); // Smallest postitive value
36 printBits (-Double.MIN_VALUE); // Largest negative value
37 }
38