cs64 lecture2 binaryarithmetic · lecture outline • review of positional notation, binary logic...

36
Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB

Upload: others

Post on 12-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BinaryArithmetic

CS64:ComputerOrganizationandDesignLogicLecture#2Fall2018

ZiadMatni,Ph.D.

Dept.ofComputerScience,UCSB

Page 2: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

AdministrativeStuff

•  Theclassisfull–IwillnotbeaddingmorepplL

•  Didyoucheckoutthesyllabus?•  Didyoucheckouttheclasswebsite?•  DidyoucheckoutPiazza(andgetaccesstoit)?•  Didyougotolabyesterday?•  Doyouunderstandhowyouwillbesubmittingyourassignments?

10/3/18 Matni,CS64,Fa18 2

Page 3: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

LectureOutline

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

10/3/18 Matni,CS64,Fa18 3

Page 4: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

What’sinaNumber?

642

Whatisthat???

Well,whatNUMERICALBASEareyouexpressingitin?

10/3/18 Matni,CS64,Fa18 4

Page 5: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

PositionalNotationofDecimalNumbers

10/3/18 Matni,CS64,Fa18 5

642inbase10(decimal)canbedescribedin“positionalnotation”as:

6x100=600

4x10=402x1=2=642inbase10

6x102=+4x101=+2x100=

6 4 2100 10 1

642(base10)=600+40+2

Page 6: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

NumericalBasesandTheirSymbols

•  Howmany“symbols”or“digits”doweuseinDecimal(Base10)?

•  Base2(Binary)?•  Base16(Hexadecimal)?

•  BaseN?

10/3/18 Matni,CS64,Fa18 6

Page 7: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

EachdigitgetsmultipliedbyBN

Where: B=thebase N=thepositionofthedigit

Example:giventhenumber613inbase7:Numberindecimal=6x72+1x71+3x70=304

PositionalNotation

10/3/18 Matni,CS64,Fa18 7

Thisishowyouconvertanybasenumberintodecimal!

Page 8: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

PositionalNotationinBinary

10/3/18 Matni,CS64,Fa18 8

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

Page 9: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

ConvenientTable…HEXADECIMAL BINARY

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

10/3/18 Matni,CS64,Fa18 9

HEXADECIMAL(Decimal)

BINARY

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

Page 10: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

AlwaysHelpfultoKnow…N 2N

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

10/3/18 Matni,CS64,Fa18 10

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

Page 11: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

ConvertingBinarytoOctalandHexadecimal

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

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

10/3/18 Matni,CS64,Fa18 11

Page 12: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

ConvertingBinarytoOctalandHexadecimal

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

10/3/18 Matni,CS64,Fa18 12

2 4 6

10 6

246inoctal

A6inhexadecimal

Page 13: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

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

24110/3/18 Matni,CS64,Fa18 13

Page 14: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

In-ClassExercise:ConvertingDecimalintoBinary&Hex

Convert54(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)

10/3/18 Matni,CS64,Fa18 14

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

Page 15: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BinaryLogicRefresherNOT,AND,OR

X NOTXX

0 11 0

10/3/18 Matni,CS64,Fa18 15

X Y XORYX||YX+Y

0 0 00 1 11 0 11 1 1

X Y XANDYX&&Y

X.Y0 0 00 1 01 0 01 1 1

Page 16: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BinaryLogicRefresherExclusive-OR(XOR)

10/3/18 Matni,CS64,Fa18 16

X Y XXORYXOY

0 0 00 1 11 0 11 1 0

+

Theoutputis“1”onlyiftheinputsareopposite

Page 17: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BitwiseNOT

•  SimilartologicalNOT(!),exceptitworksonabit-by-bitmanner

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

10/3/18 Matni,CS64,Fa18 17

Page 18: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

Exercises

•  Sometimeshexadecimalnumbersarewritteninthe0xhhnotation,soforexample: Thehex3Bwouldbewrittenas0x3B

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

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

10/3/18 Matni,CS64,Fa18 18

Page 19: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BitwiseAND

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

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

=0001

10/3/18 Matni,CS64,Fa18 19

Page 20: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

Exercises

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

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

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

•  Notehow&canbeusedasa“masking”function

10/3/18 Matni,CS64,Fa18 20

Page 21: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BitwiseOR

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

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

=1101

10/3/18 Matni,CS64,Fa18 21

Page 22: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

Exercises

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

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

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

10/3/18 Matni,CS64,Fa18 22

Page 23: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BitwiseXOR

•  Worksonabit-by-bitmanner

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

=1100

10/3/18 Matni,CS64,Fa18 23

Page 24: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

Exercises

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

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

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

10/3/18 Matni,CS64,Fa18 24

Page 25: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BitShiftLeft

•  MoveallthebitsNpositionstotheleft•  Whatdoyoudothepositionsnowempty?

– YouputinNnumberof0s

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

•  Whyisthisusefulasaformofmultiplication?

10/3/18 Matni,CS64,Fa18 25

Page 26: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

MultiplicationbyBitLeftShifting

•  VeeeeryusefulinCPU(ALU)design– Why?

•  Becauseyoudon’thavetodesignamultiplier•  Youjusthavetodesignawayforthebitstoshift(whichisrelativelyeasier)

10/3/18 Matni,CS64,Fa18 26

Page 27: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

BitShiftRight•  MoveallthebitsNpositionstotheright,subbing-in

eitherNnumberof0sorN1sontheleft•  Takesontwodifferentforms

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

•  Theinformationcarriedinthelast2bitsislost.•  IfShiftLeftdoesmultiplication,

whatdoesShiftRightdo?–  Itdivides,butittruncatestheresult

10/3/18 Matni,CS64,Fa18 27

Page 28: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

TwoFormsofShiftRight

•  Subbing-in0smakessense•  Whataboutsubbing-intheleftmostbitwith1?

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

•  It’susedfortwos-complementpurposes

– What?

10/3/18 Matni,CS64,Fa18 28

Page 29: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

NegativeNumbersinBinary

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

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

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

–  Baddesignchoice…10/3/18 Matni,CS64,Fa18 29

Page 30: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

TwosComplementMethod

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

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

10/3/18 Matni,CS64,Fa18 30

011010011010

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

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

Page 31: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

Let’sdoitBackwards…BydoingitTHESAMEEXACTWAY!

•  2s-ComplementtoDecimalmethodisthesame!

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

10/3/18 Matni,CS64,Fa18 31

Page 32: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

AnotherViewof2sComplement

10/3/18 Matni,CS64,Fa18 32

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

Page 33: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

AnotherViewof2sComplement

10/3/18 Matni,CS64,Fa18 33

NOTE:Oppositenumbersshowupassymmetricallyoppositeeachotherinthecircle.

NOTEAGAIN:Whenwetalkof2scomplement,wemustalsomentionthenumberofbitsinvolved

Page 34: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

Ranges

•  Therangerepresentedbynumberofbitsdiffersbetweenpositiveandnegativebinarynumbers

•  GivenNbits,therangerepresentedis:0to

and

10/3/18 Matni,CS64,Fa18 34

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

Page 35: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

YOURTO-DOs

•  Assignment#1– DueonFriday!!!

•  Nextweek,wewilldiscussafewmoreArithmetictopicsandstartexploringAssemblyLanguage!– Doyourreadings!

(again:foundontheclasswebsite)

10/3/18 Matni,CS64,Fa18 35

Page 36: CS64 Lecture2 BinaryArithmetic · Lecture Outline • Review of positional notation, binary logic • Bitwise operations • Bit shift operations • Two’s complement • Addition

10/3/18 Matni,CS64,Fa18 36