C# Bitwise and Bit Shift Operators - Programiz
文章推薦指數: 80 %
Bitwise XOR CourseIndex ExploreProgramiz Python JavaScript SQL C C++ Java Kotlin Swift C# DSA PopularTutorials C#"HelloWorld"Program OperatorsinC# C#ifStatement C#foreachLoop C#forLoop StartLearningC# LearningPaths Challenges LearnPythonInteractively TryforFree Courses BecomeaPythonMaster BecomeaCMaster BecomeaJavaMaster ViewallCourses Python JavaScript SQL C C++ Java Kotlin Swift C# DSA PopularTutorials C#"HelloWorld"Program OperatorsinC# C#ifStatement C#foreachLoop C#forLoop StartLearningC# AllC#Tutorials Python JavaScript C C++ Java Kotlin PopularExamples Addtwonumbers Checkprimenumber Findthefactorialofanumber PrinttheFibonaccisequence Checkleapyear AllPythonExamples Introduction C#HelloWorld C#Keywords&Identifiers C#Variables C#Operators C#OperatorPrecedence C#BitwiseOperators C#BasicI/O C#Expressions&Statements C#Comments FlowControl C#if...else C#switchStatement C#TernaryOperator C#forLoop C#whileLoop C#NestedLoops C#breakStatement C#continueStatement Arrays C#Arrays C#MultidimensionalArrays C#JaggedArray C#foreachLoop OOP(I) C#ClassandObjects C#Methods C#AccessModifiers C#VariableScope C#Constructors C#thisKeyword C#staticKeyword C#Strings OOP(II) C#Inheritance C#AbstractClass&Methods C#NestedClass C#PartialClass C#SealedClass C#Interface C#MethodOverloading C#ConstructorOverloading AdditionalTopics C#using C#TypeConversion C#PreprocessorDirectives C#Namespaces C#struct RelatedTopics C#Operators C#OperatorPrecedenceandAssociativity C#BasicInputandOutput C#ternary(?:)Operator C#switchStatement C#if,if...else,if...elseifandNestedifStatement C#BitwiseandBitShiftOperators Inthistutorial,wewilllearnindetailaboutbitwiseandbitshiftoperatorsinC#.C#provides4bitwiseand2bitshiftoperators. Bitwiseandbitshiftoperatorsareusedtoperformbitleveloperationsoninteger(int,long,etc)andbooleandata.Theseoperatorsarenotcommonlyusedinreallifesituations. Ifyouareinterestedtoexploremore,visitpracticalapplicationsofbitwiseoperations. ThebitwiseandbitshiftoperatorsavailableinC#arelistedbelow. ListofC#BitwiseOperators Operator OperatorName ~ BitwiseComplement & BitwiseAND | BitwiseOR ^ BitwiseExclusiveOR(XOR) << BitwiseLeftShift >> BitwiseRightShift BitwiseOR BitwiseORoperatorisrepresentedby|.ItperformsbitwiseORoperationonthecorrespondingbitsoftwooperands.Ifeitherofthebitsis1,theresultis1.Otherwisetheresultis0. Iftheoperandsareoftypebool,thebitwiseORoperationisequivalenttologicalORoperationbetweenthem. ForExample, 14=00001110(InBinary) 11=00001011(InBinary) BitwiseORoperationbetween14and11: 00001110 00001011 -------- 00001111=15(InDecimal) Example1:BitwiseOR usingSystem; namespaceOperator { classBitWiseOR { publicstaticvoidMain(string[]args) { intfirstNumber=14,secondNumber=11,result; result=firstNumber|secondNumber; Console.WriteLine("{0}|{1}={2}",firstNumber,secondNumber,result); } } } Whenweruntheprogram,theoutputwillbe: 14|11=15 BitwiseAND BitwiseANDoperatorisrepresentedby&.ItperformsbitwiseANDoperationonthecorrespondingbitsoftwooperands.Ifeitherofthebitsis0,theresultis0.Otherwisetheresultis1. Iftheoperandsareoftypebool,thebitwiseANDoperationisequivalenttologicalANDoperationbetweenthem. ForExample, 14=00001110(InBinary) 11=00001011(InBinary) BitwiseANDoperationbetween14and11: 00001110 00001011 -------- 00001010=10(InDecimal) Example2:BitwiseAND usingSystem; namespaceOperator { classBitWiseAND { publicstaticvoidMain(string[]args) { intfirstNumber=14,secondNumber=11,result; result=firstNumber&secondNumber; Console.WriteLine("{0}&{1}={2}",firstNumber,secondNumber,result); } } } Whenweruntheprogram,theoutputwillbe: 14&11=10 BitwiseXOR BitwiseXORoperatorisrepresentedby^.ItperformsbitwiseXORoperationonthecorrespondingbitsoftwooperands.Ifthecorrespondingbitsaresame,theresultis0.Ifthecorrespondingbitsaredifferent,theresultis1. Iftheoperandsareoftypebool,thebitwiseXORoperationisequivalenttologicalXORoperationbetweenthem. ForExample, 14=00001110(InBinary) 11=00001011(InBinary) BitwiseXORoperationbetween14and11: 00001110 00001011 -------- 00000101=5(InDecimal) IfyouwanttomoreabouttheusageofBitwiseXOR,visitTheMagicofXOR Example3:BitwiseXOR usingSystem; namespaceOperator { classBitWiseXOR { publicstaticvoidMain(string[]args) { intfirstNumber=14,secondNumber=11,result; result=firstNumber^secondNumber; Console.WriteLine("{0}^{1}={2}",firstNumber,secondNumber,result); } } } Whenweruntheprogram,theoutputwillbe: 14^11=5 BitwiseComplement BitwiseComplementoperatorisrepresentedby~.Itisaunaryoperator,i.e.operatesononlyoneoperand.The~operatorinvertseachbitsi.e.changes1to0and0to1. ForExample, 26=00011010(InBinary) BitwiseComplementoperationon26: ~00011010=11100101=229(InDecimal) Example4:BitwiseComplement usingSystem; namespaceOperator { classBitWiseComplement { publicstaticvoidMain(string[]args) { intnumber=26,result; result=~number; Console.WriteLine("~{0}={1}",number,result); } } } Whenweruntheprogram,theoutputwillbe: ~26=-27 Wegot-27asoutputwhenwewereexpecting229.Whydidthishappen? Ithappensbecausethebinaryvalue11100101whichweexpecttobe229isactuallya2'scomplementrepresentationof-27.Negativenumbersincomputerarerepresentedin2'scomplementrepresentation. Foranyintegern,2'scomplementofnwillbe-(n+1). 2'scomplement Decimal Binary 2'sComplement 0 00000000 -(11111111+1)=-00000000=-0(InDecimal) 1 00000001 -(11111110+1)=-11111111=-256(InDecimal) 229 11100101 -(00011010+1)=-00011011=-27 Overflowvaluesareignoredin2'scomplement. Thebitwisecomplementof26is229(indecimal)andthe2'scomplementof229is-27.Hencetheoutputis-27insteadof229. BitwiseLeftShift Bitwiseleftshiftoperatorisrepresentedby<<.the indecimal num forexample bitwiseliftshiftoperationon42: example5:bitwiseleftshift usingsystem namespaceoperator classleftshift publicstaticvoidmain intnumber="42;" console.writeline whenweruntheprogram bitwiserightshift bitwiseright>>.The>>operatorshiftsanumbertotherightbyaspecifiednumberofbits.Thefirstoperandisshiftedtorightbythenumberofbitsspecifiedbysecondoperand. Indecimal,itisequivalentto floor(num/2bits) ForExample, 42=101010(InBinary) BitwiseLiftShiftoperationon42: 42>>1=21(Inbinary010101) 42>>2=10(Inbinary001010) 42>>4=2(Inbinary000010) Example6:BitwiseRightShift usingSystem; namespaceOperator { classLeftShift { publicstaticvoidMain(string[]args) { intnumber=42; Console.WriteLine("{0}>>1={1}",number,number>>1); Console.WriteLine("{0}>>2={1}",number,number>>2); Console.WriteLine("{0}>>4={1}",number,number>>4); } } } Whenweruntheprogram,theoutputwillbe: 42>>1=21 42>>2=10 42>>4=2 TableofContents ListofC#BitOperators BitwiseOR BitwiseAND BitwiseXOR BitwiseComplement BitwiseLeftShift BitwiseRightShift PreviousTutorial: C#OperatorPrecedence NextTutorial: C#BasicI/O Shareon: Didyoufindthisarticlehelpful? Sorryaboutthat. Howcanweimproveit? Feedback* Leavethisfieldblank RelatedTutorialsC#TutorialC#OperatorsC#TutorialC#OperatorPrecedenceandAssociativityC#TutorialC#BasicInputandOutputC#TutorialC#ternary(?:)Operator
延伸文章資訊
- 1Xor 運算子
- 2c# - Conditional XOR? - Stack Overflow
There is no such thing as conditional (short-circuiting) XOR. Conditional operators are only mean...
- 3[C#][.NET]Exclusive OR(XOR)⊕ | 史丹利好熱 - - 點部落
部分密碼演算法(Algorithm)有特殊的邏輯運算需求,筆記常用的Exclusive OR(XOR)⊕,順便複習OR及AND運算差異。PIN Bl.
- 4C# Bitwise and Bit Shift Operators - Programiz
Bitwise XOR
- 5位元運算子(二進制) - iT 邦幫忙
... 與運算子(+、-、*、/、>、<....等)-來用C#算數學吧!(下). C#與ASP.Net入門-我要成為工程師!! 系列第9 篇 ... XOr(互斥), 當A跟B都是或都是1,結果...