{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture_02 - ECE190 Lecture02 January20,2011...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ECE190 Lecture02 January20,2011 Bits and Operations on Bits Lecture Topics Unsignedandsignedintegerrepresentations Conversionbetweenbinaryanddecimalrepresentations Arithmeticandlogicaloperationsonbinarynumbers Floating ­pointdatarepresentation Otherrepresentations Lecture materials TextbookChapter2 Homework Postedonthecoursewebsite(http://courses.engr.illinois.edu/ece190/) DueWednesdayJanuary26at5pmintheECE190dropboxlocatedinthebasementofEverittLab   1 V.Kindratenko ECE190 Lecture02 January20,2011 Unsigned and signed integer representations Signed ­magnitude Sofarwedefinedawaytowritepositive(unsigned)integernumbersusing0and1bits,buthow aboutwritingnegativenumbers? o Wecanusehalfofthedistinctpatternsmadeofkbitstorepresentpositivevaluesand theotherhalftorepresentnegativevalues Example:with3bitswecanrepresentintegernumbersfrom ­3to+3;this leavesuswithone3 ­bitcodenotassigned Westillneedtodecidewhatdistinctpatternsweshoulduseforrepresenting positivenumbersvs.negativenumbers o Oneway,calledsigned ­magnitude,istousetheleadingdigittoindicateifthenumberis positiveornegative Positivenumberswillhave0astheleadingdigit Negativenumberswillhave1astheleadingdigit Binarynotation 000 001 010 011 100 101 110 111 Signedmagnitude 0 1 2 3  ­0  ­1  ­2  ­3 Thelargestpositivenumberisthisexampleis0112=310 Thesmallestnegativenumberinthisnotationis1112= ­310 Westillarenotusingonerepresentationefficiently:1002 Anotherwaytorepresentbothpositiveandnegativeintegers,called ,isbased ontheideathatallnegativenumberscanberepresentedbyflippingdigitsinthepositive numbers o Example:0102=210.1012= ­210 o Inthiscase,westillarenotusingonerepresentationefficiently:1112 o Thisrepresentationwasactuallyusedinsomeearlycomputers,e.g.,CDC6600 Binarynotation 000 001 010 011 100 101 110 111 Signedmagnitude 0 1 2 3  ­0  ­1  ­2  ­3  0 1 2 3  ­3  ­2  ­1  ­0   2 V.Kindratenko ECE190 Lecture02 January20,2011  Positivenumbersarerepresentedinastraightforwardway,withleadingdigitsetto0 o Withkbits,2k ­1 ­1positivenumbers(from1to2k ­1 ­1)canberepresentedthisway Thenegativeintegersarerepresentedbycountingbackwardandwrappingaround o Example:1112= ­110,1102= ­210  o Thedefaultruleisthatallnegativenumbershavealeadingbitsetto1 Negativenumbersaredefinedinsuchawaythatwhenaddedtothepositivenumberswiththe samemagnitude,0isobtained o Thismakesimplementingdigitallogicparticularlysimplerthanusinganyotherbinary representation Binarynotation Signedmagnitude   000 0 0 0 001 1 1 1 010 2 2 2 011 3 3 3 100  ­0  ­3  ­4 101  ­1  ­2  ­3 110  ­2  ­1  ­2 111  ­3  ­0  ­1 Withsuchannotation,usingkbits,wecandefine2k ­1 ­1positivenumbers,0,and2k ­1 ­1negative numbers,or2*(2k ­1 ­1)+1=2k ­1numbersaltogether.Whatdowedowiththeunused representation,1002inourexample? o Wenotethatwhenwecontinuecountingbackward,thenextvalueafter1012= ­310is 1002,andthusitislogicaltoassumethat1002= ­410.  ­(2)k ­1to2k ­1 ­1 o Example:k  3 ­1 3 ­1  ­(2) = ­410,thelargestnumberis2  ­1=310. Howe representationfornegativenumbers? TakethepositivenumberA2,flipallitsbits(thisgivesusthecomplementofA2),andadd1to thecomplementofA2;theresultis ­A2: ­A2=complement(A2)+12  ­1310 o Startwiththepositivenumberthathasthesamemagnitude:1310=011012 o Finditscomplementbyflippingeverybit:complement(011012)=100102 o Add1tothecomplement:100102+12=100112 o Verifythatthenumberwegotisreally ­1310 Thisshouldhold:1310+( ­1310)=010,l it:011012+100112 01101 10011 100000  3 V.Kindratenko ECE190 Lecture02 January20,2011 Notethatacarryoutoffivebitsisproduced,thatisthesumisreally1000002. Thiscarryoutbitisalways representation Inmathematics,radixcomplementisthenumberwhich,addedtothegivenn ­digitnumberinradixr, resultsinr0.Whenusingbinarynumbers,r=2,thusthenameof diminishedradixcomplementforr=2. Relation to C InClanguage,integernumbersarestoredusing  o Examplesofliteralvalues:2, ­5,128, ­10000 o Examplesofanexpressionusingintegerliteralvalues:10+20 Callowstheprogrammertorefertovaluessymbolically,byname.Suchsymbolnamesare calledvariables Avariabledeclarationrequiresvariabletypeandname Todeclareavariableofanintegertype,weuseintkeyword o Example:inta;< ­wedeclaredavariableoftypeint;itwillbestoredincomputer  unsignedmodifierputinfrontofintkeywordcanbeusedtoindicatethattheleftmostbit shouldnotbetreatedasasignbit,insteaditshouldbetreatedaspartofthemagnitude o Example:unsignedintb;< ­wedeclaredavariablebthatwillbestoredincomputer memoryusing(unsigned)magnituderepresentation Ifunsignedmodifierisnotused,signedmodifierisimplied o intaisthesameassignedinta Thesizeoftypeint(numberofbitsallocatedincomputermemorytostoreavariableoftype int)isarchitecture ­dependent.E.g.,ona32 ­bitarchitecture,sizeofintis32bits. Callowstospecifytypesofintegernumbersthatoccupyapre ­definednumberofbitsin memory.Thisisdonewiththehelpofshortandlongtypemodifiers: Type shortint unsignedshortint longint unsignedint longlongint unsignedlonglongint Bytes 2 2 4 4 8 8 Bits 16 16 32 32 64 64 Range  ­(2)15to215 ­1,or ­32,768 ­>+32,767 0to216 ­1,or0 ­>+65,535  ­2,147,483,648 ­>+2,147,483,647 0 ­>+4,294,967,295   Conversion between binary and decimal representations Binary to decimal conversion ak ­1ak ­2 o  integers integernumberconsistingofkbitsiswrittenintheform 1a0whereaiiseither0or1 Example:110001112 4 V.Kindratenko ECE190 Lecture02 January20,2011 Examinetheleadingbit,ak ­1 o Ifitis0,thentheintegerispositiveandwecancomputeitsvaluerightaway o representationofthepositivenumberwhosemagnitudeisthesame  ­A2=complement(A2)+12 Computethemagnitude o A10=ak ­2*2k ­2+ 1*21+a0*20 Ifthe  ­  Example:convert110001112tothedecimalnotation: o Leadingbitissetto1,thusthisisanegativenumber o  Flipbitsandadd12:001110002+12=001110012 o Themagnitudeis0*26+1*25+1*24+1*23+0*22+0*21+1*20=3210+1610+810+110=57 o Applysign;theansweris ­57 2m ­1to theleft ­most(sign)bitasfollows:A10= ­ak ­1*2k ­1+ak ­2*2k ­2+ 1*21+a0*20 Convert,themagnitudeofagivennumbertoitsbinaryrepresentationusingtheshortdivision methodpresentedinthelastlecture Ifthenumberispositive,wearedone  Example: ­15610=?2 o Convertitsmagnitudefirst:15610=0100111002 o numberwith theoppositesign: ­15610=1011001002 Arithmetic and Logical operations on binary numbers Addition and subtraction similarlytothedecimalnumbers: o o o Proceedfromrighttoleft,onedigitatatime Ateachpointwegenerateasumdigitandacarry Insteadofgeneratingcarryafter9,asindecimalnotation,wegeneratecarry after1since1isthelargestbinarydigit Example: 01011 00011 01110 A2 ­B2=A2+( ­B2)  5 V.Kindratenko ECE190 Lecture02 January20,2011 o Example:011102 ­010012=011102+( ­010012)=011102+101112 Addingnumbertoitselfequalsshiftingbitstotheleft o Example: 00111011 00111011 01110110 Howtoaddnumberswithdifferentnumberofbinarydigits? o Thenumbersneedtobeconvertedtothesamelength o Rule1:thevalueofapositivenumberdoesnotchangeifweextendthesignbit0as manybitpositionstotheleftasneeded o Rule2:thevalueofanegativenumberdoesnotchangeifweextendthesignbit1as manybitpositionstotheleftasneeded o Sincethisisthesignbitthatgetsextended,thisoperationisreferredtoasSign ­ EXTension,orSEXT o Example: 00001101  00001101 100101  11100101    11110010 Overflowmeansthattheresultofaddingtwonumberscannotberepresentedinthenumberof bitsallotted.Intheexamplebelowweadd9+11,butwith5bitsthelargestpositivenumber thatcanberepresentedis15.Notethatthesignbitofthisresultiswrong o Example: 01001 01011 10100 o Overflowiseasytodetect: Iftheleadingbitbecomes1whenaddingtwopositivenumbers,overflowhas occurred Iftheleadingbitbecomes0whenaddingtwonegativenumbers,overflowhas occurred o Overflowmeansthattheoperationwearetryingtoperformexceedsthecapabilitiesof themachine Logical operations on binary numbers Anotherclassofbasicoperationsthatcanbeperformedonbinarynumbersislogicaloperations.They arecalledthiswaybecauseabitvalueof0canbeinterpretedasfalseandabitvalueof1canbe interpretedastrue. Aconvenientwaytorepresentbehavioroflogicaloperationsistousethetruthtablethat specifiestheoutputofalogicaloperationforagivensetofinputs o ExampleforsomehypotheticalfunctionFUNC:  6 V.Kindratenko ECE190 Lecture02 January20,2011 AB|FUNC 00|1 01|0 10|1 11|1 ANDbinarylogicalfunction o requirestwosourceoperands,eachisalogicalvariabletakingavalueof0or1 o theoutputofANDis1onlyifbothoperandsare1,otherwiseitis0 o ThetruthtableforANDfunctionis AB|AND 00|0 01|0 10|0 11|1 o ApplyingANDfunctiontotwobitpatternsofmbitseachmeansapplyingtheoperation individuallytoeachpairofthecorrespondingbits Example:11000110001AND11110000011=? 11000110001 11110000011 11000000001 o ANDfunctionhasauniqueuse:maskingoutbitsthatarenotneeded Maskingallowstoisolatethebitsinaparticularlocationwhileignoringtherest ofthebits Maskingrequiresabitpattern,calledbitmask,suchthatbitsatthelocationof interestaresetto1andtherestofbitsaresetto0 TheinputbitpatternisANDedwiththebitmaskresultinginzeroingeverything butthebitsofinterest Example:bits2&3areofinterestinan4 ­bitworda3a2a1a0 a3a2a1a0 1100 bitmask a3a200 (inclusive ­)ORbinarylogicalfunction o requirestwosourceoperands,eachisalogicalvariabletakingavalueof0or1 o theoutputofORis1ifeitherofthetwooperandsis1,otherwiseitis0 o ThetruthtableforORfunctionis AB|OR 00|0 01|1 10|1 11|1 o ApplyingORfunctiontotwobitpatternsofmbitseachmeansapplyingtheoperation individuallytoeachpairofthecorrespondingbits,aswithAND Example:11000110001OR11110000011=?  7 V.Kindratenko ECE190 Lecture02 January20,2011 11000110001 11110000011 11110110011 (exclusive ­)XORbinarylogicalfunction o requirestwosourceoperands,eachisalogicalvariabletakingavalueof0or1 o theoutputofORis1onlyiftwooperandsaredifferent,otherwiseitis0 o ThetruthtableforXORfunctionis  AB|XOR 00|0 01|1 10|1 11|0 o XORhasauniqueuse:twobitpatternsareidenticalifXORoutputforthemhasallzeros NOTfunction o Isaunarylogicaloperation,itoperatesonlyononeoperand(inputvalue) o Alsoknownascomplementoperation o ThetruthtableforNOTfunctionis A|NOT 0|1 1|0 o NOTisappliedtobitpatternsthesamewayasotheroperations,bitbybit: Example:NOT11000110001=? 11000110001 00111001110 AND,OR,andNOTarethreefundamentallogicalfunctions,anyotherlogicalfunctionscanbe createdfromthem o Example:XORcanbeimplementedas(NOTaANDb)OR(aANDNOTb) Relation to C Cprovidesarithmeticandbitmanipulationoperatorsformanipulatingintegernumbers Arithmeticoperatorsinclude o Addition+ o Subtraction ­ o Multiplication* o Division/ o Modulus% o Increment++ o Decrement ­ ­ Bitwiseoperatorsinclude o BitwiseNOT~ o BitwiseAND&  8 V.Kindratenko ECE190 Lecture02 January20,2011 o BitwiseOR| o BitwiseXOR^ o Leftshift<< o Rightshift>> Examples: o 5+10*25/11< ­thisexpressionwillbeevaluatedusingintegerarithmeticrounding resultstothesmallerintegervalue o 5|2^10<<2< ­integervalueswillbetreatedasbitfieldsandappropriatebit manipulationswillbeperformedintheorderdefinedbytheoperatorsprecedence Floating ­point data representation Using16 ­ scomplementdatatype,wecanrepresentintegernumbersintherangefrom 15 15  ­2 to+2  ­1. o Theprecisionofthisrepresentationis15bits o Therangeofthisrepresentationis215 Issueswiththisnotation o Howdowerepresentavaluelargerthan215? o Howdowerepresentanon ­integervalue,say2.34*10234? Floatingpointdatatypeisthesolutionforthisproblem o Insteadofusingallthebitstorepresenttheprecisionofavalue,somebitsareallocated tostoretherangeofavalue Supportforfloating ­pointdatatypesisspecifiedinISA Thereisanexistingstandardthatissupportedbymostcomputersystems,calledIEEEStandard forFloatingPointArithmetic,orIEEE754 Thestandarddefinesseveraltypesoffloatingpointnumbers,wewilltakealookatwhatis knownasfloat,whichrequires32bitstorepresentanumber o 1bitforthesign o 8bitsfortherange(theexponentfield) o 23bitsforprecision(thefractionfield),orsignificantdigitsinthescientificnotation terminology o Thisnotationroughlycorrespondstothescientificnotationintheforma*10b 1 8 sign 23 exponent fraction  s ×1.fraction×2exponent ­127where1<=exponent<=254  9 V.Kindratenko ECE190 Lecture02 January20,2011 o Thefractionisnormalized,meaningthatexactlyonenon ­zerobinarydigitappearsto theleftofthebinarypoint Sincethereisonlyonesuchdigit,1,inbinarynotation,itisignoredinthe representation,butisimplicitlyassumedasalwayspresent;thiseffectively countstowardsusing24bitsinsteadof23bitsforthefractionrepresentation o Theexponentfielduses8bitstostoretheexponentasanunsignednumber ­128 Forexample,iftheexponentis810,thevaluestoredintheexponentfieldis 13510:13510 ­12710=810 Therearetwospecialcasesfortheexponentfield:all0sandall1s. s Forthecaseofall0s: ×0.fraction×2 ­126 Forthecaseofall1s,theinterpretationofthevaluestoredmaybe eitherNaNor+/ ­infinity,dependingonthefractionvalue Example:00111101100000000000000000000000 o 00111101100000000000000000000000 o Leadingbitis0 positivenumber o 011110112=12310,12310 ­12710= ­4 exponent o 1.000000000000000000000002 o Thus,thenumberis+1*1.0*2 ­4=1/16 Relation to C InClanguage,realnumbersarestoredusing32 ­bitor64 ­bitIEEE754floatingpoint representation o Examplesof32 ­bitfloatingpointnumbersinC:2.5f, ­1.56f< ­notefcharacter o Examplesof64 ­bitfloatingpointnumbersinC:2.5, ­1.56< ­noteabsenceoffcharacter Todeclareavariableofafloatingpointtype,weusefloat(for32 ­bit)ordouble(for64 ­bit) keywords o Example:floata;< ­wedeclaredavariableoftypefloat;itwillbestoredincomputer memoryin32 ­bitIEEE754representation o Example:doubleb;< ­wedeclaredavariableoftypedouble;itwillbestoredin computermemoryin64 ­bitIEEE754representation Other representations of information Bitvector o Awaytousebitstorepresentthingsotherthannumbers,e.g.,stateofthesystemwith respectofbeingfreeorbusy o Usen ­bitbinarypattern,calledbitvector,tokeeptrackofthestateofsomeunits,with 1indicatingthatunitisfreeand0indicatingthatunitisbusy BUSYNESSbitvectorasanexample Bitmaskasanotherexample AmericanStandardCodeforInformationInterchange(ASCII)  10 V.Kindratenko ECE190 Lecture02 January20,2011 o Awaytointerchangeinformationbetweencomputerequipmentmadebydifferent manufacturers o EachkeyonthekeyboardisidentifiedbyitsuniqueASCIIcodestoredusing8bits Digit3hasthisASCIIcode:00110011 Lowercasee:01100101 Carriagereturn:00001101 Hexadecimalnotation o Isanumeralsystemwith...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern