Bitwise Operators - Binary - learn.sparkfun.com

文章推薦指數: 80 %
投票人數:10人

Bitwise operators perform functions bit-by-bit on either one or two full binary numbers. They make use of boolean logic operating on a group of binary symbols. Home Tutorials Binary Binary ≡Pages Contributors: jimblom Favorited Favorite 48 Share UsethisURLtoshare: ShareonTumblr Submittoreddi ShareonTwitter ShareonFacebook PinIt BitwiseOperators Thereareanumberofwaystomanipulatebinaryvalues.Justasyoucanwithdecimalnumbers,youcanperformstandardmathematicaloperations-addition,subtraction,multiplication,division-onbinaryvalues(whichwe’llcoveronthenextpage).Youcanalsomanipulateindividualbitsofabinaryvalueusingbitwiseoperators. Bitwiseoperatorsperformfunctionsbit-by-bitoneitheroneortwofullbinarynumbers.Theymakeuseofbooleanlogicoperatingonagroupofbinarysymbols.Thesebitwiseoperatorsarewidelyusedthroughoutbothelectronicsandprogramming. Complement(NOT) Thecomplementofabinaryvalueislikefindingtheexactoppositeofeverythingaboutit.Thecomplementfunctionlooksatanumberandturnsevery1intoa0andevery0becomesa1.ThecomplementoperatorisalsocalledNOT. Forexampletofindthecomplementof10110101: NOT10110101(decimal181) --------= 01001010(decimal74) NOTistheonlybitwiseoperatorwhichonlyoperatesonasinglebinaryvalue. OR ORtakestwonumbersandproducestheunionofthem.Here’stheprocesstoORtwobinarynumberstogether:lineupeachnumbersothebitsmatchup,thencompareeachoftheirbitsthatshareaposition.Foreachbitcomparison,ifeitherorbothbitsare1,thevalueoftheresultatthatbit-positionis1.Ifbothvalueshavea0atthatposition,theresultalsogetsa0atthatposition. ThefourpossibleORcombinations,andtheiroutcomeare: 0OR0=0 0OR1=1 1OR0=1 1OR1=1 Forexampletofindthe10011010OR01000110,lineupeachofthenumbersbit-by-bit.Ifeitherorbothnumbershasa1inacolumn,theresultvaluehasa1theretoo: 10011010 OR01000110 --------= 11011110 ThinkoftheORoperationasbinaryaddition,withoutacarry-over.0plus0is0,but1plusanythingwillbe1. AND ANDtakestwonumbersandproducestheconjunctionofthem.ANDwillonlyproducea1ifbothofthevaluesit’soperatingonarealso1. TheprocessofAND'ingtwobinaryvaluestogetherissimilartothatofOR.Lineupeachnumbersothebitsmatchup,thencompareeachoftheirbitsthatshareaposition.Foreachbitcomparison,ifeitherorbothbitsare0,thevalueoftheresultatthatbit-positionis0.Ifbothvalueshavea1atthatposition,theresultalsogetsa1atthatposition. ThefourpossibleANDcombinations,andtheiroutcomeare: 0AND0=0 0AND1=0 1AND0=0 1AND1=1 Forexample,tofindthevalueof10011010AND01000110,startbyliningupeachvalue.Theresultofeachbit-positionwillonlybe1ifbothbitsinthatcolumnarealso1. 10011010 AND01000110 --------= 00000010 ThinkofANDaskindoflikemultiplication.Wheneveryoumultiplyby0theresultwillalsobe0. XOR XORistheexclusiveOR.XORbehaveslikeregularOR,exceptit’llonlyproducea1ifeitheroneortheothernumbershasa1inthatbit-position. ThefourpossibleXORcombinations,andtheiroutcomeare: 0XOR0=0 0XOR1=1 1XOR0=1 1XOR1=0 Forexample,tofindtheresultof10011010XOR01000110: 10011010 XOR01000110 --------= 11011100 Noticethe2ndbit,a0resultingfromtwo1’sXOR’edtogether. Bitshifts Bitshiftsaren’tnecessarilyabitwiseoperatorlikethoselistedabove,buttheyareahandytoolinmanipulatingasinglebinaryvalue. Therearetwocomponentstoabitshift-thedirectionandtheamountofbitstoshift.Youcanshiftanumbereithertotheleftorright,andyoucanshiftbyonebitormanybits. Whenshiftingtotheright,oneormoreoftheleast-significantbits(ontheright-sideofthenumber)justgetcutoff,shiftedintotheinfinitenothing.Leadingzeroscanbeaddedtokeepthebit-lengththesame. Forexample,shifting10011010totherighttwobits: RIGHT-SHIFT-210011010(decimal154) --------= 00100110(decimal38) Shiftingtotheleftaddspushesallofthebitstowardthemost-significantside(theleft-side)ofthenumber.Foreachshift,azeroisaddedintheleast-significant-bitposition. Forexample,shifting10011010totheleftonebit: LEFT-SHIFT-110011010(decimal154) --------= 100110100(decimal308) Thatsimplebitshiftactuallyperformsarelativelycomplicatedmathematicalfunction.Shiftstotheleftnbitsmultipliesanumberby2n(seehowthelastexamplemultipliedtheinputbytwo?),whileashiftnbitstotherightwilldoanintegerdivideby2n.Shiftingtotherighttodividecangetweird-anyfractionsproducedbytheshiftdivisionwillbechoppedoff,whichiswhy154shiftedrighttwiceequals38insteadof154/4=38.5.Bitshiftscanbeapowerfullyfastwaytodivideormultiplyby2,4,8,etc. Thesebitwiseoperatorsprovideusmostofthetoolsnecessarytodostandardmathematicaloperationsonbinarynumbers. Share UsethisURLtoshare: ShareonTumblr Submittoreddi ShareonTwitter ShareonFacebook PinIt Viewasasinglepage NextPage→ BinaryinProgramming ←PreviousPage Bits,Nibbles,andBytes × Pages ABC'sof1'sand0's CountingandConverting Bits,Nibbles,andBytes BitwiseOperators BinaryinProgramming ResourcesandGoingFurther Comments 3 SinglePage Print Tags Communication ComputerEngineering Programming License CreativeCommons tutorialsareCCBY-SA4.0 YourAccount LogIn Register ×Close LogIn



請為這篇文章評分?