binary arithmetics & operations · with the same number of digits. ... (n-1)-bit binary paern...

29
Binary Arithmetics & Operations

Upload: others

Post on 30-May-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinaryArithmetics&Operations

Page 2: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinaryArithmetic•  Addi$on•  Subtrac$on•  Mul$plica$on•  Division

Page 3: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Operations•  Complement’sofanumber(DiscussedinBinaryOpera$ons)•  Genera$ngBinaryCodedDecimalsandExcess-3codes•  ShiFOpera$ons•  Arithme$cShiFLeF•  Arithme$cShiFRight•  LogicalShiFLeF•  LogicalShiFRight

•  Floa$ngPointRepresenta$on

Page 4: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinaryAddition•  1+1=0plusacarryof1•  0+1=1•  1+0=1•  0+0=0

Page 5: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinaryAdditionClassExample:Addfollowingtwobinarynumbers:

101001100111

Page 6: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinarySubtraction•  0–0=0•  1–0=1•  1–1=0•  0–1=1withborrowof1

Page 7: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinarySubtraction•  Toperformasubtrac$on•  Alignthetwonumbersasyouwouldindecimalsubtrac$on.•  Appendleadingzerosifnecessarytorepresentbothnumberswiththesamenumberofdigits.

•  Applytwo'scomplementtothesecondterm•  Addthecomplementednumbertothefirstterm.•  Thesuminthepreviousstepshouldhaveonemoredigitthanyoustartedwith

Page 8: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Complementsofanumber•  Complementsareusedindigitalcomputersforsimplifying

thesubtrac$onopera$onandforlogicalmanipula$ons•  2typesforeachbase-rsystem

1)  r’scomplement(Radixcomplement)2)  (r-1)’scomplement(DiminishedradixComplement)

Page 9: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Complementsofanumber•  Referredtoasr’scomplement•  Ther’scomplementofNisobtainedas(rn)-Nwhere r=baseorradix n=numberofdigits N=number

Page 10: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

DiminishedRadixComplement•  Inthebinarynumbersystemr=2thenr-1=1sothe1’scomplementofNis(2n-1)-N

•  Whenabinarydigitissubtractedfrom1,theonlypossibili$esare1-0=1or1-1=0

Therefore,1’scomplementofabinarynumeralisformedbychanging1’sto0’sand0’sto1’s.

Page 11: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Complementsofanumber•  Givethe10’scomplementforthefollowingnumber a.583978 b.5498

Solu$on: a.N=583978 n=6 106-583978 1,000,000–583978=416022 b.N=5498 n=4 104-5498 10,000–5498=4502

Page 12: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

One’sComplement•  Themostsignificantbit(msb)isthesignbit,withvalueof0represen$ngposi$veintegersand1represen$ngnega$veintegers.•  Theremainingn-1bitsrepresentsthemagnitudeoftheinteger,asfollows:•  forposi$veintegers,theabsolutevalueoftheintegerisequalto"themagnitudeofthe(n-1)-bitbinarypadern".•  fornega$veintegers,theabsolutevalueoftheintegerisequalto"themagnitudeofthecomplement(inverse)ofthe(n-1)-bitbinarypadern"(hencecalled1'scomplement).

Page 13: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

One’sComplement•  Example1:Supposethatn=8andthebinaryrepresenta$on01000001.Signbitis0⇒posi$veAbsolutevalueis1000001=65Hence,theintegeris+65•  Example2:Supposethatn=8andthebinaryrepresenta$on10000001.Signbitis1⇒nega$veAbsolutevalueisthecomplementof0000001B,i.e.,1111110B=126Hence,theintegeris-126

Page 14: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Two’sComplement• Again,themostsignificantbit(msb)isthesignbit,withvalueof0represen$ngposi$veintegersand1represen$ngnega$veintegers.•  Theremainingn-1bitsrepresentsthemagnitudeoftheinteger,asfollows:•  forposi$veintegers,theabsolutevalueoftheintegerisequalto"themagnitudeofthe(n-1)-bitbinarypadern".•  fornega$veintegers,theabsolutevalueoftheintegerisequalto"themagnitudeofthecomplementofthe(n-1)-bitbinarypadernplusone"(hencecalled2'scomplement).

Page 15: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Two’sComplement•  Example1:Supposethatn=8andthebinaryrepresenta$on00000000.Signbitis0⇒posi$veAbsolutevalueis0000000=0Hence,theintegeris+0•  Example2:Supposethatn=8andthebinaryrepresenta$on11111111.Signbitis1⇒nega$veAbsolutevalueisthecomplementof1111111Bplus1,i.e.,0000000+1=1Hence,theintegeris-1

Page 16: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

ExampleSubtractionBinarySubtractionStep1

Page 17: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinarySubtractionStep2

Page 18: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinarySubtractionStep3

Page 19: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinarySubtractionStep4

Page 20: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinarySubtractionStep5

Page 21: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinaryMultiplication

•  0x0=0•  0x1=0•  1x0=0•  1x1=1

Page 22: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinaryMultiplication

•  ClassExample•  Mul$plyfollowingtwobinarynumbersanddiscusstheresult:•  101•  100

Page 23: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinaryOperationsBinaryCodes(BCD&Excess-3)DecimalDigit (BCD)

8421Excess-3

0 0000 0011

1 0001 0100

2 0010 0101

3 0011 0110

4 0100 0111

5 0101 1000

6 0110 1001

7 0111 1010

8 1000 1011

9 1001 1100

In a digital system, it may sometimes represent a binary number, other times some other discrete quantity of information

Page 24: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

BinaryOperations•  ShiFopera$ons•  Itistheopera$onofshiFingabitstringtotherightorleF.

Page 25: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

ArithmeticShiftArithme$cShiFisanopera$onofshiFingabitstring,exceptforthesignbit.Example:ShiFbitsby1

ALS 1 1 1 1 1 0 1 0 1 1 1 1 0 1 0 0

ARS 1 1 1 1 1 0 1 0 1 1 1 1 1 1 0 1

Sign bit

Insert a zero in the vacated spot

Sign bit overflow overflow

Page 26: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

LogicalShift

Page 27: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Floatingpointrepresentation•  Arealnumberisrepresentedinexponen$alform(a=+-mxre)

1bit 8bits 23bits(singleprecision)

0 10000100 11010000000000000000000

Sign Exponent Man$ssa

Radix point

Page 28: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Floatingpointrepresentation•  Steps•  Indicatenumberwheternega$veorposi$ve•  NormalizeTheNumber(1.10000100101000)•  FindExponentpart(powerofn)•  Thenputinthefollowingform:

•  Becarefulabouttheexponentpart(E-127)•  ClassExample

1bit 8bits 23bits(singleprecision)

0 10000100 11010000000000000000000

Sign Exponent Man$ssa

Page 29: Binary Arithmetics & Operations · with the same number of digits. ... (n-1)-bit binary paern plus one" (hence called 2's complement). Two’s Complement • Example 1: Suppose that

Floatingpointrepresentation