# Java Quick Guide

This guide contains useful Java information.

## Java double type.

Information
A Java double number (8 bytes) adheres to the IEEE-754 1985 floating point standard.
A floating point has 4 parts:

or

Show Java double binary representation A.

where:
• signbit (bitnumber 63, total 1 bit)
The sign is 1 (positive number) or -1 (negative number).
The sign can also be written as sign=(-1)signbit
The signbit is 0 (positive number) and 1 (negative number)

For a Java float or double the radix is always 2

• exponent (bitnumber 52-62, total 11 bits)

In this tutorial this binary representation is called the exponent field and its unsigned value exponent_field_val.

The exponent indicates the positive or negative power of the radix that the mantissa and sign should be multiplied by.
The exponent field contains the binary numbers which represents only positive numbers (= unsigned numbers).
However the exponent needs to represent both positive and negative numbers (= signed numbers). To get signed numbers you must subtract the exponent_field_val by an expbias value.
The expbias = (2n-1)-1 = 1023 (n is the number of bits used for the exponent field, which is 11)
Thus: exponent = exponent_field_val - 1023

The minimum and maximum allowed exponent values are:
exponentmin = -1022
exponentmax = 1023

IEEE reserves exponent field values of all 0s (exponentmin - 1 = -1023) and all 1s (exponentmax + 1 = 1024) to denote special values in the floating-point scheme. This will be discussed in Table A.

Example 1a:

The exponent_field_val represent unsigned value 3
exponent = exponent_field_val - 1023 = 3-1023 = -1020

Example 1b:

The exponent_field_val represent unsigned value 1792
exponent = exponent_field_val - 1023 = 1792-1023 = 769

• mantissa (bitnumber 0-51, total 52 bits)
The mantissa is composed of the fraction (1.02267) and an implicit leading digit (1.02267).

Show Java double binary representation B.

The fraction is represented as a binary decimal.
In a binary decimal the highest order bit (bitnumber 51) corresponds to a value of 2-1 = 0.5
In a binary decimal the lowest order bit (bitnumber 0) corresponds to a value of 2-52 = 2.22044....e-16

As displayed above the mantissa of a double has only 52 bits, however there is one hidden bit (bitnumber 52). This hidden bit determines what the "implicit leading digit" will be as mentioned earlier. This hidden bit is predictable and is therefore not included, because the exponent value determines what the hidden bit value will be.

If the exponent is all zeros,

the hidden bit (bitnumber 52) = 0 in all other cases the hidden bit = 1.

Example 2a:
The exponent is:

Because the exponent is all zeros, the hidden bit is 0.

Show Java double binary representation C.

mantissa = hidden bit value + fraction
mantissa = 0 + 2-1 + 2-2 + 2-3 = 0 + 0.875 = 0.875

Example 2b:
The exponent is:

Because the exponent in not all zeros, the hidden bit is 1.

Show Java double binary representation D.

mantissa = hidden bit value + fraction
mantissa = 1 + 2-3 + 2-4 + 2-6 + 2-9 = 1 + 0.205078125 = 1.205078125

Note:
A floating point number is normalized if the mantissa is within the range 1/radix <= mantissa <1, else it is called denormalized.
As mentioned earlier if the exponent field contains all 0s or 1s it denote special values in the floating-point scheme.

Table A: Special values
 exponent field exponent fraction represents 00000000000bin exponentmin-1 fraction==0 ±0 00000000000bin exponentmin-1 fraction != 0 (-1)signbit * 0.fraction * 2exponentmin 11111111111bin exponentmax+1 fraction == 0 ±infinity 11111111111bin exponentmax+1 fraction != 0 Not a Number not all 0s or 1s exponentmin<=exponent<=exponentmax any (-1)signbit * 1.fraction * 2exponent

The sign, mantissa, radix and exponent are now explained. You should now be able to calculate the double value, given the binary representation.

Example 3:

Show Java double binary representation E.

signbit = 0
exponent_field_val = 00010001001bin = 137dec
exponent = exponent - 1023 = 137 - 1023 = -886
mantissa = hidden bit value + fraction
mantissa = 1 + 2-3 + 2-4 + 2-6 + 2-9 = 1.205078125
double = (-1)signbit * mantissa * radixexponent
double = (-1)0 * 1.205078125 * 2-886 = 2.3358182363676799893446007541396e-267

Example 4:

Show Java double binary representation F.

signbit = 1
exponent_field_val = 00000001001bin = 9dec
exponent = exponent_field_val - 1023 = 9 - 1023 = -1014
mantissa = hidden bit value + fraction
mantissa = 1 + 2-1 + 2-52 = 1.5000000000000002220446049250313
double = (-1)signbit * mantissa * radixexponent
double = (-1)1 * 1.5000000000000002220446049250313 * 2-1014 = -8.5442836166676545758745469881476e-306

Example 5:

Show Java double binary representation G.

signbit = 1
exponent_field_val = 00000000000bin (special case)
exponent = exponentmin = -1022
mantissa = hidden bit value + fraction
mantissa = 0 + 2-1 + 2-2 = 0.75
double = (-1)signbit * mantissa * radixexponent
double = (-1)1 * 0.75 * 2-1022 = -1.6688053938804010373176745379993e-308

Example 6:

Show Java double binary representation H.

signbit = 1
exponent_field_val = 00000000000bin (special case)
fraction = 0
double = -0

Example 7:

Show Java double binary representation I.

signbit = 0
exponent_field_val = 11111111111bin (special case)
fraction = 0
double = +infinity

Example 8:

Show Java double binary representation J.

signbit = 1
exponent_field_val = 11111111111bin (special case)
fraction != 0
double = NaN

Example 9 (largest positive double, Double.MAX_VALUE):

Show Java double binary representation K.

signbit = 0
exponent_field_val = 11111111110bin = 2046dec
exponent = exponent - 1023 = 2046 - 1023 = 1023
mantissa = hidden bit value + fraction
mantissa = 1 + 2-1 + 2-2 + ... +2-52 = 2-2-52 = 1.9999999999999997779553950749687
double = (-1)signbit * mantissa * radixexponent
double = (-1)0 * (2-2-52) * 21023 = 1.79769313486231570e+308

Example 10 (lowest positive double, Double.MIN_VALUE):

Show Java double binary representation L.

signbit = 0
exponent_field_val = 00000000000bin (special case)
exponent = exponentmin = -1022
mantissa = hidden bit value + fraction
mantissa = 0 + 2-52 = 2-52
double = (-1)signbit * mantissa * radixexponent
double = (-1)0 * 2-52 * 2-1022 = 2-1074 = 4.94065645841246544e-324