C++ Bitwise Operators - Linux Hint
文章推薦指數: 80 %
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
延伸文章資訊
- 1你所不知道的C 語言:bitwise 操作 - HackMD
複習數值系統,從應用案例引導學員掌握bitwise 操作,從而邁向專業的程式開發. ... If there are N value bits, each bit shall represent...
- 2位元AND 運算子:& | Microsoft Docs
C++ 標準語言位AND 運算子語法和使用方式。 ... compile with: /EHsc // Demonstrate bitwise AND #include <iostream> u...
- 3Bitwise Operators - C++ - Codecademy
C++ supports different types of bitwise operators that can perform operations on integers at bit-...
- 4Bitwise Operators in C/C++ - GeeksforGeeks
The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two nu...
- 5How does bitwise ^ (XOR) work? | LoginRadius Blog