cs64 lecture2 binaryarithmeticlecture #2 ziad matni dept. of computer science, ucsb adding this...

Post on 12-Oct-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

BinaryArithmetic

CS64:ComputerOrganizationandDesignLogicLecture#2

ZiadMatni

Dept.ofComputerScience,UCSB

AddingthisClass

•  Theclassisfull–IwillnotbeaddingmorepplL– Evenifothersdrop

4/5/18 Matni,CS64,Sp18 2

LectureOutline

•  Reviewofpositionalnotation,binarylogic•  Bitwiseoperations•  Bitshiftoperations•  Two’scomplement•  Additionandsubtractioninbinary•  Multiplicationinbinary

4/5/18 Matni,CS64,Sp18 3

PositionalNotationinDecimal

4/5/18 Matni,CS64,Sp18 4

Continuing with our example… 642 in base 10 positional notation is:

= 6 x 100 = 600

= 4 x 10 = 40 = 2 x 1 = 2 = 642 in base 10

6x102+4x101+2x100

6 4 2100 10 1

642(base10)=600+40+2

Whatif“642”isexpressedinthebaseof13?

6 x 132 = 6 x 169 = 1014 + 4 x 131 = 4 x 13 = 52 + 2 x 13º = 2 x 1 = 2

PositionalNotationThisishowyouconvertanybasenumberintodecimal!

4/5/18 Matni,CS64,Sp18 5

6 4 2132 131 130

642(base13)=1014+52+2 =1068(base10)

PositionalNotationinBinary

4/5/18 Matni,CS64,Sp18 6

11101 in base 2 positional notation is:

1 x 24 = 1 x 16 = 16 + 1 x 23 = 1 x 8 = 8 + 1 x 22 = 1 x 4 = 4 + 0 x 21 = 1 x 2 = 0 + 1 x 20 = 1 x 1 = 1

So, 11101 in base 2 is 16 + 8 + 4+ 0 + 1 = 29 in base 10

ConvenientTable…HEXADECIMAL BINARY

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

4/5/18 Matni,CS64,Sp18 7

HEXADECIMAL(Decimal)

BINARY

A(10) 1010B(11) 1011C(12) 1100D(13) 1101E(14) 1110F(15) 1111

AlwaysHelpfultoKnow…N 2N

1 22 43 84 165 326 647 1288 2569 51210 1024=1kilobits

4/5/18 Matni,CS64,Sp18 8

N 2N

11 2048=2kb12 4kb13 8kb14 16kb15 32kb16 64kb17 128kb18 256kb19 512kb20 1024kb=1megabits

N 2N

21 2Mb22 4Mb23 8Mb24 16Mb25 32Mb26 64Mb27 128Mb28 256Mb29 512Mb30 1Gb

ConvertingBinarytoOctalandHexadecimal

(oranybasethat’sapowerof2)NOTETHEFOLLOWING:•  Binaryis 1bit•  Octalis 3bits•  Hexadecimalis 4bits

•  Usethe“groupthebits”technique– Alwaysstartfromtheleastsignificantdigit– Groupevery3bitstogetherforbinàoct– Groupevery4bitstogetherforbinàhex

4/5/18 Matni,CS64,Sp18 9

ConvertingBinarytoOctalandHexadecimal

•  Taketheexample:10100110…tooctal:10100110…tohexadecimal:10100110

4/5/18 Matni,CS64,Sp18 10

2 4 6

10 6

246inoctal

A6inhexadecimal

While (the quotient is not zero) 1.  Divide the decimal number by the new base 2.  Make the remainder the next digit to the left in the answer 3.  Replace the original decimal number with the quotient 4.  Repeat until your quotient is zero

Algorithmforconvertingnumberinbase10tootherbases

ConvertingDecimaltoOtherBases

Example:Whatis98(base10)inbase8?

98/8=12R2

12/8=1R4

1/8=0R1

2414/5/18 Matni,CS64,Sp18 11

In-ClassExercise:ConvertingDecimalintoBinary&HexConvert54(base10)intobinaryandhex:•  54/2=27R0•  27/2=13R1•  13/2=6R1•  6/2=3R0•  3/2=1R1•  1/2=0R1

54(decimal)=110110(binary)=36(hex)

4/5/18 Matni,CS64,Sp18 12

Sanitycheck:110110=2+4+16+32=54

BinaryLogicRefresherNOT,AND,OR

X NOTXX

0 11 0

4/5/18 Matni,CS64,Sp18 13

X Y XORYX||YX+Y

0 0 00 1 11 0 11 1 1

X Y XANDYX&&YX.Y

0 0 00 1 01 0 01 1 1

BinaryLogicRefresherExclusive-OR(XOR)

4/5/18 Matni,CS64,Sp18 14

X Y XXORYXOY

0 0 00 1 11 0 11 1 0

+

Theoutputis“1”onlyiftheinputsareopposite

BitwiseNOT

•  SimilartologicalNOT(!),exceptitworksonabit-by-bitmanner

•  InC/C++,it’sdenotedbyatilde:~ ~(1001)=0110

4/5/18 Matni,CS64,Sp18 15

Exercises

•  Sometimeshexadecimalnumbersarewritteninthe0xhhnotation,soforexample: Thehex3Bwouldbewrittenas0x3B

•  Whatis~(0x04)?– Ans:0xFB

•  Whatis~(0xE7)?– Ans:0x18

4/5/18 Matni,CS64,Sp18 16

BitwiseAND

•  SimilartologicalAND(&&),exceptitworksonabit-by-bitmanner

•  InC/C++,it’sdenotedbyasingleampersand:&(1001&0101)=1001 &0101

=0001

4/5/18 Matni,CS64,Sp18 17

Exercises

•  Whatis(0xFF)&(0x56)?–  Ans:0x56

•  Whatis(0x0F)&(0x56)?–  Ans:0x06

•  Whatis(0x11)&(0x56)?–  Ans:0x10

•  Notehow&canbeusedasa“masking”function

4/5/18 Matni,CS64,Sp18 18

BitwiseOR

•  SimilartologicalOR(||),exceptitworksonabit-by-bitmanner

•  InC/C++,it’sdenotedbyasinglepipe:|(1001|0101)=1001 |0101

=1101

4/5/18 Matni,CS64,Sp18 19

Exercises

•  Whatis(0xFF)|(0x92)?– Ans:0xFF

•  Whatis(0xAA)|(0x55)?– Ans:0xFF

•  Whatis(0xA5)|(0x92)?– Ans:B7

4/5/18 Matni,CS64,Sp18 20

BitwiseXOR

•  Worksonabit-by-bitmanner

•  InC/C++,it’sdenotedbyasinglecarat:^(1001^0101)=1001 ^0101

=1100

4/5/18 Matni,CS64,Sp18 21

Exercises

•  Whatis(0xA1)^(0x13)?– Ans:0xB2

•  Whatis(0xFF)^(0x13)?– Ans:0xEC

•  Notehow(1^b)isalways~bandhow(0^b)isalwaysb

4/5/18 Matni,CS64,Sp18 22

BitShiftLeft

•  MoveallthebitsNpositionstotheleft•  Whatdoyoudothepositionsnowempty?

– YouputinN0s

•  Example:Shift“1001”2positionstotheleft1001<<2=100100

•  Whyisthisusefulasaformofmultiplication?

4/5/18 Matni,CS64,Sp18 23

MultiplicationbyBitLeftShifting

•  VeeeeryusefulinCPU(ALU)design– Why?

•  Becauseyoudon’thavetodesignamultiplier•  Youjusthavetodesignawayforthebitstoshift

4/5/18 Matni,CS64,Sp18 24

BitShiftRight•  MoveallthebitsNpositionstotheright,subbing-in

eitherN0sorN1sontheleft•  Takesontwodifferentforms

•  Example:Shift“1001”2positionstotheright1001>>2=either0010or1110

•  Theinformationcarriedinthelast2bitsislost.•  IfShiftLeftdoesmultiplication,

whatdoesShiftRightdo?–  Itdivides,butittruncatestheresult

4/5/18 Matni,CS64,Sp18 25

TwoFormsofShiftRight

•  Subbing-in0smakessense•  Whataboutsubbing-intheleftmostbitwith1?

•  It’scalled“arithmetic”shiftright:1100(arithmetic)>>1=1110

•  It’susedfortwos-complementpurposes

– What?

4/5/18 Matni,CS64,Sp18 26

NegativeNumbersinBinary

•  Soweknowthat,forexample,6(10)=110(2)•  Butwhatabout–6(10)???

•  Whatifweaddedonemorebitonthefarlefttodenote“negative”?–  i.e.becomesthenewMSB

•  So:110(+6)becomes1110(–6)•  Butthisleavesalottobedesired

–  Baddesignchoice…4/5/18 Matni,CS64,Sp18 27

TwosComplementMethod

•  ThisishowTwosComplementfixesthis.•  Let’swriteout-6(10)in2s-Complementbinaryin4bits:

So,–6(10)=1010(2)

4/5/18 Matni,CS64,Sp18 28

011010011010

Firsttaketheunsigned(abs)value(i.e.6)andconverttobinary:

Thennegateit(i.e.doa“NOT”functiononit):Nowadd1:

Let’sdoitBackwards…BydoingitTHESAMEEXACTWAY!

•  2s-ComplementtoDecimalmethodisthesame!

•  Take1010fromourpreviousexample•  Negateitanditbecomes0101•  Nowadd1toit&itbecomes0110,whichis6(10)

4/5/18 Matni,CS64,Sp18 29

AnotherViewof2sComplement

4/5/18 Matni,CS64,Sp18 30

NOTE:InTwo’sComplement,ifthenumber’sMSBis“1”,thenthatmeansit’sanegativenumberandifit’s“0”thenthenumberispositive.

AnotherViewof2sComplement

4/5/18 Matni,CS64,Sp18 31

NOTE:Oppositenumbersshowupassymmetricallyoppositeeachotherinthecircle.

NOTEAGAIN:Whenwetalkof2scomplement,wemustalsomentionthenumberofbitsinvolved

Ranges

•  Therangerepresentedbynumberofbitsdiffersbetweenpositiveandnegativebinarynumbers

•  GivenNbits,therangerepresentedis:0to

and

4/5/18 Matni,CS64,Sp18 32

+2N–1forpositivenumbers–2N-1to+2N-1–1for2’sComplementnegativenumbers

Addition•  Wehaveanelementarynotionofaddingsingledigits,along

withanideaofcarryingdigits–  Example:whenadding3to9,weputforward2andcarrythe1

(i.e.tomean12)

•  Wecanbuildonthisnotiontoaddnumberstogetherthataremorethanonedigitlong

•  Example: 123 + 389

4/5/18 Matni,CS64,Sp18 33

11

215

AdditioninBinary

•  Samemathematicalprincipalapplies

0011+1101

4/5/18 Matni,CS64,Sp18 34

1

00

11

00

3+13

161

1

Exercises

Implementingan8-bitadder:•  Whatis(0x52)+(0x4B)?

– Ans:0x9D,outputcarrybit=0

•  Whatis(0xCA)+(0x67)?– Ans:0x31,outputcarrybit=1

4/5/18 Matni,CS64,Sp18 35

YOURTO-DOs

•  Assignment#1– Lookforitovertheweekendontheclasswebsite– LAB#1isonMONDAY!

•  Nextweek,wewilldiscussmoreArithmetictopicsandstartexploringAssemblyLanguage– Doyourreadings!

(again:foundontheclasswebsite)

4/5/18 Matni,CS64,Sp18 36

4/5/18 Matni,CS64,Sp18 37

top related