Fundamentals of Computer Engineering 1 Exercise 7 Fundamentals of Computer Engineering 1 Exercise 7 Faculty of Engineering Department of Electrical Engineering and Information Technology Institute for Computer Engineering- Prof. Dr. A. Hunger Fundamentals of Computer Engineering 1 Kevin Moheb, M. Sc. Tel: 0203 - 379 - 3577 Kevin.moheb@uni-due.de Slide 1 Fundamentals of Computer Engineering 1 Exercise 7 Objectives • Learn how to represent signed numbers in binary systems: - Sign-Magnitude Representation - 1s complement - 2s complement • Learn what Floating Points are and how tod eal with them Slide 2 1 Fundamentals of Computer Engineering 1 Exercise 7 Intro What is 3710 as binary number ? Æ100101 What is –3710 as binary number ? Æ??? New representation needed Slide 3 Fundamentals of Computer Engineering 1 Exercise 7 Sign-Magnitude Representation Most Significant Bit is sign bit 0 Æ positive 1 Æ negative ? all t a -3710 y=bi-ts1001012 n t m-a 0100101 en = i c w fi Ho uf ion s =In- m0000100101 = ?? at Æ or inf Slide 4 2 Fundamentals of Computer Engineering 1 Exercise 7 Excursion: Representations For every sensible conversion to a signed binary system we need to know • the representation system • the number of bits (=data word length) Slide 5 Fundamentals of Computer Engineering 1 Exercise 7 Sign-Magnitude – Representation Proper question: What is –3710 as a 8 bit SignMagnitude number ? -3710 = - 0010 01012 = 1010 01012,SM Slide 6 3 Fundamentals of Computer Engineering 1 Exercise 7 Sign-Magnitude: Range Sign Highest Smallest 2n-1-1 Magnitude 0 1111….111 -(2n-1-1) 1 1111…111 1 bit n-1 bits Range of 4 bit SM numbers: [-7, 7] 8 bit SM numbers: [-127,+127] etc Slide 7 Fundamentals of Computer Engineering 1 Exercise 7 1s complement What is –3710 as 8 bit 1s complement? -3710 = - 0010 01012 Invert each single bit = 1101 1010 MSB indicates the sign but is not the same as SM‘s sign bit! Æ Magnitude cannot be read directly Slide 8 4 Fundamentals of Computer Engineering 1 Exercise 7 1s complement: Range MSB Highest 2n-1-1 Smallest 0 111…1111 -(2n-1-1) 1 000…0000 1 Range of 4 bit 1s compl. : 8 bit 1s compl. : etc n-1 [-7, 7] [-127,+127] Slide 9 Fundamentals of Computer Engineering 1 Exercise 7 2s complement What is –3710 as 8 bit 2s complement? -3710 = - 0010 01012 Invert each single bit = 1101 1010 + 1 = 1101 1011 MSB indicates the sign but is not the same as SM‘s sign bit! Slide 10 5 Fundamentals of Computer Engineering 1 Exercise 7 2s complement: Range MSB Highest 2n-1-1 0 111…1111 Smallest -2n-1 1 000…0000 1 n-1 Range of 4 bit 2s compl. : 8 bit 2s compl. : etc [-8, 7] [-128,+127] Slide 11 Fundamentals of Computer Engineering 1 Exercise 7 Signed Number = Negative Number? A signed number CAN be negative, but it can also bear the sign „+“ ! -1 -2 -3 +0 0000 +1 0001 0010 1101 -4 1100 -5 1011 -6 1111 1110 1010 -7 + 0011 +3 0 100 = + 4 0100 +4 1 100 = - 4 0101 1001 +2 +5 Both are 2s Complement numbers ! - 0110 1000 -8 0111 +6 +7 Slide 12 6 Fundamentals of Computer Engineering 1 Exercise 7 Interpret Signed Binary Numbers Which decimals could N= 101 10002 correspond to ? 7 bit SM: 101 1000 = - 24 7 bit 1s c.: 101 1000 = - 010 0111 = - 39 7 bit 2s c.: 101 1000 = - 010 1000 = - 40 All representations 2537 bit 2s compl.: with more then 7 bit 101 1000 1000 = = 0..0101 0..0101 1000 1000 = = 88 88 101 2530 zeros Slide 13 Fundamentals of Computer Engineering 1 Exercise 7 Choosing a proper system (1) Which representation system is sensible for a voltage measurement device that has a range from - 2 V to 2 V with a sensitivity of 1 mV ? Slide 14 7 Fundamentals of Computer Engineering 1 Exercise 7 Choosing a proper system (2) Numbers needed: -2000 mV to 2000 mV Æ2n-1-1 >= 2000 Æn-1 >= ld 2001 Æn-1 >= 10.9966 Æn = 12 Range: [-2047,+2047] (SM, 1s Compl.) Slide 15 Fundamentals of Computer Engineering 1 Exercise 7 Floating Point Format (1) Problem: General Format needed that can store 0.00000234 - very small!! as well as 129,802,323,443,123 - very big!! Slide 16 8 Fundamentals of Computer Engineering 1 Exercise 7 Floating Point Format (2) Solution: Use Exponential Form: 0.00000234 = 2.34 • 10-6 129,802,323,443,123 = 1.29802323443123 • 1014 Slide 17 Fundamentals of Computer Engineering 1 Exercise 7 Floating Point Format (3) Similar in binary: 0.001001101 = 1.001101 • 2-3 1101 1100 001 = 1.101 1100 0011 • 211 Slide 18 9 Fundamentals of Computer Engineering 1 Exercise 7 Floating Point Format (4) In general : Every number can be written as ± M • BE M Æ Mantissa B Æ Base E Æ Exponent Floating Point Format!! Information to be stored: how to store the sign, M, B and E ? ÎMany choices Slide 19 Fundamentals of Computer Engineering 1 Exercise 7 IEEE Floating Point Numbers (1) NIEEE floating point = ± M • 2E Sign Exponent 0 0111 1100 1 bit 8 bit Mantissa 0100 0000 0000 0000 0000 000 23 bit IEEE Single Precision Floating Point Format (32 bit) Slide 20 10 Fundamentals of Computer Engineering 1 Exercise 7 IEEE Floating Point Numbers (2) - the sign Sign bit: 0 Æ positive 1 Æ negative Slide 21 Fundamentals of Computer Engineering 1 Exercise 7 IEEE Floating Point Numbers (4) - the mantissa Trick to save one bit: Mantissa is always M = 1.m 1<=M<102 for definition ... float the point ... ÆJust store the fraction m Hidden Bit ÆStore 24 bit in 23 “slots“ Slide 22 11 Fundamentals of Computer Engineering 1 Exercise 7 IEEE Floating Point Numbers (3) - the exponent Negative Exponents ? Æ Offset of 127 (Excess-127 representation) Example: Exponent is E = - 4 Store Characteristic C = E + 127 = 123 E = C - 127 8 bit Characteristic Æ RangeC = [0,255] Æ RangeE = RangeC - 127 = [-127,128] Slide 23 Fundamentals of Computer Engineering 1 Exercise 7 Floating Point Example (1) Example : Conversion of IEEE single precision floating point number to decimal number. Sign 0 Characteristic Mantissa 00110 11110 0010 0000 0000 0000 0000 0000 11 1100 0100 0000 0000 0000 0000 000 Sign : 0 Æ Positive Characteristic : 0 1 1 1 1 1 0 02 = 124 = - 3 + 127 Æ Exponent = - 3 Mantissa ( hidden bit ) : 1 . 0 1 0 0 0000 0000 0000 0000 000 -2 -1 -3 Number :001...1 0 00 1 01 0 1 1 0 0 0 1 010 00 0 010 000 000 0 000 000 000 00 0 000 000 000 00 0 000 000 000 00 0 0000 0000 0000 000 xx 2 2-0 Decimal number : 0 . 0 0 1 0 12 = 0.1562510 Slide 24 12 Fundamentals of Computer Engineering 1 Exercise 7 Floating Point Example (2) Example: Conversion of decimal number to IEEE single precision floating point number. - 9.8281210 Æ IEEE 32-bit floating point 9.8281210 = 1 0 0 1 . 1 1 0 1 0 011 1111 1111 1 0 1 02 1233 .011111 01 01011 1 000011 0 x0 = 10 .. .0000 01 1 00 0 11 00 11001111111111 1111111111111111 11111111 x x2 x222 Characteristic : 127 + 3 = 13010 = 1 0 0 0 0 0 1 02 Sign Characteristic 1 1000 0010 Sign bit : 1 ( negative ) Mantissa 0011 1010 0111 1111 1111 010 Slide 25 Fundamentals of Computer Engineering 1 Exercise 7 Floating Point Example Sign Characteristic 1 1000 0010 Mantissa 0011 1010 0111 1111 1111 010 1100 0001 0001 1101 0011 1111 1111 1 010 C 1 1 D 3 F F A -9.8281210 in IEEE 32 Bit Single Precision Floating Point, visualized Hexadecimal -9.8281210 Æ C1 1D 3F FA Slide 26 13