24
Dale Robert Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point Representation Dale Roberts, Lecturer Dale Roberts, Lecturer IUPUI IUPUI [email protected] [email protected]

Department of Computer and Information Science, School of Science, IUPUI

  • Upload
    wylie

  • View
    22

  • Download
    1

Embed Size (px)

DESCRIPTION

Department of Computer and Information Science, School of Science, IUPUI. CSCI 230. Information Representation: Negative and Floating Point Representation. Dale Roberts, Lecturer IUPUI [email protected]. Negative Numbers in Binary. - PowerPoint PPT Presentation

Citation preview

Page 1: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Department of Computer and Information Science,School of Science, IUPUI

CSCI 230

Information Representation: Negative and Floating Point Representation

Dale Roberts, LecturerDale Roberts, Lecturer

IUPUIIUPUI

[email protected]@cs.iupui.edu

Page 2: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Negative Numbers in BinaryNegative Numbers in BinaryFour different representation schemes are used Four different representation schemes are used for negative numbersfor negative numbers

1.1. Signed MagnitudeSigned MagnitudeLeft most bit (LMB) is the sign bit :

0 positive (+)1 negative (-)

Remaining bits hold absolute magnitudeExample:

210 0000 0010b

-210 1000 0010b

Q: 0000 0000 = ? 1000 0000 = ?

Try, 1000 0100b = -410

Page 3: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

2.2. One’s ComplementOne’s Complement– Left most bit is the sign bit :

• 0 positive (+)• 1 negative (-)

– The magnitude is Complemented

Example: 210 0 000 0010b

-210 1 111 1101b

Exercise: try - 410 using 1’s Complement

Q: 0000 0000 = ?Q: 0000 0000 = ? 1111 1111 = ?1111 1111 = ?

Solution: 410 = 0 000 0100 b

-410 = 111 1011 b1

Page 4: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Negative Numbers in Binary Negative Numbers in Binary (cont.)(cont.)

3. 2’s Complement• Sign bit same as above• Magnitude is Complemented first and a “1” is added to

the Complemented digits

Example: 210 0 000 0010b

1’s Complement 1 111 1101b

+ 1

-210 1 111 1110b

710

1’s Complement +

1

-710

Exercise: try -710 using 2’s Complement0000 0111b

1111 1000b

1111 1001b

Page 5: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Negative Numbers in Binary Negative Numbers in Binary (cont.)(cont.)

771010 = 0000 0111 = 0000 0111bb

331010 = 0000 0011 = 0000 0011bb 1’s complement1’s complement 1111 1100 1111 1100bb 2’s complement2’s complement 1111 1101 1111 1101bb -3 -31010

7+(-3) 7+(-3) 0000 01110000 0111 ++ 1111 11011111 1101

Example: 7+(-3) [hint]: A – B = A + (~B) +1

1 1111 111 carry

ignore 1 0000 0100 0000 0100 410

Page 6: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Value RepresentationRepresentation Signed Magnitude 1's Complement 2's Complement

0 00000 00010 00100 00110 01000 01010 01100 01110 10000 10010 10100 10110 11000 11010 11100 11111 00001 00011 00101 00111 01001 01011 01101 01111 10001 10011 10101 10111 11001 11011 11101 1111

0123456789

101112131415-0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15

0123456789101112131415-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1-0

0123456789101112131415-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1

Three Representation of Signed IntegerThree Representation of Signed Integer

Page 7: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Negative Numbers in Binary Negative Numbers in Binary (cont.)(cont.)

4.4. Excess RepresentationExcess Representation– For a given fixed number of bits

the range is remapped such that roughly half the numbers are negative and half are positive.

Example: (as left)Excess – 8 notation for 4 bit numbers

Binary value = 8 + excess-8 valueMSB can be used as a sign bit, but

If MSB =1, positive numberIf MSB =0, negative number

Numbers Binary Value

Notation Excess – 8 Value

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

-8

-7

-6

-5

-4

-3

-2

-1

0

1

2

3

4

5

6

7

Page 8: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Fundamental Data TypeFundamental Data Type

2 byte unsigned (Default type is

int)2 byte int

0000 0000 0000 0000 ( 0D)

0000 0000 0000 0001 ( 1D )

0000 0000 0000 0010 ( 2D )

….

0111 1111 1111 1111 ( 32767D 215 -1)1000 0000 0000 0000 ( 32768D 215)

….

1111 1111 1111 1111 ( 216 –1)

1000 0000 0000 0000 ( -32768D - 215 )

1000 0000 0000 0001 ( -32767D - 215 +1)

….

1111 1111 1111 1110 ( - 2D )

1111 1111 1111 1111 ( - 1D )

0000 0000 0000 0000 ( 0D )

0000 0000 0000 0001 ( 1D )

0000 0000 0000 0010 ( 2D )

….

0111 1111 1111 1111 ( 32767D 215 -1)

• With vs. without using sign bitWith vs. without using sign bit

For a 16 bit binary pattern:

Page 9: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Fundamental Data TypeFundamental Data TypeFour Data TypesFour Data Types in Cin C (assume 2’s complement, byte machine)

Data TypeData Type AbbreviationAbbreviation Size (byte)Size (byte) RangeRange

char char 1 -128 ~ 127

unsigned char 1 0 ~ 255

int

int 2 or 4 -215 ~ 215-1 or -231 ~ 231-1

unsigned int unsigned 2 or 4 0 ~ 65535 or 0 ~ 232-1

short int short 2 -32768 ~ 32767

unsigned short int

unsigned short 2 0 ~ 65535

long int long 4 -231 ~ 231-1

unsigned long int

unsigned long 4 0 ~ 232-1

float 4

double 8

Note: 27 = 128, 215 =32768, 215 = 2147483648Complex and double complex are not available

Page 10: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Fractional NumbersFractional NumbersExamplesExamples: : 456.78456.781010 = 4 x = 4 x 101022 + 5 x + 5 x 101011 + 6 x + 6 x 101000 + 7 x + 7 x 1010-1-1+8 x +8 x 1010-2-2

1011.111011.1122 = 1 x = 1 x 2233 + 0 x + 0 x 2222 + 1 x + 1 x 2211 + 1 x + 1 x 220 0 + 1 x + 1 x 22-1-1 + +

1 x 1 x 22-2-2

= 8 + 0 + 2 + 1 + 1/2 + ¼= 8 + 0 + 2 + 1 + 1/2 + ¼

= 11 + 0.5 + 0.25 = 11.75= 11 + 0.5 + 0.25 = 11.751010

Conversion from binary number system to Conversion from binary number system to decimal systemdecimal system

ExamplesExamples: : 111.11111.1122 = 1 x = 1 x 2222 + 1 x + 1 x 2211 + 1 x + 1 x 220 0 + 1 x + 1 x 22-1-1 + 1 x + 1 x 22-2-2

= 4 + 2 + 1 + 1/2 + ¼ = 7.75= 4 + 2 + 1 + 1/2 + ¼ = 7.751010

ExamplesExamples: 11.011: 11.01122

22 21 20 2-1 2-2 2-3

4 2 1 ½ ¼ 1/8

2 1 0 -1 -2 -3

x x x x

Page 11: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Conversion from decimal number system to binary systemConversion from decimal number system to binary systemExamplesExamples: : 7.757.751010 = (?) = (?)22

1.1. Conversion of the Conversion of the integerinteger part: same as before – repeated division by 2 part: same as before – repeated division by 2

7 / 2 = 3 (Q), 7 / 2 = 3 (Q), 11 (R) (R) 3 / 2 = 1 (Q), 3 / 2 = 1 (Q), 11 (R) (R) 1 / 2 = 0 (Q), 1 / 2 = 0 (Q), 11 (R) 7 (R) 710 10 == 111 11122

2.2. Conversion of the Conversion of the fractional fractional part: perform a repeated multiplication by 2 and extract the integer part part: perform a repeated multiplication by 2 and extract the integer part of the resultof the result0.75 x 2 =0.75 x 2 =11..50 50 extract extract 11

0.5 x 2 = 0.5 x 2 = 11..0 0 extract extract 11 0.75 0.751010 = 0. = 0.111122

0.0 0.0 stop stop

Combine the results from integer and fractional part, 7.75Combine the results from integer and fractional part, 7.751010 = = 111111..111122

How about choose some ofHow about choose some of

ExamplesExamples: try 5.625: try 5.625BB

write in the same order

4 2 1 1/2 1/4 1/8=0.5 =0.25 =0.125

Page 12: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Fractional Numbers Fractional Numbers (cont.)(cont.)

Exercise 1Exercise 1: Convert (0.625): Convert (0.625)10 10 to its binary formto its binary form

Exercise 2Exercise 2: Convert (0.6): Convert (0.6)1010 to its binary formto its binary form

Solution: Solution:

Solution: 0.625 x 2 = 1.25 extract 1

0.25 x 2 = 0.5 extract 0

0.5 x 2 = 1.0 extract 1

0.0 stop

(0.625)10 = (0.101)2

0.6 x 2 = 1.2 extract 1

0.2 x 2 = 0.4 extract 0

0.4 x 2 = 0.8 extract 0

0.8 x 2 = 1.6 extract 1

0.6 x 2 =

(0.6)10 = (0.1001 1001 1001 …)2

Page 13: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Fractional Numbers Fractional Numbers (cont.)(cont.)

Exercise 3Exercise 3: Convert (0.8125): Convert (0.8125)10 10 to its binary formto its binary form

Solution: 0.8125 x 2 = 1.625 extract 1

0.625 x 2 = 1.25 extract 1

0.25 x 2 = 0.5 extract 0

0.5 x 2 = 1.0 extract 1

0.0 stop

(0.8125)10 = (0.1101)2

Page 14: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Fractional Numbers Fractional Numbers (cont.)(cont.)

ErrorsErrorsOne source of error in the computations is due to back and One source of error in the computations is due to back and forth conversions between decimal and binary formatsforth conversions between decimal and binary formatsExample: (0.6)Example: (0.6)1010 + (0.6) + (0.6)10 10 = 1.2= 1.21010

Since Since (0.6)(0.6)1010 = (0.1001 1001 1001 …)= (0.1001 1001 1001 …)22

Lets assume a 8-bit representation: Lets assume a 8-bit representation: (0.6)(0.6)1010 = (0 .= (0 .10011001 10011001))2 2 , therefore, therefore

0.60.6 0.100110010.10011001+ 0.6 + 0.6 + + 0.100110010.10011001

1.001100101.00110010Lets reconvert to decimal system: Lets reconvert to decimal system:

(1.00110010)(1.00110010)bb

= 1 x = 1 x 220 0 + 0 x + 0 x 22-1-1 + 0 x + 0 x 22-2 -2 + 1 x + 1 x 22-3-3 + 1 x + 1 x 22-4 -4 + 0 x + 0 x 22-5-5 + 0 x + 0 x 22-6-6 + 1 x + 1 x 22-7-7 + 0 x + 0 x 22-8 -8

= 1 + 1/8 + 1/16 + 1/128 = 1.1953125= 1 + 1/8 + 1/16 + 1/128 = 1.1953125

Error = 1.2 – 1.1953125Error = 1.2 – 1.1953125

= 0.0046875= 0.0046875

Page 15: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

If x is a real number then its normal form representation is:

x = f • Base E

where f : mantissa

E: exponentexponent

Example: 125.3210 = 0.12532 • 103

mantissa

- 125.3210 = - 0.12532 • 103

0.054610 = 0.546 • 10 –1

The mantissa is normalized, so the digit after the fractional point is non-zero.

If needed the mantissa should be shifted appropriately to make the first digit (after the fractional point) to be non-zero & the exponent is properly adjusted.

Floating Point Number RepresentationFloating Point Number Representation

Page 16: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Example:

134.1510 = x 10

0.002110 = x 10

101.11B =

0.011B =

AB.CDH=

0.00ACH=

0.13415

0.21

3

-2

Page 17: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Assume we use 16-bit binary pattern for normalized binary form based on the following convention (MSB to LSB)

Sign of mantissa (±)= left most bit (where 0: +; 1: - )

Mantissa (f)= next 11 bits

Sign of exponent (±)= next bit (where 0: +; 1: - )

Exponent (E) = next three bits

x = ± f • Base ± E

LSBMSB

+ : 0- : 1

E : converted to binary, b1b2b3

?1 ?2 ?3 ?4 ?11?10?9?8?7?5 ?6

f = 0.?1?2?3?4…?11 ?12…?15

b1 b2 b3

+ : 0- : 1

Page 18: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Question:Question:

How the computer expresses the 16-bit approximation of 1110.111010111111 in normalized binary form using the following convention

Sign of mantissa = left most bit (where 0: +; 1: - )

Mantissa = next 11 bits

Sign of exponent = next bit (where 0: +; 1: - )

Exponent = next three bits

Answer:Answer:Step 1: NormalizationStep 1: Normalization1110.111010111111 = + 0.1110111010111111 * 2 1110.111010111111 = + 0.1110111010111111 * 2 +4+4

Step 2: “Plant” 16 bitsStep 2: “Plant” 16 bits

the 16 bit floating point representation isthe 16 bit floating point representation is 00 1110111010111101110101 00 100100

Floating Point Number RepresentationFloating Point Number Representation

exponent3 bits

sign1 bit

mantissa11 bits

sign1 bit

Page 19: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Question:Question:

Interpret the normalized binary number

0111 0000 0000 1010 B

using the convention mentioned Sign of mantissa = left most bit (where 0: +; 1: - )

Mantissa = next 11 bits

Sign of exponent = next bit (where 0: +; 1: - )

Exponent = next three bits

find its decimal equivalent.

Answer:

0 11100000000 1 010 B = 0.111B * 2-2

= 7/32 = 0.21875D

Page 20: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Real Life Example: IEEE 754Real Life Example: IEEE 754

IEEE Standard 754 is the representation of IEEE Standard 754 is the representation of floating point used on most computers.floating point used on most computers.

Single precision (float) is 32 bits or 4 bytes with Single precision (float) is 32 bits or 4 bytes with the following configuration.the following configuration.

1 sign bit 8 exponent 23 fraction•The sign field is 0 for positive or 1 for negative•The exponent field has a bias of 127, meaning that 127 is added to the exponent before it’s stored. 20 becomes 127, 21 becomes 128, 2-3 becomes 124, etc.•In the mantissa, the decimal point is assumed to follow the first ‘1’. Since the first digit is always a ‘1’, a hidden bit is used to representing the bit. The fraction is the 23 bits following the first ‘1’. The fraction really represents a 24 bit mantissa.

Page 21: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Real Life Example: IEEE 754Real Life Example: IEEE 754

IEEE 754 Examples: Normalized NumbersIEEE 754 Examples: Normalized Numbers

0 1000 0011 0000 0000 0000 0000 0000 000 0 1000 0011 0000 0000 0000 0000 0000 000

= 1 x 2= 1 x 244 = 16 = 16

0 0011 0001 0000 0000 0000 0000 0000 000 0 0011 0001 0000 0000 0000 0000 0000 000

= 1 x 2= 1 x 2-78-78 = 3.3087e-24 = 3.3087e-24

0 1000 0001 0100 0000 0000 0000 0000 000 0 1000 0001 0100 0000 0000 0000 0000 000

= 1.25 x 2= 1.25 x 222 = 5 = 5

Page 22: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Real Life Example: IEEE 754Real Life Example: IEEE 754

Double precision (double) is 64 bites or 8 bytes Double precision (double) is 64 bites or 8 bytes with the following configuration.with the following configuration.

1 sign bit 11 exponent 52 fraction

•The definition of the fields matches single precision.•The double precision bias is 1023.•What value can you not represent because of the hidden bit?•Certain bit patterns are reserved to represent special values. Of particular importance is the representation for zero (all bits zero). There are also patterns to represent infinity, positive and negative numeric overflow, positive and negative numeric underflow, and not-a-number (abbreviated NaN).

Page 23: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

The 32 Bit Single Precision Floating Point Format for IBM 370The 32 Bit Single Precision Floating Point Format for IBM 370Base = 16Base = 16

Exponent = Excess-64 notation (i.e., compute binary equivalent, then substrate 64)Exponent = Excess-64 notation (i.e., compute binary equivalent, then substrate 64)

Sign = sign of number (0: positive, 1: negative)Sign = sign of number (0: positive, 1: negative)

Mantissa = normalized fraction (i.e. first digital after ‘.’ is non-zero)Mantissa = normalized fraction (i.e. first digital after ‘.’ is non-zero)

Example: What is the value of the following point number?

1 100 0010 1001 0011 1101 0111 1100 0010

Sign = 1 the number is negative

Exponent (E) = 100 00102 = 6610 = 2 (substrate 64, because of Excess-64 )

Mantissa (f ) = 1001 0011 1101 0111 1100 0010 = 93D7C2H

The above floating point number is: x = (sign) f • 16 E = - 0.93D7C2 • 16 2

x = - (9 x 16-1 + 3 x 16-2

+ D x 16-3 + 7 x 16-4

+ C x 16-5 + 2 x 16-6) • 16 2

= - (9 x 161 + 3 x 160

+ 13 x 16-1 + 7 x 16-2

+ 12 x 16-3 + 2 x 16-4)

= - (144+3 +0.8125+0.02734375 + 0.0029296875 + 0.000030517578125)

= - 147.842803955078125

Real Life Example: IBM 370Real Life Example: IBM 370

mantissa24 bitsexponent

7 bitssign1 bit

Page 24: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

AcknowledgementsAcknowledgementsThese slides where originally prepared by Dr. Jeffrey Huang, updated by Dale Roberts.

IEEE 754 information was obtained from Steve Hollasch http://stevehollasch.com/cgindex/coding/ieeefloat.html.

IEEE 754 examples were obtained from Tony Cassandra at St. Edward’s University.