Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point Representation Dale Roberts, Lecturer
Dale Roberts Negative Numbers in Binary Four different representation schemes are used for negative numbers 1. Signed Magnitude Left most bit (LMB) is the sign bit : 0 positive (+) 1 negative (-) Remaining bits hold absolute magnitude Example: 2 10 b b Q: = ? = ? Try, b =-4 10
Dale Roberts 2.One’s Complement – –Left most bit is the sign bit : 0 positive (+) 1 negative (-) – –The magnitude is Complemented Example: 2 10 b b Exercise: try using 1’s Complement Q: = ? = ? = ? Solution: 4 10 = b = b 1
Dale Roberts Negative Numbers in Binary (cont.) 3.2’s Complement Sign bit same as above Magnitude is Complemented first and a “1” is added to the Complemented digits Example: 2 10 b 1’s Complement b b 7 10 1’s Complement Exercise: try using 2’s Complement b b b
Dale Roberts Negative Numbers in Binary (cont.) 7 10 = b 3 10 = b 1’s complement b 2’s complement b (-3) Example: 7+(-3) [hint]: A – B = A + (~B) carry ignore 4 10
Dale Roberts Three Representation of Signed Integer
Dale Roberts Negative Numbers in Binary (cont.) 4. Excess 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 value MSB can be used as a sign bit, but If MSB =1, positive number If MSB =0, negative number Numbers Binary Value Notation Excess – 8 Value
Dale Roberts Fundamental Data Type 2 byte unsigned (Default type is int ) 2 byte int ( 0 D ) ( 1 D ) ( 2 D ) … ( D ) ( D 2 15 ) … ( 2 16 –1) ( D ) ( D ) … ( - 2 D ) ( - 1 D ) ( 0 D ) ( 1 D ) ( 2 D ) … ( D ) With vs. without using sign bit With vs. without using sign bit For a 16 bit binary pattern:
Dale Roberts Fundamental Data Type Four Data Types in C Four Data Types in C (assume 2’s complement, byte machine) Data Type Abbreviation Size (byte) Range char ~ 127 unsigned char10 ~ 255 int 2 or ~ or ~ unsigned int unsigned 2 or 40 ~ or 0 ~ short int short ~ unsigned short int unsigned short 20 ~ long int long ~ unsigned long int unsigned long 40 ~ float4 double8 Note:2 7 = 128, 2 15 =32768, 2 15 = Complex and double complex are not available
Dale Roberts Fractional Numbers Examples: = 4 x x x x x = 1 x x x x x x 2 -2 = /2 + ¼ = = Conversion from binary number system to decimal system Examples: = 1 x x x x x 2 -2 = /2 + ¼ = Examples: ½ ¼ 1/ xxxx
Dale Roberts Conversion from decimal number system to binary system Examples: = (?) 2 1. Conversion of the integer part: same as before – repeated division by 2 7 / 2 = 3 (Q), 1 (R) 3 / 2 = 1 (Q), 1 (R) 1 / 2 = 0 (Q), 1 (R) 7 10 = Conversion of the fractional part: perform a repeated multiplication by 2 and extract the integer part of the result 0.75 x 2 =1.50 extract x 2 = 1.0 extract = stop Combine the results from integer and fractional part, = Combine the results from integer and fractional part, = How about choose some of Examples: try B write in the same order 421 1/21/41/8 =0.5 =0.25=0.125
Dale Roberts Fractional Numbers (cont.) Exercise 1: Convert (0.625) 10 to its binary form Exercise 2: Convert (0.6) 10 to its binary form Solution: Solution: x 2 = 1.25 extract x 2 = 0.5 extract x 2 = 1.0 extract stop (0.625) 10 = (0.101) x 2 = 1.2 extract x 2 = 0.4 extract x 2 = 0.8 extract x 2 = 1.6 extract x 2 = (0.6) 10 = ( …) 2
Dale Roberts Fractional Numbers (cont.) Exercise 3: Convert (0.8125) 10 to its binary form Solution: x 2 = extract x 2 = 1.25 extract x 2 = 0.5 extract x 2 = 1.0 extract stop (0.8125) 10 = (0.1101) 2
Dale Roberts Fractional Numbers (cont.) Errors One source of error in the computations is due to back and forth conversions between decimal and binary formats Example: (0.6) 10 + (0.6) 10 = Since (0.6) 10 = ( …) 2 Lets assume a 8-bit representation: (0.6) 10 = ( ) 2, therefore Lets reconvert to decimal system: ( ) b = 1 x x x x x x x x x 2 -8 = 1 + 1/8 + 1/16 + 1/128 = Error = 1.2 – = =
Dale Roberts If x is a real number then its normal form representation is: x = f Base E where f : mantissa E: exponent exponent Example: = mantissa = = –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 Representation
Dale Roberts Example: = x = x B = B = AB.CD H = 0.00AC H =
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, b 1 b 2 b 3 ?1?1 ?2?2 ?3?3 ?4?4 ? 11 ? 10 ?9?9 ?8?8 ?7?7 ?5?5 ?6?6 f = 0.? 1 ? 2 ? 3 ? 4 …? 11 ? 12 …? 15 b1b1 b2b2 b3b3 + : 0 - : 1
Dale Roberts Question: How the computer expresses the 16-bit approximation of 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 bitsAnswer: Step 1: Normalization = * 2 +4 Step 2: “Plant” 16 bits the 16 bit floating point representation is Floating Point Number Representation exponent 3 bits sign 1 bit mantissa 11 bits sign 1 bit
Dale Roberts Question: Interpret the normalized binary number 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: B = B * 2 -2 = 7/32 = D
Dale Roberts Real Life Example: IEEE 754 IEEE Standard 754 is the representation of floating point used on most computers. Single precision (float) is 32 bits or 4 bytes with 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. 2 0 becomes 127, 2 1 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.
Dale Roberts Real Life Example: IEEE 754 IEEE 754 Examples: Normalized Numbers = 1 x 2 4 = = 1 x = e = 1.25 x 2 2 = 5
Dale Roberts Real Life Example: IEEE 754 Double precision (double) is 64 bites or 8 bytes with the following configuration. 1 sign bit 11 exponent 52 fraction The definition of the fields matches single precision. The double precision bias is 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).
Dale Roberts The 32 Bit Single Precision Floating Point Format for IBM 370 Base = 16 Exponent = Excess-64 notation (i.e., compute binary equivalent, then substrate 64) Sign = sign of number (0: positive, 1: negative) Mantissa = normalized fraction (i.e. first digital after ‘.’ is non-zero) Example: What is the value of the following point number? Sign = 1 the number is negative Exponent (E) = = = 2 (substrate 64, because of Excess-64 ) Mantissa (f ) = = 93D7C2 H The above floating point number is: x = (sign) f 16 E = D7C x = - ( 9 x x D x x C x x ) 16 2 = - ( 9 x x x x x x ) = - ( ) = Real Life Example: IBM 370 mantissa 24 bits exponent 7 bits sign 1 bit
Dale Roberts Acknowledgements These slides where originally prepared by Dr. Jeffrey Huang, updated by Dale Roberts. IEEE 754 information was obtained from Steve Hollasch IEEE 754 examples were obtained from Tony Cassandra at St. Edward’s University.