Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
BinaryArithmetic
CS64:ComputerOrganizationandDesignLogicLecture#2Fall2018
ZiadMatni,Ph.D.
Dept.ofComputerScience,UCSB
AdministrativeStuff
• Theclassisfull–IwillnotbeaddingmorepplL
• Didyoucheckoutthesyllabus?• Didyoucheckouttheclasswebsite?• DidyoucheckoutPiazza(andgetaccesstoit)?• Didyougotolabyesterday?• Doyouunderstandhowyouwillbesubmittingyourassignments?
10/3/18 Matni,CS64,Fa18 2
LectureOutline
• Reviewofpositionalnotation,binarylogic• Bitwiseoperations• Bitshiftoperations• Two’scomplement• Additionandsubtractioninbinary
10/3/18 Matni,CS64,Fa18 3
What’sinaNumber?
642
Whatisthat???
Well,whatNUMERICALBASEareyouexpressingitin?
10/3/18 Matni,CS64,Fa18 4
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
NumericalBasesandTheirSymbols
• Howmany“symbols”or“digits”doweuseinDecimal(Base10)?
• Base2(Binary)?• Base16(Hexadecimal)?
• BaseN?
10/3/18 Matni,CS64,Fa18 6
EachdigitgetsmultipliedbyBN
Where: B=thebase N=thepositionofthedigit
Example:giventhenumber613inbase7:Numberindecimal=6x72+1x71+3x70=304
PositionalNotation
10/3/18 Matni,CS64,Fa18 7
Thisishowyouconvertanybasenumberintodecimal!
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
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
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
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
ConvertingBinarytoOctalandHexadecimal
• Taketheexample:10100110…tooctal:10100110…tohexadecimal:10100110
10/3/18 Matni,CS64,Fa18 12
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
24110/3/18 Matni,CS64,Fa18 13
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
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
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
BitwiseNOT
• SimilartologicalNOT(!),exceptitworksonabit-by-bitmanner
• InC/C++,it’sdenotedbyatilde:~ ~(1001)=0110
10/3/18 Matni,CS64,Fa18 17
Exercises
• Sometimeshexadecimalnumbersarewritteninthe0xhhnotation,soforexample: Thehex3Bwouldbewrittenas0x3B
• Whatis~(0x04)?– Ans:0xFB
• Whatis~(0xE7)?– Ans:0x18
10/3/18 Matni,CS64,Fa18 18
BitwiseAND
• SimilartologicalAND(&&),exceptitworksonabit-by-bitmanner
• InC/C++,it’sdenotedbyasingleampersand:&(1001&0101)=1001 &0101
=0001
10/3/18 Matni,CS64,Fa18 19
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
BitwiseOR
• SimilartologicalOR(||),exceptitworksonabit-by-bitmanner
• InC/C++,it’sdenotedbyasinglepipe:|(1001|0101)=1001 |0101
=1101
10/3/18 Matni,CS64,Fa18 21
Exercises
• Whatis(0xFF)|(0x92)?– Ans:0xFF
• Whatis(0xAA)|(0x55)?– Ans:0xFF
• Whatis(0xA5)|(0x92)?– Ans:B7
10/3/18 Matni,CS64,Fa18 22
BitwiseXOR
• Worksonabit-by-bitmanner
• InC/C++,it’sdenotedbyasinglecarat:^(1001^0101)=1001 ^0101
=1100
10/3/18 Matni,CS64,Fa18 23
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
BitShiftLeft
• MoveallthebitsNpositionstotheleft• Whatdoyoudothepositionsnowempty?
– YouputinNnumberof0s
• Example:Shift“1001”2positionstotheleft1001<<2=100100
• Whyisthisusefulasaformofmultiplication?
10/3/18 Matni,CS64,Fa18 25
MultiplicationbyBitLeftShifting
• VeeeeryusefulinCPU(ALU)design– Why?
• Becauseyoudon’thavetodesignamultiplier• Youjusthavetodesignawayforthebitstoshift(whichisrelativelyeasier)
10/3/18 Matni,CS64,Fa18 26
BitShiftRight• MoveallthebitsNpositionstotheright,subbing-in
eitherNnumberof0sorN1sontheleft• Takesontwodifferentforms
• Example:Shift“1001”2positionstotheright1001>>2=either0010or1110
• Theinformationcarriedinthelast2bitsislost.• IfShiftLeftdoesmultiplication,
whatdoesShiftRightdo?– Itdivides,butittruncatestheresult
10/3/18 Matni,CS64,Fa18 27
TwoFormsofShiftRight
• Subbing-in0smakessense• Whataboutsubbing-intheleftmostbitwith1?
• It’scalled“arithmetic”shiftright:1100(arithmetic)>>1=1110
• It’susedfortwos-complementpurposes
– What?
10/3/18 Matni,CS64,Fa18 28
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
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:
Let’sdoitBackwards…BydoingitTHESAMEEXACTWAY!
• 2s-ComplementtoDecimalmethodisthesame!
• Take1010fromourpreviousexample• Negateitanditbecomes0101• Nowadd1toit&itbecomes0110,whichis6(10)
10/3/18 Matni,CS64,Fa18 31
AnotherViewof2sComplement
10/3/18 Matni,CS64,Fa18 32
NOTE:InTwo’sComplement,ifthenumber’sMSBis“1”,thenthatmeansit’sanegativenumberandifit’s“0”thenthenumberispositive.
AnotherViewof2sComplement
10/3/18 Matni,CS64,Fa18 33
NOTE:Oppositenumbersshowupassymmetricallyoppositeeachotherinthecircle.
NOTEAGAIN:Whenwetalkof2scomplement,wemustalsomentionthenumberofbitsinvolved
Ranges
• Therangerepresentedbynumberofbitsdiffersbetweenpositiveandnegativebinarynumbers
• GivenNbits,therangerepresentedis:0to
and
10/3/18 Matni,CS64,Fa18 34
+2N–1forpositivenumbers–2N-1to+2N-1–1for2’sComplementnegativenumbers
YOURTO-DOs
• Assignment#1– DueonFriday!!!
• Nextweek,wewilldiscussafewmoreArithmetictopicsandstartexploringAssemblyLanguage!– Doyourreadings!
(again:foundontheclasswebsite)
10/3/18 Matni,CS64,Fa18 35
10/3/18 Matni,CS64,Fa18 36