Java Quick Guide

 
 
This guide contains useful Java information.







Java float type.



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

sign * mantissa * radixexponent
or
(-1)signbit * mantissa * radixexponent

31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber


where:
  • signbit (bitnumber 31, 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)

  • radix
    For a Java float or double the radix is always 2

  • exponent (bitnumber 23-30, total 8 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 = 127 (n is the number of bits used for the exponent field, which is 8)
    Thus: exponent = exponent_field_val - 127

    The minimum and maximum allowed exponent values are:
    exponentmin = -126
    exponentmax = 127

    IEEE reserves exponent field values of all 0s (exponentmin - 1 = -127) and all 1s (exponentmax + 1 = 128) 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 - 127 = 3-127 = -124

    Example 1b:

    The exponent_field represent unsigned value 224
    exponent = exponent_field_val - 127 = 224-127 = 97

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

    22
    21
    20
    19
    18
    17
    16
    15
    14
    13
    12
    11
    10
     9
     8
     7
     6
     5
     4
     3
     2
     1
     0
    <bitnumber

    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23


    The fraction is represented as a binary decimal.
    In a binary decimal the highest order bit (bitnumber 22) 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-23 = 0.000000119...

    As displayed above the mantissa of a float has only 23 bits, however there is one hidden bit (bitnumber 23). 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 23) = 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.

    22
    21
    20
    19
    18
    17
    16
    15
    14
    13
    12
    11
    10
     9
     8
     7
     6
     5
     4
     3
     2
     1
     0
    <bitnumber

    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    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.

    22
    21
    20
    19
    18
    17
    16
    15
    14
    13
    12
    11
    10
     9
     8
     7
     6
     5
     4
     3
     2
     1
     0
    <bitnumber

    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    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
00000000bin exponentmin-1 fraction==0 ±0
00000000bin exponentmin-1 fraction != 0 (-1)signbit * 0.fraction * 2exponentmin
11111111bin exponentmax+1 fraction == 0 ±infinity
11111111bin 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 float value, given the binary representation.

Example 3:
Binary representation of a float
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber

  
  
  
  
  
  
  
  
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23

signbit = 0
exponent_field_val = 10001001bin = 137dec
exponent = exponent - 127 = 137 - 127 = 10
mantissa = hidden bit value + fraction
mantissa = 1 + 2-3 + 2-4 + 2-6 + 2-9 = 1.205078125
float = (-1)signbit * mantissa * radixexponent
float = (-1)0 * 1.205078125 * 210 = 1234.0

Example 4:
Binary representation of a float
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber

  
  
  
  
  
  
  
  
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23

signbit = 1
exponent_field_val = 00001001bin = 9dec
exponent = exponent_field_val - 127 = 9 - 127 = -118
mantissa = hidden bit value + fraction
mantissa = 1 + 2-1 + 2-23 = 1.50000011920928955078125
float = (-1)signbit * mantissa * radixexponent
float = (-1)1 * 1.50000011920928955078125 * 2-118 = -4.5138986658899908977531184594082e-36

Example 5:
Binary representation of a float
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber

  
  
  
  
  
  
  
  
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23

signbit = 1
exponent_field_val = 00000000bin (special case)
exponent = exponentmin = -126
mantissa = hidden bit value + fraction
mantissa = 0 + 2-1 + 2-2 = 0.75
float = (-1)signbit * mantissa * radixexponent
float = (-1)1 * 0.75 * 2-126 = -8.8162076311671563097655240291668e-39

Example 6:
Binary representation of a float
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber

  
  
  
  
  
  
  
  
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23

signbit = 1
exponent_field_val = 00000000bin (special case)
fraction = 0
float = -0

Example 7:
Binary representation of a float
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber

  
  
  
  
  
  
  
  
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23

signbit = 0
exponent_field_val = 11111111bin (special case)
fraction = 0
float = +infinity

Example 8:
Binary representation of a float
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber

  
  
  
  
  
  
  
  
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23

signbit = 1
exponent_field_val = 11111111bin (special case)
fraction != 0
float = NaN

Example 9 (largest positive float, Float.MAX_VALUE):
Binary representation of a float
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber

  
  
  
  
  
  
  
  
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23

signbit = 0
exponent_field_val = 11111110bin = 254dec
exponent = exponent - 127 = 254 - 127 = 127
mantissa = hidden bit value + fraction
mantissa = 1 + 2-1 + 2-2 + ... +2-23 = 2 - 2-23 = 1.99999988079071044921875
float = (-1)signbit * mantissa * radixexponent
float = (-1)0 * (2 - 2-23) * 2127 = 3.40282346638528860e+38

Example 10 (lowest positive value, Float.MIN_VALUE):
Binary representation of a float
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
<bitnumber

  
  
  
  
  
  
  
  
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23

signbit = 0
exponent_field_val = 00000000bin (special case)
exponent = exponentmin = -126
mantissa = hidden bit value + fraction
mantissa = 0 + 2-23 = 2-23
float = (-1)signbit * mantissa * radixexponent
float = (-1)0 * 2-23 * 2-126 = 2-149 = 1.40129846432481707e-45