You are on page 1of 35

ComputerArithmetic

y Numbersystemsandbasicarithmeticoperations y Unconventionalfixedpointnumbersystems y Sequential q algorithms g formultiplication p anddivision y Floatingpointarithmetic y Algorithms Al ith for f fast f taddition dditi y Highspeedmultiplication

TheBinaryNumberSystem
y Inconventionaldigitalcomputers integers

representedasbinarynumbersoffixedlengthn y Anorderedsequence(xn1, xn2,, x1, xo)ofbinarydigits y Eachdigitxi (bit)is0 or1 y The h above b sequencerepresentsthe h integervalue l X
X = x n 1 2
n 1

+ x n 2 2

n 2

+ ... + x 1 2 + x 0 = x i 2i
i =1

n 1

y Upper pp caselettersrepresent p numericalvaluesor

sequencesofdigits y Lowercaseletters, ,usually yindexed, ,represent p individual digits

RadixofaNumberSystem
y Theweightofthedigitxi isthei th powerof2 y 2istheradix ofthebinarynumbersystem y Binary Bi numbers b areradix di 2 numbers b allowed ll ddi digits i are

0,1 y Decimalnumbersareradix10 numbers alloweddigits are0,1,2,,9 y Radixindicatedinsubscript p asadecimalnumber y Example:


y (101)10 decimalvalue101 y (101)2 decimalvalue5

RangeofRepresentations
y Operandsandresultsarestoredinregistersoffixed

lengthn finitenumberofdistinctvaluesthatcan berepresentedwithinanarithmeticunit y Xmin ;Xmax smallestandlargestrepresentable values y [Xmin,Xmax] range oftherepresentablenumbers y AresultlargerthenXmax orsmallerthanXmin incorrectlyrepresented y Thearithmeticunitshouldindicatethatthe generatedresultisinerror anoverflow indication

Example OverflowinBinarySystem
y Unsignedintegerswith5 binarydigits(bits) y Xmax = (31)10 =(11111)2 Xmin = (0)10 =(00000)2 y IncreasingXmax by1 =(32)10=(100000)2 y 5bit representation t ti yielding i ldi (00000) ( )2=(0) ( )10 y Ingeneral y Anumber b X notin i the h range[Xmin,Xmax]=[0,31] ] [ ]is i representedbyX mod32 y IfX+Y X Y exceeds d Xmax the th result ltis i S =(X+Y)mod d32 y Example: X 1000117

+Y 1001018 1 000113=35mod32
y Result l has h tobe b stored dina5bit b register the h most

significantbit(withweight25 =32)isdiscarded

MachineRepresentationsofNumbers
y Binarysystem oneexampleofanumbersystemthat

canbeusedtorepresentnumericalvaluesinan arithmeticunit y Anumbersystem isdefinedbythesetofvaluesthat eachdigitcanassumeandbyaninterpretationrule thatdefinesthemappingbetweenthesequencesof di i and digits dtheir h i numerical i lvalues l y Typesofnumbersystems y conventional (e.g.,binary,decimal) y unconventional (e.g., g signed g digit g numbersystem) y

ConventionalNumberSystems
y Propertiesofconventionalnumbersystems: y Nonredundant y Everynumberhasauniquerepresentation,thus y Notwosequenceshave h the h samenumerical i lvalue l y Weighted y Asequenceof fweights i h wn1,wn2,...,w1,w0 determines d i thevalueofthentuple xn1,xn2,...,x1,x0 by
X = x iw i
i =1 n 1

y wi weightassignedtoxi digitinith position

y Positional y Theweightwi dependsonlyonthepositioni ofdigitxi y wi =ri

i dRadix di Systems Fixed


y r the th radix di of fthe th number b system t y Conventional numbersystemsarealsocalledfixed

radix systems y Withnoredundancy 0 xi r1 y xi r introduces i t d redundancy d d i into t th thefi fixed dradix di numbersystem y Ifxi r isallowed

x i r = (x i r )r + 1.r
i i

i +1

y twomachinerepresentationsforthesamevalue

( xi+1,xi,...)and (...,

( xi+1+1, (..., +1 xir,...)

R Representation t ti of fMixed Mi dNumbers N b


y Asequence q ofn digits g inaregister g notnecessarily y

representinganinteger y Canrepresent p amixednumberwithafractionalpart p andanintegralpart y Then digitsarepartitionedintotwo k inthe integralpartandm inthefractionalpart(k+m=n) y Thevalueofanntuplewitharadixpointbetweenthe k mostsignificantdigitsandthemleastsignificant digits g
y is
X = x k 1r
k 1

+ x k 2 r

k 2

+ ... + x 1r + x 0 + x 1r + ... + x m r

i = m

k 1

xiri

FixedPointRepresentations
y Radixp pointnotstoredinregister g understoodtobeina

fixedpositionbetweenthek mostsignificantdigitsandthe m leastsignificantdigits


y Thesearecalledfixedpoint representations

y Programmernotrestrictedtothepredeterminedposition

oftheradixp point
y Operandscanbescaled samescalingforalloperands

y Addandsubtractoperationsarecorrect y aX aY=a(X Y) (a scalingfactor) y Correctionsrequiredformultiplicationanddivision y aX X aY=a Y 2 X Y;aX/aY=X/Y X/ Y X/Y y Commonlyusedpositionsfortheradixpoint y rightmostsideofthenumber(pureintegers m=0) m 0) y leftmostsideofthenumber(purefractions k=0)

ULP UnitinLastPosition
y Giventhelengthn oftheoperands,theweightrm

oftheleastsignificantdigitindicatestheposition oftheradixpoint y Unitinthelastposition(ulp) theweightofthe leastsignificantdigit y ulp =rm y Thisnotationsimplifiesthediscussion y Noneedtodistinguishbetweenthedifferent partitionsofnumbersintofractionalandintegral parts
y Radixconversion Integer andFractional

RepresentationofNegativeNumbers
y Fixedpointnumbersinaradixr system y Twoways y ofrepresenting p gnegative g numbers: y Signandmagnitude representation (orsigned

magnitude g representation) p ) y Complementrepresentation withtwoalternatives


y Radixcomplement(two (two's scomplementinthebinary

system) y Diminishedradixcomplement p ( (one'scomplement p in thebinarysystem)

SignedMagnitudeRepresentation
y Signandmagnitudearerepresentedseparately y Firstdigitisthesigndigit,remainingn1 digits y y y y

represent ep ese tthe t emagnitude ag tude Binarycase signbitis0 forpositive,1fornegative numbers Nonbinarycase 0 andr1 indicatepositiveand negativenumbers Only2.rn1 outofthern possiblesequencesareutilized Tworepresentations p forzero p positiveandnegative g
y Inconvenientwhenimplementinganarithmeticunit

whentestingforzero,thetwodifferentrepresentations must tbe b checked h k d

DisadvantageoftheSignedMagnitudeRepresentation
yO Operation ti maydepend d donthe th signs i of fthe th operands d y Example addingapositivenumberX andanegative

number Y :X+(Y) y IfY>X,finalresultis(YX) y Calculation


y switchorderofoperands y performsubtractionratherthanaddition y attachtheminussign

y Asequenceofdecisionsmustbemade, made costingexcess

controllogicandexecutiontime y Thisisavoidedinthecomplementrepresentation methods

ComplementRepresentationsofNegativeNumbers
y Twoalternatives y Radixcomplement(calledtwo two's scomplementinthe binarysystem) y Diminishedradixcomplement(calledone'scomplement inthebinarysystem) y Inbothcomplementmethods positivenumbers

represented t dasin i the th signed i dmagnitude it d method th d y AnegativenumberY isrepresentedbyRY whereR isa constant y Thisrepresentationsatisfies(Y)=Y sinceR(RY)=Y

AdvantageofComplementRepresentation
y Nodecisionsmadebeforeexecutingadditionor

subtraction y Example:XY=X+(Y) y Y isrepresentedbyRY y AdditionisperformedbyX+(RY)=R(YX) y IfY>X,(YX) isalreadyrepresentedasR(YX) y Noneedtointerchangetheorderofthetwo operands

R Requirements i t for f Selecting S l ti R


y IfX>Y theresultisX+(R ( Y)=R+(X ) ( Y) ) insteadofXY

additionalR mustbediscarded y Rse selected ectedtos simplify p yo oreliminate e atethis t scorrection co ect o y Anotherrequirement calculationofthecomplementR Y shouldbesimpleanddoneathighspeed y Definitions:
y Complementofasingledigitxi

x i = (r 1) x i y Complement l of fanntuple l X obtained b dby b complementing l everydigitinthesequencecorrespondingtoX


X = x k 1 , x k 2 ,..., x m

SelectingRinRadixComplementRep. Rep
y y y

y y y y

X + X + ulp p = rk Resultstoredintoaregister oflength g n(=k+m) Mostsignificantdigitdiscarded finalresultiszero Ingeneral,storingtheresultofanyarithmeticoperationinto afixed f dlength l hregisterisequivalent l totaking k the h remainder d afterdividingbyrk r k X = X + ulp l SelectingR=rk : R X=rk X=X+ulp CalculationofRX simpleandindependentofk Thisisradixcomplementrepresentation R=rk discardedwhencalculatingR+(XY) nocorrection neededwhenX+(RY) ispositive(X>Y)

E Example l Twos T C Complement l t


y r=2, ,k=n=4, 4,m=0, ,ulp=2 p 0 =1 y Radixcomplement(calledtwo'scomplementinthebinary

case)ofanumberX =24 X y ItcaninsteadbecalculatedbyX + 1 y 0000 to0111 representpositivenumbers010 to710


y The Th two's ' complement l of f0111 is i 1000+1=1001 it i representsthe h

value(7)10 y Thetwo'scomplement p of0000 is1111+1=10000=0mod24 single g representationofzero

y Eachpositivenumberhasacorrespondingnegativenumber

that th tstarts t t with itha1 y 1000 representing(8)10 hasnocorrespondingpositive number y Rangeofrepresentable numbersis8 X 7

Example AdditioninTwoscomplement
y CalculatingX+(Y) withY>X

3+(5) 00113 + 10115 11102 y Correct C tresult ltrepresented t d i inth thetwo's t ' complement l tmethod th d noneedforpreliminarydecisionsorpostcorrections y Calculating C l l i X+( X (Y) with i hX>Y X Y 5+( (3) ) 01015 + 1101 3 1 00102 y Onlythelastfourleastsignificantdigitsareretained, yielding0010

A2ndAlternativeforR:Diminished RadixComplement p Representation p


y SelectingR asR=rk ulp y Thisisthediminishedradixcomplement

R X = (r k ulp ) X = X y Derivationofthecomplementissimplerthanthe radixcomplement


y Allthedigitcomplements x i canbecalculatedin

p parallel fastcomputation p of x y AcorrectionstepisneededwhenR+(XY) is obtainedandXY ispositive p

Example OnesComplementinBinarySystem
y r=2,k=n=4,m=0,ulp=20 =1 y Diminishedradixcomplement p (called ( one'scomplement p y y y y

inthebinarycase)ofanumberX =(241) X= X Asbefore, ,thesequences q 0000 to0111 represent p the positivenumbers010 to710 Theone one's scomplementof0111 is1000,representing(7)10 Theone'scomplementofzero is1111 two representationsofzero Rangeofrepresentable numbersis7 X 7

ComparingtheThreeRepresentationsinaBinarySystem

Example:RadixComplementDecimalSystem
y Leadingdigit0,1,2,3,4 positive y Leadingdigit5,6,7,8,9 negative y Example n=4 y 0000 to4999 positive y 5000 000 to9999 negative negati e (5000) 000)to 1 y Range 5000 X 4999 y Y=1234 34 y RepresentationofY=1234 y y y y

radixcomplementRY withR=104 RY= Y +ulp Digitcomplement=9 digit ;ulp=1 8765; 8766 representationof Y Y +1=8766 Y =8765 Y+(Y)=1234+8766=104 =0mod104

Th Two's The T ' Complement C l tRepresentation R t ti


y From F nowon,the th system t is: i r=2,k=n, k ulp=1 l y Rangeofnumbersin twoscomplementmethod:

2n1

X 2n1 ulp p( (ulp p=20 =1) )

y Slightlyasymmetric onemorenegativenumber y 2n1 (representedby100)doesnothaveapositive

equivalent y Acomplementoperationforthisnumberwillresultinan overflowindication y Ontheotherhand, hand thereisauniquerepresentationfor0

NumericalValueofaTwosComplementRepresentation
y NumericalvalueXofrepresentation(xn1,xn2,...,x0) in

t o's two scomplement


y Ifxn1=0 X = x i 2i y Ifxn1=1 negativenumber absolutevalueobtained
i =0 n 1

bycomplementingthesequence(i.e.,complementing eachbitandadding1)andaddingaminussign y Example Giventhe4tuple1010 negative complementing 0101+1=0110 valueis 6 original sequenceis 6

DifferentCalculationofNumericalValue
X = x n 1.2
n 1

+ x i 2i
i =0

n 2

y Example 1010 X=8+2=6 y Calculatingthenumericalvalueofones

complement
X = x n 1.(2 (2
n 1

ulp ) + x i 2i
i =0

n 2

y Example p 4tuple p 1001 X=7 7+1=6

Additi and Addition dS Subtraction bt ti


y Insigned g magnitude g representation p y Onlymagnitudebitsparticipateinadding/subtracting sign bitsaretreatedseparately y Carry C out t(or ( borrow b out) t)indicates i di t overflow fl y Example 0 1001 +9

0+ 0111 +7 01 00000=16mod16 y Finalresultpositive(sumoftwopositivenumbers)but wrong y Inbothcomplementrepresentations


y Alldigits,includingthesigndigit,participateintheaddor

subtractoperation y Acarryoutisnotnecessarilyanindicationofanoverflow

Addition/SubtractioninComplementMethods
y Example (twos E l (t complement) l t)
010019 11001 7 1 000102

y Carryoutdiscarded doesnotindicateoverflow

y Ingeneral,ifX andYhaveoppositesigns no

overflowcanoccurregardlessofwhetherthereisa carryoutornot y Examples (two (twos scomplement)

Addition/Subtraction Complement Cont.


y IfX andY havethesamesignandresulthasdifferent

sign overflowoccurs y Examples (twoscomplement)


10111 9 101119
1 0111014 4=18mod3 32

y Carryoutandoverflow
010019 001117 0 1000016=16mod32 y Nocarry youtbutoverflow

Addition/Subtraction OnesComplement
y y y y y y y

Carryout indicatestheneedforacorrectionstep Example addingpositiveX andnegativeY X+(2n ulp )Y=(2 (2n ulp)+(XY) IfX>Y correctresultisXY 2n represents p thecarry youtbit discardedinaregister g oflength g n ResultisXYulp correctedbyaddingulp Example 010019 + 110007
1 00001 p Correction 1ulp 000102

y Thegeneratedcarryoutiscalledendaroundcarry itisan

indicationthata1 shouldbeaddedtotheleastsignificantposition

Addition/Subtraction sComplement One Ones Cont. Cont


y IfX<Y X Y the th result ltXY= Y (YX) is i negative ti y Shouldberepresentedby(2n ulp) (YX) y Thereisnocarryout nocorrectionisneeded y Example

101109 001117 111012 y Noendaroundcarrycorrectionisnecessaryintwo's complementaddition

Subtraction
y In I b both hcomplement l systems subtract b operation, i XY, Y

isperformedbyaddingthecomplementofY toX y In I the h one's ' complement l system

X Y = X +Y
y Inthetwo'scomplementsystem

X Y = X + (Y + ulp p)
y Thisstillrequiresonlyasingleadderoperation,since

ulp isaddedthroughtheforcedcarryinputtothe binaryadder

A ith ti ShiftOperations Arithmetic O ti


y Another h wayof fdistinguishing d h amongthe h three h

representationsofnegativenumbers theinfinite extensions t i t toth theright i htand dleft l ftof fagiven i number b
y Signedmagnitudemethod themagnitudexn2,..., x0 can

b viewed be i dasthe h i infinite fi i sequence ,0,0,{xn2,...,x0},0,0,... y Radixcomplementscheme theinfiniteextensionis ,xn1,xn1,{xn1,...,x0},0,0, (xn1 thesigndigit) y Diminishedradixcomplementscheme thesequenceis , ,xn1,xn1,{xn1, ,..., ,x0},xn1,xn1, ,

Arithmetic h Shift h f Operations Examples l


y 1010., s 1010 11010.0, 11010 0 111010.00 111010 00 allrepresent6 intwo two's y y y y

complement 1001.,11001.1,111001.11 allrepresent 6 inone one's s complement Usefulwhenadding goperands p withdifferentnumbersof bits shorterextendedtolonger Rulesforarithmeticshiftoperations:leftandrightshift areequivalenttomultiplyanddivideby2,respectively TwoscomplementOnescomplement

Sh.L{00110=6}=01100=12 Sh L{ } Sh L{ Sh.L{11001= 6}=10011= } 12 Sh.R{00110=6}=00011=3 Sh.R{11001=6}=11100=3 y Sh.L{11010=6}=10100=12 y Sh.R{11010=6}=11101=3

You might also like