Floating Point Binary

Floating Point Binary IEEE 754 http://www.h-schmidt.net/FloatConverter/IEEE754.html CSc 28 1 IEEE Short Real 32 bits Sign 1 2 3 4 5 6 7 8 9 10 1...
Author: Winifred Hines
2 downloads 1 Views 561KB Size
Floating Point Binary IEEE 754 http://www.h-schmidt.net/FloatConverter/IEEE754.html

CSc 28

1

IEEE Short Real 32 bits Sign 1

2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

0

1 0 0 0 1 0 0 0 Exponent

0 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 Mantissa

Single Precision • 1 bit for the sign • 8 bits for the exponent • 23 bits for the mantissa

CSc 28

2

IEEE Long Real 32 bits Sign 1

1 2 3 4 5 6 7 8 9 10 11

1

0

1 0 0 0 1 0 0 0 0 0 0 Exponent

0 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 Mantissa

2

3

4

5

6

7

8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

Double Precision • 1 bit for the sign • 11 bits for the exponent • 52 bits for the mantissa

CSc 28

3

0

0

0

Convert the 8-bit floating point number e7 (in hex) to decimal. 1. 2. 3. 4. 5. 6. 7.

Convert: e716 = 111001112. Separate: 11100111 Mantissa: 1.0111 Exponent: 1102 = 610; 6 − 3 = 3. De-normalize: 1.01112 × 23 = 1011.1 Convert: Sign: negative.

Result: e7 is -11.5 23

22

21

20

2-1

Place Values

8

4

2

1

0.5

Bits

1

0

1

1

.

Value

8

1

+

Exponents

+

2

CSc 28

+

1 0.5 =

11.5

4

Convert the 8-bit floating point number e7 (in hex) to decimal. 1. 2. 3. 4. 5. 6. 7.

Convert: e716 = 111001112. Separate: 11100111 Mantissa: 1.0111 Exponent: 1102 = 610; 6 − 3 = 3. De-normalize: 1.01112 × 23 = 1011.1 Convert: Sign: negative.

Result: e7 is -11.5 23

22

21

20

2-1

Place Values

8

4

2

1

0.5

Bits

1

0

1

1

.

Value

8

1

+

Exponents

+

2

CSc 28

+

1 0.5 =

11.5

5

Convert the 8-bit floating point number e7 (in hex) to decimal. 1. 2. 3. 4. 5. 6. 7.

Convert: e716 = 111001112. Separate: 11100111 Mantissa: 1.0111 Exponent: 1102 = 610; 6 − 3 = 3. De-normalize: 1.01112 × 23 = 1011.1 Convert: Sign: negative.

Result: e7 is -11.5 23

22

21

20

2-1

Place Values

8

4

2

1

0.5

Bits

1

0

1

1

.

Value

8

1

+

Exponents

+

2

CSc 28

+

1 0.5 =

11.5

6

Convert the 8-bit floating point number e7 (in hex) to decimal. 1. 2. 3. 4. 5. 6. 7.

Convert: e716 = 111001112. Separate: 11100111 Mantissa: 1.0111 Exponent: 1102 = 610; 6 − 3 = 3. De-normalize: 1.01112 × 23 = 1011.1 Convert: Sign: negative.

Result: e7 is -11.5 23

22

21

20

2-1

Place Values

8

4

2

1

0.5

Bits

1

0

1

1

.

Value

8

1

+

Exponents

+

2

CSc 28

+

1 0.5 =

11.5

7

Convert the 8-bit floating point number e7 (in hex) to decimal. 1. 2. 3. 4. 5. 6. 7.

Convert: e716 = 111001112. Separate: 11100111 Mantissa: 1.0111 Exponent: 1102 = 610; 6 − 3 = 3. De-normalize: 1.01112 × 23 = 1011.1 Convert: Sign: negative.

Result: e7 is -11.5 23

22

21

20

2-1

Place Values

8

4

2

1

0.5

Bits

1

0

1

1

.

Value

8

1

+

Exponents

+

2

CSc 28

+

1 0.5 =

11.5

8

Convert the 8-bit floating point number e7 (in hex) to decimal. 1. 2. 3. 4. 5. 6. 7.

Convert: e716 = 111001112. Separate: 11100111 Mantissa: 1.0111 Exponent: 1102 = 610; 6 − 3 = 3. De-normalize: 1.01112 × 23 = 1011.1 Convert: Sign: negative.

Result: e7 is -11.5 23

22

21

20

2-1

Place Values

8

4

2

1

0.5

Bits

1

0

1

1

.

Value

8

1

+

Exponents

+

2

CSc 28

+

1 0.5 =

11.5

9

Normalize Fixed Point Number Normalize 0.00010101 Shift decimal 4 places to right 1.0101 mantissa (normalized) exponent – 4

1 assumed 0101 mantissa

1.0101

To un-normalize, shift decimal 4 places to the left 1.0101 to the original .00010101

CSc 28

10

Bias Notation Represent negative and positive numbers – but to do so, store them so that 000 represents the smallest value and 111 the largest. (1) largest

smallest

k=3 bits 111 110 101 100 011 010 001 000

value 7 6 5 4 3 2 1 0

CSc 28

(3)

(2) Value - 2 3 2 1 0 -1 -2 -3 -4

(k-1)

(2) + 2

(k-1)

7 6 5 4 3 2 1 0

11

Bias Notation – 3 bit exponent Represent negative and positive numbers – but to do so, store them so that 000 represents the smallest value and 111 the largest. (1) largest

smallest

value 7 6 5 4 3 2 1 0

k=3 bits 111 110 101 100 011 010 001 000

(3)

(2) Value - 2 3 2 1 0 -1 -2 -3 -4

(k-1)

(2) + 2

(k-1)

7 6 5 4 3 2 1 0

3 bit exponent shift value applied to Mantissa

12

Positive and Negative Exponents Exponents to be stored: -7 -6 -5 -4

-3

-2

-1

0

1

2

3

4

5

6

7

8

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Corresponding characteristics 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

If the exponent to be stored is – 7  e  8, the corresponding characteristic, c, would be c = e + (2k-1 -1) To convert the corresponding characteristic to the exponent: e = c – (2k-1 -1)

CSc 28

e is the shift number k the number of bits used to represent the exponent

13

8-bit exponent k

8 k-1

Bias 2 - 1 Value of Exponent (1) 1111 1111 255 1111 1110 1111 1101 … 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101

254 253 … 129 128 127 126 125

… 0000 0001 0000 0000

… 1 0

3 bit exponent k 3

4 bit exponent 4 k

127

(1) - Bias For infinities 127 126 … 2 1 0 -1 -2 … -126 For denorms

k-1

Bias 2 - 1 Value of Exponent (1) 15 1111 14 1110 13 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 0000

12 11 10 9 8 7 6 5 4 3 2 1 0

CSc 28

7

(1) - Bias 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7

k-1

Bias 2 - 1 3 Value of Exponent Exponent - Bias (1) - Bias (1) 111 7 4 110 6 3 101 5 2 100 4 1 011 3 0 010 2 -1 001 1 -2 000 0 -3

k the number of bits used to represent the exponent

14

Floating Point Representation The Sign Single bit (1 = negative, 0 = positive)

Binary number represented in normalized exponential form with two parts: Both numbers are represented as integers

1010.1 Mantissa: 1.0101 Exponent: 011

How to represent negative exponents CSc 28

15

8 bit Example Sign 0

1 1 0 Biased Exponent

0

1 1 Mantissa

Mantissa: 1.0111 Biased Exponent: c = 110 = 6 e = c – (2k-1 – 1) e = 6 – (23-1 – 1) = 6 - (3) = 3

1

k the number of bits used to represent the exponent

1.0111  23 = 1011.1 = 11 + .5 = 11.5 Powers of 2 9 8 7

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

CSc 28

16

Examples Binary Decimal Fraction 0.1 1/2 0.01 1/4 0.001 1/8 0.0001 1/16 0.00001 1/32

CSc 28

Decmal Value 0.50000 0.25000 0.12500 0.06250 0.31250

17

IEEE Short 32 bit exponents 8-bit exponent k

Corresponding Exponent Characteristic e + 127 e +5 132 0 127 - 10 117 + 128 255 - 127 0 -1 126

Powers of 2 9 8 7

8 k-1

Bias 2 - 1 Value of Exponent (1) 255 1111 1111

Binary 1000 0100 0111 1111 0111 0101 1111 1111 0000 0000 0111 1110

1111 1110 1111 1101 … 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101

254 253 … 129 128 127 126 125

… 0000 0001 0000 0000

… 1 0

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

CSc 28

127

(1) - Bias For infinities 127 126 … 2 1 0 -1 -2 … -126 For denorms

18

0.00011 to 1.10000 -4 1000.1 to 1.0001 +4

Normalizing the Mantissa Binary Normalized Exponent Value as e 1101.101 1.101101 +3 0.00101 1.01 -3 1.0001 1.0001 0 10000011 1.0000011 +7

• Shift the binary point so that only one digit appears before the point • The exponent expresses the number of positions the decimal point was moved • The sign: to restore the binary point to its original position; plus (+) means the point must be shifted to the right, minus (– ) means the point must be shifted to the left CSc 28

19

Conversion Procedure 1. If the original number is hex, convert it to binary 2. Separate the sign, exponent and mantissa fields 3. Extract the mantissa and restore the leading one (omit the trailing zeros) 4. Extract the exponent and subtract the bias to get the actual exponent (n) Bias = 2k-1 – 1 where k is the number of bits in the exponent field (3 for the 8-bit format and 127 for the 32-bit format)

5. De-normalize the number: move the binary point n spaces to the right if n is negative or to the left if n is positive. 6. Convert the binary value to decimal. 7. Set the sign of the decimal number according to the sign bit of the original floating point number CSc 28

20

Shift = – 5

Normalize c = (127 + (– 5) = 122)

Normalize .0000101 0.000101 00.00101 000.0101 0000.101 00001.01 Negative exponent means a shift to the left to restore

de-Normalize 1.01 .101 .0101 .00101 .000101 .0000101 1.01 2– 5

1.01  2– 5 CSc 28

21

Shift = – 5

Normalize c = (127 + (– 5) = 122)

Normalize 1011.101 101.1101 10.11101 1.011101 Positive exponent means a shift to the right to restore 1.011101  2+ 3

de-Normalize 1.011101 1.011101 10.11101 101.1101 1.011101  2+ 3

CSc 28

22

Your turn… Convert the 8-bit floating point number to decimal 0010 0110 sign bit 3-bit Exponent 4-bit mantissa

Powers of 2 9 8 7

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

CSc 28

23

0010 0110 Bias Sign bit: positive Exponent: 010 = 2 e = c – (2k-1 – 1) = 2 – (22 – 1) = 2 – 3 = – 1 Denormalize 1.011  2-1 = 0.1011 Value 0.5 + 0.125 + .0625 = .6875 k the number of bits used to represent the exponent CSc 28

24

One more Convert the 8-bit floating point number to decimal 11010011 sign bit 3-bit Exponent 4-bit mantissa

Powers of 2 9 8 7

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

CSc 28

25

1101 0011 Sign bit: negative Exponent: 101 = 5 e = c – (2k-1 – 1) = 5 – (22 – 1) = 5 – 3 = 2 Denormalize k the number of bits +2 1.0011  2 = 100.11 used to represent the Value – (4 + 0.5 + .25) = – 4.75

CSc 28

exponent

26

Interpreting IEEE Bit Representation Biased Exponent 127 130 124 132 120

Binary Value -1.11 1101.101 -0.00101 100111.0 .0000001101011

Powers of 2 9 8 7

Sign Exponent 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 1

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10 CSc 28

1 0 1 0 1

1 0 1 0 1

1 0 1 1 0

1 1 0 0 0

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms

1 0 0 0 0

Mantissa 1 1 1 0 1 0 1 0 1 1 1 0 1 Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

1

0

1

1 0

1

1

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0

Interpreting IEEE Bit Representation Biased Exponent 127 130 124 132 120

Binary Value -1.11 1101.101 -0.00101 100111.0 .0000001101011 Powers of 2 9 8 7

Sign Exponent 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 1

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

Biased Exponent – Bias 127 – 127 = 0 Decimal equivalent 1.11  20 = 1.11 = 1.75 CSc 28

1 0 1 0 1

1 0 1 0 1

1 0 1 1 0

1 1 0 0 0

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms

1 0 0 0 0

Mantissa 1 1 1 0 1 0 1 0 1 1 1 0 1 Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

1

0

1

1 0

1

1

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0

Creating IEEE Bit Representation Biased Exponent 127 130 124 132 120

Binary Value -1.11 1101.101 -0.00101 100111.0 .0000001101011 Powers of 2 9 8 7

Sign Exponent 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 1

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

29

CSc 28

1 0 1 0 1

1 0 1 0 1

1 0 1 1 0

1 1 0 0 0

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms

1 0 0 0 0

Mantissa 1 1 1 0 1 0 1 0 1 1 1 0 1 Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

1

0

1

1 0

1

1

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0

Creating IEEE Bit Representation Biased Exponent 127 130 124 132 120

Binary Value -1.11 1101.101 -0.00101 100111.0 .0000001101011 Powers of 2 9 8 7

Sign Exponent 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 1

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

Biased Exponent – Bias Exponent = 130 – 127 = 3

Decimal equivalent 1.101101  23 = 1101.101 = 13.625

30

CSc 28

1 0 1 0 1

1 0 1 0 1

1 0 1 1 0

1 1 0 0 0

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms

1 0 0 0 0

Mantissa 1 1 1 0 1 0 1 0 1 1 1 0 1 Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

1

0

1

1 0

1

1

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0

Creating IEEE Bit Representation Biased Exponent 127 130 124 132 120

Binary Value -1.11 1101.101 -0.00101 100111.0 .0000001101011 Powers of 2 9 8 7

Sign Exponent 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 1

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

31

CSc 28

1 0 1 0 1

1 0 1 0 1

1 0 1 1 0

1 1 0 0 0

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms

1 0 0 0 0

Mantissa 1 1 1 0 1 0 1 0 1 1 1 0 1 Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

1

0

1

1 0

1

1

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0

Creating IEEE Bit Representation Biased Exponent 127 130 124 132 120

Binary Value -1.11 1101.101 -0.00101 100111.0 .0000001101011 Powers of 2 9 8 7

Sign Exponent 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 1

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

Biased Exponent – Bias Exponent = 124 – 127 = – 3

32

Decimal equivalent CSc 28 – 1. 01  2-3 = – .00101 = – .15625

1 0 1 0 1

1 0 1 0 1

1 0 1 1 0

1 1 0 0 0

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms

1 0 0 0 0

Mantissa 1 1 1 0 1 0 1 0 1 1 1 0 1 Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

1

0

1

1 0

1

1

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0

Powers of 2 9 8 7

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

Zero exponent: 0111 1111 Range of exponents: – 126 to 127

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms

Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0

1111 1111 reserved for infinities or NaN (not a number) e.g divide by zero 0000 0000 reserved not (and cannot be) normalized numbers, smaller than 2 – 126 (left with 0.001  2 -126 which is represented by 0 0000 0000 001000 … 0) CSc 28 33

Your turn… Convert the 32-bit floating point number to decimal Sign bit 0 Exponent 1000 1000 Mantissa 0110 1100 0010 …

Powers of 2 9 8 7

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms CSc 28

Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0 34

Your turn… again Convert the 32-bit floating point number to decimal Sign bit Exponent Mantissa

1 1000 0100 001011…

Powers of 2 9 8 7

6

5

4

3

2

1

0

512 256 128 64

32

16

8

4

2

1 Base 10

Unbiased Decimal Exponent For infinities 127 126 … 3 2 1 0 -1 -2 -3 … -126 For denorms CSc 28

Biased Exponent 1111 1111 1111 1110 1111 1101 … 1000 0010 1000 0001 1000 0000 0111 1111 0111 1110 0111 1101 0111 1100 … 0000 0001 0000 0000

Decimal Value Biased Exponent 255 254 253 … 130 129 128 127 126 125 124 … 1 0 35