Computer Arithmetic Appendix A 18-347 Fall 2003 Lec.03-58
Intro Computer Arithmetic Computers use the binary system Easy to implement in electronics: 1 is 1V, 0 is 0V Easy to implement with switches (transistors!) In theory could do other number systems (egbase 10 with 10 voltage levels but would be unmanageable) for LOGIC and ARITHMETIC: Binary numbers and arithmetic All calculations use binary numbers Integers (positive and negative) Fixed and floating point numbers Boolean Logic Algebra Logic functions between TRUE or FALSE values AND, OR, NOT, EXCLUSIVE OR, NOT-AND. Boolean Algebra deals with LOGIC What makes computer arithmetic interesting however is that it is done in a limited (fixed) number of binary digits (8, 16, 32, 64) 18-347 Fall 2003 Lec.03-59
Radix Number Systems (1) The general form of a decimal number. 18-347 Fall 2003 Lec.03-60
Radix Number Systems (2) The number 2001 in binary, octal, and hexadecimal. 18-347 Fall 2003 Lec.03-61
Radix Number Systems (3) (K) β = (K ν-1 K ν-2 K i K 0. K -1 K -2 K -μ ) β K ν-1 : Mostsignificantbit(MSB) K -μ : Leastsignificantbit(LSB) K ν-1 K ν-2 K i K 0 :Integer part K -1 K -2 K -μ :fractional part 18-347 Fall 2003 Lec.03-62
Conversion from One Radix to Another(1) Decimal numbers and their binary, octal, and hexadecimal equivalents. 18-347 Fall 2003 Lec.03-63
Conversion from One Radix to Another(2) Decimal numbers and their binary, octal, and hexadecimal equivalents. 18-347 Fall 2003 Lec.03-64
Conversion from One Radix to Another(3) Examples of octal-to-binary and hexadecimal-to-binary conversion. 18-347 Fall 2003 Lec.03-65
Conversion from One Radix to Another(4) Examples of octal-to-binary and hexadecimal-to-binary conversion. 18-347 Fall 2003 Lec.03-66
Conversion from One Radix to Another(5) Conversion of the decimal number 1492 to binary by successive halving, starting at the top and working downward. For example, 93 divided by 2 yields a quotient of 46 and a remainder of 1, written on the line below 18-347 Fall 2003 Lec.03-67
Conversion from One Radix to Another(6) Conversion of the binary number 101110110111 to decimal by successive doubling, starting at the bottom. Each line is formed by doubling the one below it and adding the corresponding bit. For example, 749 is twice 374 plus the 1 bit on the same line as 749. 18-347 Fall 2003 Lec.03-68
Why this works? Given a number in base 10 (Β) 10 what is its represnetation (A) β in baseβ? Assume(Β) 10 isaninteger: (Β) 10 =(A) β =(A ν-1 A ν-2 Α 0 ) β (Β) 10 =(A) β =A ν-1 xβ ν-1 +A ν-2 xβ ν-2 +.+A 1 xβ 1 +A 0 Ifwedivide(Β) 10 byβthen: ((Β) 10 /β)= (A ν-1 xβ ν-2 +A ν-2 xβ ν-3 +.+A 1 )+(A 0 /β) ((Β) 10 /β)=int((β)10/β)+fraction((β) 10 /β) whereint:istheintegerpartof((β) 10 /β). FRACTION:isthefraction((Β) 10 /β). 18-347 Fall 2003 Lec.03-69
Why this works (A 0 =REM((Β) 10 /β) WhereREM:remainder(Β) 10 /β IfwerepeatthesamestartingnowwithINT((Β) 10 /β)thenthe nextremainderis A 1 andthenextintegerpartis A ν-1 xβ ν-3 +A ν-2 xβ ν-4 +.+A 2 Repeating the same until there is no integer part gives all the digitsof(a) β 18-347 Fall 2003 Lec.03-70
Basic Binary Arithmetic (1) Binary Addition Binary Multiplication 0+0=0 0x0=0 0+1=1 0x1=0 1+0=1 1x0=0 1+1=10 1x1=1 Addition of two POSITIVE integers Α=(10111010) 2 =(186) 10 and Β=(110111) 2 =(55) 10 11111 (carry) 10111010 110111 11110001 =(241) 10 18-347 Fall 2003 Lec.03-71
Basic Arithmetic(2) 101100 multiplicand 1011 multiplier 101100 101100 Partial products 000000 101100 111100100 product (101100) 2 x(1011) 2 =44x11=484=(111100100) 2 18-347 Fall 2003 Lec.03-72
Basic Arithmetic (3) Binary Subtraction 0-0=0 1-0=1 1-1=0 0-1=1 Borrowa1fromthenexthigherposition 0111 (borrow) 10000 101-1011 1010 1=1001 110010 101=101101 1101 100101=-11000 18-347 Fall 2003 Lec.03-73
Basic Arithmetic(4) BinaryDivision (100101) 2 /(101) 2 Dividend 100101 101 Divisor 101 0111 Quotient 1000 101 0111 101 10 Remainder (100101) 2 /(101) 2 =(111) 2 andremainder(10) 2 37/5=7=(111) 2 andremainder2=(10) 2 18-347 Fall 2003 Lec.03-74
Summary: Basic Arithmetic Calculations same as in base-10 system BUT: Addition is very easy and can be implemented extremely fast Multiplication (based on addition) is also straightforward and can be done also very fast Subtraction is messy and slow Division based on subtraction also a big pain Question: how do we represent negative numbers given a limited (fixed) number of digits? 18-347 Fall 2003 Lec.03-75
Representing Negative Numbers Sign bit representations: Signed magnitude One s complement Two s complement In all these the MSB is also the sign bit (0 means positive, 1 means negative) Other representations: Excess 2 m 1 e.g., for 8 bits subtract 128 to get the number 18-347 Fall 2003 Lec.03-76
Signed Magnitude 18-347 Fall 2003 Lec.03-77
Signed Magnitude Trouble is that sign and magnitude must be treated separately Magnitudes MUST BE COMPARED to do the correct operation on them (depending on the signs ) 18-347 Fall 2003 Lec.03-78
One s & Two s complement Idea: represent negative numbers (given a fixed number of digits) with their complements: Α: Integer number n digits (represented in base b) Α c =basebcomplementofα = b n A in decimal 10 s complement in binary Two s complement Α c-1 = baseb-1complementofα= b n A 1 in decimal 9 s complement in binary One s complement 18-347 Fall 2003 Lec.03-79
One s & Two s complement (2) Two scomplement:α c =2 n A e.g.for4bits: complementof0001is2 4 0001=10000 0001=1111 One scomplementα c-1 =(b n 1) A e.g.for4bits: complementof0001is(2 4 1) 0001=1111 0001=1110 18-347 Fall 2003 Lec.03-80
One s & Two s complement (3) One scomplement change1to0and0to1 IF we compute one s complement then two s complement differsby1:add1toone scomplement Alternativewaytocomputetwo scomplement: Start from the rightmost (least significant bit) keep all the 0 s and the first 1. Reverse all the digits to the LEFT of the first 1. 18-347 Fall 2003 Lec.03-81
One s & Two s complement(4) ΑandΒarendigitnumbers:compute C=Β A Β+Α c = B+(b n A)=b n +(B A) IfΒ>Athen B Aisapositivenumber and the above result is C (n digits) but with an additional 1 on the left(i.e., in position n+1) If Α>Bthen: B Aisnegativeandcanbewrittenas (A B) and the equation becomes Β + Α c = b n (Α Β) which is the two s complement representation of the result! 18-347 Fall 2003 Lec.03-82
One s & Two s complement(5) Examples Two s complement additions in 4 bits +3 0011-2 1110 +4 +0100 + -6 +1010 +7 0111-8 1 1000 +6 0110 +4 0100 +-3 +1101 +-7 +1001 +3 1 0011-3 1101 18-347 Fall 2003 Lec.03-83
One s & Two s complement(6) Difference in one s complement and two s complement additions: 18-347 Fall 2003 Lec.03-84
Negative Binary Numbers (1) Negative 8-bit numbers in four systems. 18-347 Fall 2003 Lec.03-85
Negative Binary Numbers (2) Negative 8-bit numbers in four systems. 18-347 Fall 2003 Lec.03-86
Overflow Overflow means that a number cannot be represented In Two s complement addition: If numbers are of opposite signs overflow cannot occur If numbers are of the same sign and the result is of different sign the overflow! Rule: carry in to sign bits!= carry out of sign bits 18-347 Fall 2003 Lec.03-87
Summary Unsignednumbers Signednumbers Signmagnitude One scomplement Two scomplement Excess2 (m-1) Stillonlyintegers Howtorepresentfractionsetc? Useadecimalpoint Fixedpoint Floating point 18-347 Fall 2003 Lec.03-88
Fixed Point Fixedpointnumbers:dddd.dd Aspecificnumberofdigitstorepresent<1 Butstillverysmallrange Usedmostlyinembeddedsystemswhentherangeofnumbers is known 18-347 Fall 2003 Lec.03-89
Floating Point Numbers 18-347 Fall 2003 Lec.03-90
Floating Point Numbers 18-347 Fall 2003 Lec.03-91
Floating Point Numbers 18-347 Fall 2003 Lec.03-92
Floating Point Numbers 18-347 Fall 2003 Lec.03-93
Floating Point Numbers 18-347 Fall 2003 Lec.03-94
Floating Point Numbers 18-347 Fall 2003 Lec.03-95
Floating Point Numbers 18-347 Fall 2003 Lec.03-96
Floating Point Numbers 18-347 Fall 2003 Lec.03-97
Floating Point Numbers 18-347 Fall 2003 Lec.03-98