Bitwise Operators in C: AND, OR, XOR, Shift & Complement
文章推薦指數: 80 %
Bitwise operators are special operator set provided by 'C.' · They are used in bit level programming. · These operators are used to manipulate ...
Skiptocontent
WhatareBitwiseOperators?
BitwiseOperatorsareusedformanipulatingdataatthebitlevel,alsocalledbitlevelprogramming.Bitwiseoperatesononeormorebitpatternsorbinarynumeralsattheleveloftheirindividualbits.Theyareusedinnumericalcomputationstomakethecalculationprocessfaster.
Followingisthelistofbitwiseoperatorsprovidedby‘C’programminglanguage:
Operator
Meaning
&
BitwiseANDoperator
|
BitwiseORoperator
^
BitwiseexclusiveORoperator
~
BinaryOne’sComplementOperatorisaunaryoperator
<<
Leftshiftoperator
>>
Rightshiftoperator
Bitwiseoperatorscannotbedirectlyappliedtoprimitivedatatypessuchasfloat,double,etc.Alwaysrememberonethingthatbitwiseoperatorsaremostlyusedwiththeintegerdatatypebecauseofitscompatibility.
Thebitwiselogicaloperatorsworkonthedatabitbybit,startingfromtheleastsignificantbit,i.e.LSBbitwhichistherightmostbit,workingtowardstheMSB(MostSignificantBit)whichistheleftmostbit.
Theresultofthecomputationofbitwiselogicaloperatorsisshowninthetablegivenbelow.
x
y
x&y
x|y
x^y
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0
Inthistutorial,youwilllearn-
WhatareBitwiseOperators?
BitwiseAND
BitwiseOR
BitwiseExclusiveOR
Bitwiseshiftoperators
Bitwisecomplementoperator
BitwiseAND
Thisisoneofthemostcommonlyusedlogicalbitwiseoperators.Itisrepresentedbyasingleampersandsign(&).Twointegerexpressionsarewrittenoneachsideofthe(&)operator.
TheresultofthebitwiseANDoperationis1ifboththebitshavethevalueas1;otherwise,theresultisalways0.
Letusconsiderthatwehave2variablesop1andop2withvaluesasfollows:
Op1=00001101
Op2=00011001
TheresultoftheANDoperationonvariablesop1andop2willbe
Result=00001001
Aswecansee,twovariablesarecomparedbitbybit.Wheneverthevalueofabitinboththevariablesis1,thentheresultwillbe1orelse0.
BitwiseOR
Itisrepresentedbyasingleverticalbarsign(|).Twointegerexpressionsarewrittenoneachsideofthe(|)operator.
TheresultofthebitwiseORoperationis1ifatleastoneoftheexpressionhasthevalueas1;otherwise,theresultisalways0.
Letusconsiderthatwehave2variablesop1andop2withvaluesasfollows:
Op1=00001101
Op2=00011001
TheresultoftheORoperationonvariablesop1andop2willbe
Result=00011101
Aswecansee,twovariablesarecomparedbitbybit.Wheneverthevalueofabitinoneofthevariablesis1,thentheresultwillbe1orelse0.
BitwiseExclusiveOR
Itisrepresentedbyasymbol(^).Twointegerexpressionsarewrittenoneachsideofthe(^)operator.
TheresultofthebitwiseExclusive-ORoperationis1ifonlyoneoftheexpressionhasthevalueas1;otherwise,theresultisalways0.
Letusconsiderthatwehave2variablesop1andop2withvaluesasfollows:
Op1=00001101
Op2=00011001
TheresultoftheXORoperationonvariablesop1andop2willbe
Result=00010100
Aswecansee,twovariablesarecomparedbitbybit.Wheneveronlyonevariableholdsthevalue1thentheresultis0else0willbetheresult.
Letuswriteasimpleprogramthatdemonstratesbitwiselogicaloperators.
#include
延伸文章資訊
- 1Bitwise operations in C - Wikipedia
- 2Bitwise Operators in C | GATE Notes - Byju's
- 3Bitwise Operators in C Programming - Programiz
In this tutorial you will learn about all 6 bitwise operators in C programming with examples. In ...
- 4Bitwise operators in C with Examples - Linux Hint
Bitwise AND Operator · Bitwise OR Operator · Bitwise XOR Operator · Bitwise NOT Operator · Bitwis...
- 5Bitwise Operators in C: AND, OR, XOR, Shift & Complement
Bitwise operators are special operator set provided by 'C.' · They are used in bit level programm...