Computer Engineering Chapter 2 Data Representations Hiroaki Kobayashi 4/21/2008 4/21/2008 1 Agenda in Chapter 2 Translation between binary numbers and decimal numbers Data Representations for Integers Negative numbers Fixed-point and floating-point numbers Tradeoff between Number of Significant Digits and Range Characters Basic Operations defined for individual data representations Addition, subtraction, multiplication, division 4/21/2008 2 1
Fundamental Principle of Computers Binary states on/off controlled by a (mechanical and/or electrical) switch correspond to 0 /1 (or True/False), and all the operations are processed in the binary system for computers ON 1 OFF 0 Switch Quantity (analog data) is convreted into digital (discrete) data in a computer Decimal Analog Binary Digital 4/21/2008 3 Binary Representation Decimal Number Sequence of digits Base-10 system Binary Numbers Sequence of Bits Base-2 system (binary digit) Base-10 system Base-2 system Carry up in counting binary numbers Correspondence between decimal numbers and binary numbers 4/21/2008 4 2
Representation of Binary Numbers in Computers Fixed number of bits used for data representations and operations Computer jargon 1 byte = 8 bits 1 word: a fixed-sized group of bits that are handled together by a computer A 32-bit computer use 32 bits as a word and handles 32-bit data as a unit for operations MSB(Most Significant Bit): leftmost bit of a word/byte LSB(Least Significant Bit): rightmost bit of a word/byte Bit sequence Upper Word Lower 01000111010111010100010101110111 1 word 32-bit for a 32-bit computer 4/21/2008 5 Binary Decimal Conversion n-bit unsigned binary integer number a n-1 a n-2 a 2 a 1 a 0 is converted into a positive integer N= a n-1 2 n-1 + a n-2 2 n-2 + + a 2 2 2 + a 1 2 1 + a 0 2 0 2 n -1 N 0 n-bit unsigned binary fraction number a n-1 a n-2 a 2 a 1 a 0 a -1 a Binary point -2 is converted into a positive decimal fraction N N= a n-1 2 n-1 + a n-2 2 n-2 + a 2 2 2 + a 1 2 1 + a 0 2 0 + a -1 2-1 + a -2 2-2 4/21/2008 6 3
Decimal Binary Conversion Decimal Number N is converted into n-bit binary number a n-1 a n-2 a 2 a 1 a 0 N = a n-1 2 n-1 + a n-2 2 n-2 + a 2 2 2 + a 1 2 1 + a 0 2 0 =2(a n-1 2 n-2 + a n-2 2 n-1 + a 2 2 1 + a 1 )+ a 0 =2N 1 + a 0 =2(2(2N 2 + a 1 )+ a 0 =2(2(2( 2(2N n + a n-1 )+ a n-2 )+... a 1 )+ a 0, Here, N n =0 N i = 2N i+1 + a i, N 0 = N, i=0,1,2,...,n-1 Question: Binary representation of a decimal number N=43?? 4/21/2008 7 Decimal Binary Conversion (Cont d) Decimal fraction number N is converted into n-bit binary fraction number 0.a -1 a -2 a 1-n a -n N= a -1 2-1 + a -2 2-2 + a 1-n 2 1-n + a -n 2 -n = (a -1 + a -2 2-1 + a 2 2 2-n + a 1 2 1-n ) 2-1 = (a -1 +N -1 2-1 ) 2-1 = (a -1 + (a -2 +N -2 2-1 ) 2-1 ) 2-1 = (a -1 + (a -2 +...(a -n +N -n 2-1 ) 2-1 ) 2-1...) 2-1, whre, N -n =0 N -i = a -i-1 + N -i-1 2-1, N 0 = N, i=0,1,2,...,n-1 Question: Binary representation of decimal fraction number 0.75 4/21/2008 8 4
Binary-Decimal Conversion: Examples Binary Translation of Binary Translation of 2X 4/21/2008 9 Base-2 Arithmetic 1-bit binary addition a + b sum carry N-bit addition 1-bit binary subtraction a - b difference borrow N-bit subtraction 4/21/2008 10 5
Multiplication paper&pencil method Multiplication of 1-bit binary numbers a x b product 2n-bit required when multiplying two n-bit numbers (n bits x m bits generates n+m bits) 4/21/2008 11 Division on Binary Numbers Division on n-bit numbers paper&pencil method 2n-bit number (dividend) divided by a n-bit number(divisor) generates a n-bit quotient ;remainder 4/21/2008 12 6
Negative Integers Sing-Magnitude Representation MSB (named the sign bit) is used to indicate whether the number is positive or negative, and the remainder of the number indicates the absolute value (or magnitude) of the number binary 8 decimal binary 8 decimal Features Can represent the numbers from -(2 n-1-1) to +(2 n-1-1) For example, 8-bit can represent -127(11111111) 127(01111111) Easy to understand Two representations of and Special care needed for addition and subtraction Separate handling of sing and magnitude bits for arithmetic operations 4/21/2008 13 Addition on Sing-Magnitude Numbers Rules for X+Y If the signs of X and Y are same X+Y= sign of X X Y, X is the absolute value of X If the signs of X and Y are different If X Y is positive, X+Y= sign of X X Y If X Y is negative, X+Y= sign of Y Y X Rules for X-Y If the signs of X and Y are same, Rule is applied. If the signs of X and Y are different, Rule is applied. 4/21/2008 14 7
Addition on Sing-Magnitude Numbers: Examples Numbers in are decimal representations 4/21/2008 15 Another Representation of Negative Numbers Motivation: number representation for simple and unified operations of addition and subtraction General definition of Complement Complement of N means the number that generates a carry when adding N to its complement E.g., in case of decimal, complement of 3 is 7. Complement number representation simplifies addition and subtraction Example: 1 digit subtraction complement of 3 subtraction result by ignoring a carry 4/21/2008 16 8
Two s Complement Notation Two s complement representation of A in n-bit is 2 n A Allocate an n-bit number for a negative number as its complement so that N (n-bit) + complement number of N (n-bi) =1000 0(n+1-bit) =00000(nbit) Discarding any overflow bits that do not fit in the width (i.e., n-bit) of representation How to obtain the complement of a number Inverting each bit of the unsigned representation of the number and adding 1 to the result 2 n 1 N 2 n Two s complement of 0110 (6)??? MSB is the sign bit. is uniquely defined. (no +0/-0) Unified handling of addition and subtraction 4/21/2008 17 Number Representations Decimal Sign+Magnitude Two s complement N/A Circular N/A 4/21/2008 18 9
Two s Complement Translation Example Two s complement representation of A in n-bit is 2 n A Inverting each bit of the unsigned representation of the number and adding 1 to the result A=2 n - A=(2 n 1 A ) + 1 Obtained by negation of each bit Example 8-bit case Decimal Sign+Mag. Two s Complement 4/21/2008 19 Additions and Subtractions on Complement Numbers n-bit subtractions are processed by a b = a + (-b) = a +(2 n b) 2 n No need to separately handle the sign bit Unified operations of additions and subtractions when using complement numbers Negative results obtained in the compliment notation How to calculate a - b 1. Obtain two s compliment b c of b 2. Result obtained by adding a to b c If there is a carry from MSB (overflow from n-bit), ignore it. Negative results are also represented in the compliment notation. 4/21/2008 20 10
Examples Exercise Calculate the following expressions in 8-bit number representation with two s complements for negative numbers. 1. 9+(-13)= 2. 7-9 = 3. 9-(-13)= 4. -3-4= 4/21/2008 21 Hexadecimal & Octal Numbers Binary representation is simple but need more bits to represent numbers compared to decimal representation. Hexadecimal (base-16) notation 0,1,2,3,4,5,6,7,8,9,A(10), B(11), C(12), D(13), E(14),F(15) Binary used as a hexadecimal digit One hexadecimal digit corresponds to four bits Example, 0x1af9 Octal(base-8) notation 0 1 2 3 4 5 6 7 Hexadecimal A F used as an octal digit Binary One octal digit corresponds to three bits Example, 0o644 Octal 4/21/2008 22 11
BCD Representation Binary-Coded Decimal (BCD) Representation Decimal numbers are presented digit by digit in the binary form 4 bits are needed to represent decimal digit from 0 to 9 No error occur in representation of fraction numbers Decimal BCD 1010 to1111 are not used Redundant coding Example 0101 1000 1001 0001 0011 1000 4/21/2008 23 Additions on BCD Numbers Add 4-bit (BCD digit) by 4-bit from the lower As there is unused range from 1010 to 1111, if a result of addition of each digit is greater than 9, add offset 110(6) for compensation and carry generation >1010 carry Add offset 110 Add offset 110 4/21/2008 24 12
Subtraction on BCD Numbers Subtract 4-bit (BCD digit) by 4-bit from the lower If subtraction of each digit needs a borrow from the upper digit and a subtraction result is greater than 9, subtract offset 110(6) for compensation borrow >1010 Sub offset 4/21/2008 25 Floating-Point Numbers Fixed-point notation Binary point is fixed, e.g., rightmost for integers Easy for arithmetic operations Representable range limited Floating-point notation Numbers are presented by a mantissa (significand) and an exponent, similar to scientific notation Representable range extended Complicated processing needed for arithmetic operations sign n-bit (fixed) Integer part p-bit (fixed) Mantissa (Significand) m-bit (fixed) Fraction part Binary point (fixed) q-bit (fixed) exponent 4/21/2008 26 13
Normalization of Floating-Point Numbers In the floating-point notation, a number is normalized to effectively use mantissa s bits for floating-point representation (without redundant 0 s) mantissa A real number is called normalized, if it is in the form where b is the base number, and E.g. Normalized numbers 1.10001 2 1 (IEEE 745 representation) 3.14 10 0 Non-normalized numbers 3140 10-3, 11.0001 2 0, 0.0314 10 2 4/21/2008 27 Normalization of Non-Normalized Numbers Multiplication and division by the base number (e.g., 2 or 10) and add/sub exponent value by the number of shifted bits. 1. Shift significand bits to left (or right), and 2. Sub(or add) exponent value by the number of shifted bits. Remark Left-shift&sub (or rightshift&add) means 1/2 (or 2x) in the base-2 system Non-normalized numbers 3-bit shift left 5-bit shift right (5-3= 2) -6 + 5= -1 1.101x2 2 1.101x2-1 Normalized numbers 4/21/2008 28 14
IEEE745: Standard Format for Floating-Point Data Representation Single-Precision Format (-1) S (1+significand) 2 (exponent - 127) 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 s (exponent) (significand) 1bit 8 bits 23 bits Double-Precision Format (-1) S (1+significand) 2 (exponent - 1023) 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 s (exponent) (significand) 1bit 11 bits 20 bits (significand)( 32 bits 4/21/2008 29 Summary of IEEE745 Format (Cont d) 4/21/2008 30 15
Features of IEEE 754 Format To pack even more bits into the significand, make the leading 1 bit of normalized binary numbers explicit. 24-bit precision with the 23-bit field for singleprecision, 53-bit precision with 52-bit fileld for doubleprecision Significand is used to represent a fraction between 0 and 1 Sign+magnitude representation for significand Sign bit is separately stored in MSB, allowing a quick test of less than, greater than, or equal to 0 Easily processed by integer comparisons, especially for sorting needed in floating-point operations Biased exponent To compare exponents of two numbers for floating-point operation quickly by integer comparison, exponents are biased so that the most negative exponent is 00 000, and the most positive exponent is 11 111 Biases of 127 for single-precision, 1023 for double-precision De-normalized numbers 0.f 2 Emin ) are representable to minimize underflows Infinity( ) can be handled, e.g., 1/0 returns NaN: Not a Number NaN is used as a special symbol for unrepresentable results obtained by some invalid operations such as 0/0, Almost all computers use IEEE 754 format for floating-point data representation! 4/21/2008 31 Some Examples 1 2-1 in Two s Complement Format 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 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 +1 in Two s Complement Format 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 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2-1 in IEEE754 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 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 +1 in IEEE754 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 0 1 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 0 +0 in IEEE754 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 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 0 0 0 + in IEEE754 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 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4/21/2008 32 16
Floating-Point Addition/Subtraction Addition and subtraction of two numbers m 1 2 e1, m 2 2 e2 (e 1 >e 2 ) Step 1 To align the binary point of the number that has the smaller exponent to the larger exponent, calculate e 1 -e 2 Step 2 The significand of the number with lesser exponent is shifted right, in this case, by (e1-e2) bits Step 3 Add the significands Step 4 Normalization by shifting significands right/left, and increment/ decrement exponents accordingly Step 5 Checking for overflow/underflow Step 6 Rounding if necessary 4/21/2008 33 Overflow and Underflow An overflow or underflow occurs when an operation generates a result that cannot be expressed in the format The bit width of a computer limits the maximum and minimum numbers it can represent in the given format Overflows occur when the result of an operation is too large to represent in the input format E.g., a result obtained by division of a large number by an extremely small number Underflows occur when the result of an operation is too small to represent in the input format E.g., a result obtained by division of a small number by extremely large number A special signal (e.g., NaN of IEEE 754) generated to notify system/ programmer when overflows or underflows occur Abnormal termination of the program. NaN (Not a Number) 4/21/2008 34 17
Floating-Point Addition/Subtraction Addition and subtraction of two numbers m 1 2 e1, m 2 2 e2 (e 1 >e 2 ) Step 1 To align the binary point of the number that has the smaller exponent to the larger exponent, calculate e 1 -e 2 Step 2 The significand of the number with lesser exponent is shifted right, in this case, by (e1-e2) bits Step 3 Add the significands Step 4 Normalization by shifting significands right/left, and increment/ decrement exponents accordingly Step 5 Checking for overflow/underflow Step 6 Rounding if necessary 4/21/2008 35 Rounding Rounding is needed to fit a given number into the fixed significant digits Round to Nearest (default in IEEE745) Rounds to the nearest value; if the number falls midway it is rounded to the nearest value with an even (zero) least significant bit. Round toward Directed rounding towards positive infinity Round toward Representable numbers in n-bit Directed rounding towards negative infinity Round toward 0 Directed rounding towards zero? 1? Result obtained by operation 4/21/2008 36 18
Example of Floating-Point Addition Addition of 0.5 ten and 0.4375 ten Step 0 0.5 ten 1/2 ten 1/2 1 ten =0.1 two =0.1 two 2 0 =1.000 two 2-1 -0.4375 ten =-7/16 ten =-7/2 4 ten =-0.0111 two =-0.0111 two 2 0 =-1.1110 two 2-2 Step 1&2 Exponent alignment -1-(-2)=1: 1bit shift -1.1110 two 2-2 =-0.111 two 2-1 Step 3 addition of significands 1.000 two 2-1 (-0.111 two 2-1) =0.001 two 2-1 Step 4 Normalization 0.001 two 2-1 =0.010 two 2-2 =0.010 two 2-3 =1.000 two 2-4 Step 5 Checking for overflow/ underflow, and rounding No overflow/underflow because of 127-4 -126 The result of addition 1.000 two 2-4 =0.0001000 two =1/2 4 ten =1/16 ten =0.0625 ten 4/21/2008 37 Multiplication/Division of Floating-Point Numbers Multiplication: m 1 2 e1 m 2 2 e2 m 1 m 2 2e1 e2 Step 1 Add exponents of two inputs Step 2 Multiply their significands Step 3 Normalization and overflow/ underflow check of the product Step 4 Rounding Step 5 Sign calculation The sign of the product depends on the signs of the original operands (two inputs) If they are both the same, the sign is positive; otherwise it s negative Division m 1 2 e1 m 2 2 e2 m 1 m 2 2e1 e2 Step 1 Subtract the exponents of dividend from divisor Step 2 Divide significands of dividend by that of divisor Step 3 Normalization and overflow/ underflow check of the quotient Step 4 Rounding Step 5 Sign calculation The sign of the product depends on the signs of the original operands (two inputs) If they are both the same, the sign is positive; otherwise it s negative 4/21/2008 38 19
Example of Floating-Point Multiplication Multiplication Step 1 Add exponents of two inputs Step 2 Multiply their significands Step 3 Normalization and overflow/underflow check of the product Step 4 Rounding Step 5 Sign calculation The sign of the product depends on the signs of the original operands (two inputs) If they are both the same, the sign is positive; otherwise it s negative Example Multiplication of 0.5 ten 1.000 two 2-1 and -0.4375 ten =-1.1110 two 2-2 Step 1-1 + (-2) = -3 Step 2 1.000 two 1.1110 two =1.1110 two Step 3 No underflow/overflow occurs because of 127-3 -126 Step 4 No rounding needed because the product fit into 4 bits Step 5 The product is negative because the signs of two inputs are different. The product is -1.1110 two 2-3 =-0.001110 two -7/2 5 ten =-7/32 ten =-0.21857 ten 4/21/2008 39 Representation of Characters and Symbols Encoding characters based on the alphabetical(or order; Assign a bit sequence to each character An 8-bit code defines 256 characters A16-bit code defines 65536 characters Sorting of characters available by simply comparing numbers defined for characters Smaller numbers assigned for earlier ordered characters in abc Character type of data defined at the programming level. Typical Coding Systems ASCII(American Standard Code for Information Interchange) 7-bit for 128 printable and nonprintable characters Widely used for current computers JIS (Japan Industrial Standard) Kana-kanji extension of ASCII 2 bytes (16 bits) EBCDIC(Extended Binary Coded Decimal Interchange Code) IBM code specially-defined for main-frame computers 4/21/2008 40 20
Lower bits Upper bits 4/21/2008 41 Data Types Used in Computer Numbers Integer Real numbers Fixed-point numbers Floating-point numbers Unsigned numbers Signed numbers Positive numbers Negative numbers Characters String: a sequence of characters All these data represented in a sequence of bits - no difference in appearance, and data type is defined at the programming level 4/21/2008 42 21