C++ Bitwise Operators - Programiz

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

In C++, bitwise operators perform operations on integer data at the individual bit-level. These operations include testing, setting, or shifting the actual ... CourseIndex ExploreProgramiz Python JavaScript SQL C C++ Java Kotlin Swift C# DSA PopularTutorials C++if...elseStatement C++forLoop ArraysinC++ StringsinC++ C++Class&Objects StartLearningC++ PopularExamples Createasimplecalculator Checkprimenumber PrinttheFibonaccisequence Checkifanumberispalindromeornot Programtomultiplymatrix ExploreC++Examples ReferenceMaterials iostream cmath cstring ctime Viewall LearningPaths Challenges LearnPythonInteractively TryforFree Courses BecomeaPythonMaster BecomeaCMaster BecomeaJavaMaster ViewallCourses Python JavaScript SQL C C++ Java Kotlin Swift C# DSA PopularTutorials C++if...elseStatement C++forLoop ArraysinC++ StringsinC++ C++Class&Objects StartLearningC++ AllC++Tutorials ReferenceMaterials iostream cmath cstring ctime Viewall Python JavaScript C C++ Java Kotlin PopularExamples Createasimplecalculator Checkprimenumber PrinttheFibonaccisequence Checkifanumberispalindromeornot Programtomultiplymatrix AllC++Examples C++Introduction C++VariablesandLiterals C++DataTypes C++BasicI/O C++TypeConversion C++Operators C++Comments C++FlowControl C++if...else C++forLoop C++do...whileLoop C++break C++continue C++switchStatement C++gotoStatement C++Functions C++Functions C++FunctionTypes C++FunctionOverloading C++DefaultArgument C++StorageClass C++Recursion C++ReturnReference C++Arrays&String C++Arrays MultidimensionalArrays C++FunctionandArray C++String C++Structures C++Structures StructureandFunction C++PointerstoStructure C++Enumeration C++Object&Class C++ObjectsandClass C++Constructors C++Objects&Function C++OperatorOverloading C++Pointers C++Pointers C++PointersandArrays C++PointersandFunctions C++MemoryManagement C++Inheritance C++Inheritance InheritanceAccessControl C++FunctionOverriding InheritanceTypes C++FriendFunction C++VirtualFunction C++Templates RelatedTopics C++OperatorPrecedenceandAssociativity C++Operators C++RelationalandLogicalOperators SubtractComplexNumberUsingOperatorOverloading MakeaSimpleCalculatortoAdd,Subtract,MultiplyorDivideUsingswitch...case C++OperatorOverloading C++BitwiseOperators Inthistutorial,wewilllearnaboutbitwiseoperatorsinC++withthehelpofexamples. InC++,bitwiseoperatorsperformoperationsonintegerdataattheindividualbit-level.Theseoperationsincludetesting,setting,orshiftingtheactualbits.Forexample, a&b; a|b; Hereisalistof6bitwiseoperatorsincludedinC++. Operator Description & BitwiseANDOperator | BitwiseOROperator ^ BitwiseXOROperator ~ BitwiseComplementOperator << BitwiseShiftLeftOperator >> BitwiseShiftRightOperator TheseoperatorsarenecessarybecausetheArithmetic-LogicUnit(ALU)presentinthecomputer'sCPUcarriesoutarithmeticoperationsatthebit-level. Note:Bitwiseoperatorscanonlybeusedalongsidecharandintdatatypes. 1.C++BitwiseANDOperator ThebitwiseAND&operatorreturns1ifandonlyifboththeoperandsare1.Otherwise,itreturns0. ThefollowingtabledemonstratestheworkingofthebitwiseANDoperator.Letaandbbetwooperandsthatcanonlytakebinaryvaluesi.e.1and0. a b a&b 0 0 0 0 1 0 1 0 0 1 1 1 Note:Thetableaboveisknownasthe"TruthTable"forthebitwiseANDoperator. Let'stakealookatthebitwiseANDoperationoftwointegers12and25: 12=00001100(InBinary) 25=00011001(InBinary) //BitwiseANDOperationof12and25 00001100 &00011001 _________ 00001000=8(Indecimal) Example1:BitwiseAND #include usingnamespacestd; intmain(){ //declarevariables inta=12,b=25; cout< intmain(){ inta=12,b=25; cout< intmain(){ inta=12,b=25; cout< intmain(){ intnum1=35; intnum2=-150; cout<> Leftshiftoperator<< 5.C++RightShiftOperator Therightshiftoperatorshiftsallbitstowardstherightbyacertainnumberofspecifiedbits.Itisdenotedby>>. Whenweshiftanynumbertotheright,theleastsignificantbitsarediscarded,whilethemostsignificantbitsarereplacedbyzeroes. OnebitRightShiftAswecanseefromtheimageabove,wehavea4-bitnumber.Whenweperformaone-bitrightshiftoperationonit,eachindividualbitisshiftedtotherightby1bit. Asaresult,theright-mostbitisdiscarded,whiletheleft-mostbitremainsvacant.Thisvacancyisreplacedbya0. 6.C++LeftShiftOperator Theleftshiftoperatorshiftsallbitstowardstheleftbyacertainnumberofspecifiedbits.Itisdenotedby<<. onebitleftshiftaswecanseefromtheimageabove asaresult example5:shiftoperators> intmain(){ //declaringtwointegervariables intnum=212,i; //ShiftRightOperation cout<>"<>i)<>0=212 212>>1=106 212>>2=53 212>>3=26 ShiftLeft: 212<<0=212 212<<1=424 212<<2=848 212<<3=1696 Fromtheoutputoftheprogramabove,wecaninferthat,foranynumberN,theresultsoftheshiftrightoperatorare: N>>0=N N>>1=(N>>0)/2 N>>2=(N>>1)/2 N>>3=(N>>2)/2 andsoon. Similarly,theresultsoftheshiftleftoperatorare: N<<0=N N<<1=(N<<0)*2 N<<2=(N<<1)*2 N<<3=(N<<2)*2 andsoon. Hencewecanconcludethat, N>>m=[N>>(m-1)]/2 N<



請為這篇文章評分?