C++ Bitwise Operators - Linux Hint

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

A bitwise operator is applied to manipulate the individual bits for integers and character data types. It is used in embedded software development. Inthisarticle,wearegoingtodiscussbitwiseoperatorsintheC++programminglanguage.Wewillseeseveralworkingexamplestounderstandbitwiseoperationsindetail.InC++,thebitwiseoperatorsworkontheindividualbitlevel. BriefOverviewofBitwiseOperators Anoperatorisasymbolthatinstructsthecompilertoperformcertainmathematicalorlogicaloperations.ThereareseveraltypesofoperatorsinC++,suchas: ArithmeticOperators LogicalOperators RelationalOperators AssignmentOperators BitwiseOperators MiscOperators AlltheBitwiseoperatorsworkattheindividualbitlevel.Thebitwiseoperatorcanonlybeappliedtotheintegerandcharacterdatatypes.Forexample,ifyouhaveanintegertypevariablewiththesizeof32bitsandyouapplybitwiseNOToperation,thebitwiseNOToperatorwillbeappliedforall32bits.So,eventually,allthe32bitsinthevariablewillbeinversed. TherearesixdifferentbitwiseoperatorsareavailableinC++: BitwiseOR[representedas“|”] BitwiseAND[representedas“&”] BitwiseNOT[representedas“~”] BitwiseXOR[representedas“^”] BitwiseLeftShift[representedas“<>”] BitwiseORTruthTable TheBitwiseORoperatorproduces1whenatleastoneoperandissetto1.HereisthetruthtablefortheBitwiseORoperator: Bit-1 Bit-2 Bit-1|Bit-2 0 0 0 0 1 1 1 0 1 1 1 1 BitwiseANDTruthTable BitwiseANDoperatorproduces1whenboththeoperandsaresetto1.HereisthetruthtablefortheBitwiseANDoperator: Bit-1 Bit-2 Bit-1&Bit-2 0 0 0 0 1 0 1 0 0 1 1 1 BitwiseNOTTruthTable BitwiseNOToperatorinvertstheoperand.HereisthetruthtableforBitwiseNOToperator: Bit-1 ~Bit-1 0 1 1 0 BitwiseXORTruthTable BitwiseXORoperatorproduces1if,andonlyif,oneoftheoperandsissetto1.HereisthetruthtableforBitwiseANDoperator: Bit-1 Bit-2 Bit-1^Bit-2 0 0 0 0 1 1 1 0 1 1 1 0 BitwiseLeftShiftOperator BitwiseLeftShiftoperatorshiftsallthebitsleftbythespecifiednumberofspecifiedbits.Ifyouleftshiftallthebitsofthedataby1,theoriginaldatawillbemultipliedby2.Similarly,ifyouleftshiftallthebitsofthedataby2,theoriginaldatawillbemultipliedby4. BitwiseRightShiftOperator BitwiseRightShiftoperatorshiftsallthebitsrightbythespecifiednumberofspecifiedbits.Ifyourightshiftallthebitsofthedataby1,theoriginaldatawillbedivided(integerdivision)by2.Similarly,ifyourightshiftallthebitsofthedataby2,theoriginaldatawillbedivided(integerdivision)by4. Examples Now,sincewehaveunderstoodthebasicconceptofbitwiseoperations,letuslookatacoupleofexamples,whichwillhelpyoutounderstandthebitwiseoperationsinC++: Example-1:BitwiseOROperator Example-2:BitwiseANDOperator Example-3:BitwiseNOTOperator Example-4:BitwiseXOROperator Example-5:BitwiseLeftShiftOperator Example-6:BitwiseRightShiftOperator Example-7:SetBit Example-8:ClearBit Theexample-7and8arefordemonstratingthereal-worldusageofbitwiseoperatorsintheC++programminglanguage. Example-1:BitwiseOROperator Inthisexampleprogram,wewilldemonstratetheBitwiseORoperator. #include #include #include usingnamespacestd; //display()function voiddisplay(stringprint_msg,intnumber) {   bitset<16>myBitSet(number);   cout< #include #include usingnamespacestd; //display()function voiddisplay(stringprint_msg,intnumber) {   bitset<16>myBitSet(number);   cout< #include #include usingnamespacestd; //display()function voiddisplay(stringprint_msg,intnumber) {   bitset<16>myBitSet(number);   cout< #include #include usingnamespacestd; //display()function voiddisplay(stringprint_msg,intnumber) {   bitset<16>myBitSet(number);   cout< #include #include usingnamespacestd; //display()function voiddisplay(stringprint_msg,intnumber) {   bitset<16>myBitSet(number);   cout< #include #include usingnamespacestd; //display()function voiddisplay(stringprint_msg,intnumber) {   bitset<16>myBitSet(number);   cout<>1;   result_2=second_num>>2;   //printtheinputnumbersandoutputvalue   cout<>1    = ",result_1);   cout<>2   = ",result_2);   cout< #include #include usingnamespacestd; //display()function voiddisplay(stringprint_msg,intnumber) {   bitset<16>myBitSet(number);   cout< #include #include usingnamespacestd; //display()function voiddisplay(stringprint_msg,intnumber) {   bitset<16>myBitSet(number);   cout<



請為這篇文章評分?