Bitwise Operators in C Programming - Programiz

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

Bitwise XOR CourseIndex ExploreProgramiz Python JavaScript SQL C C++ Java Kotlin Swift C# DSA LearnCpractically andGetCertified. ENROLL PopularTutorials DataTypesinC Cif...elseStatement CforLoop ArraysinCProgramming PointersinC StartLearningC PopularExamples Checkodd/evennumber Findrootsofaquadraticequation PrintPyramidsandPatterns Checkprimenumber PrinttheFibonacciseries ExploreCExamples ReferenceMaterials string.h math.h ctype.h Viewall LearningPaths Challenges LearnCInteractively TryforFree Courses BecomeaCMaster BecomeaPythonMaster BecomeaJavaMaster ViewallCourses Python JavaScript SQL C C++ Java Kotlin Swift C# DSA LearnCpractically andGetCertified. ENROLLFORFREE! PopularTutorials DataTypesinC Cif...elseStatement CforLoop ArraysinCProgramming PointersinC StartLearningC AllCTutorials ReferenceMaterials string.h math.h ctype.h Viewall Python JavaScript C C++ Java Kotlin LearnCpractically andGetCertified. ENROLLFORFREE! PopularExamples Checkodd/evennumber Findrootsofaquadraticequation PrintPyramidsandPatterns Checkprimenumber PrinttheFibonacciseries AllCExamples InteractiveCCourse CIntroduction Keywords&Identifier Variables&Constants CDataTypes CInput/Output CComments COperators CIntroductionExamples CFlowControl Cif...else CforLoop CwhileLoop Cbreakandcontinue Cswitch...case CProgramminggoto ControlFlowExamples CFunctions CProgrammingFunctions CUser-definedFunctions CFunctionTypes CRecursion CStorageClass CFunctionExamples CProgrammingArrays CProgrammingArrays CMulti-dimensionalArrays CArrays&Function CProgrammingPointers CProgrammingPointers CPointers&Arrays CPointersAndFunctions CMemoryAllocation Array&PointerExamples CProgrammingStrings CProgrammingString CStringFunctions CStringExamples StructureAndUnion CStructure CStruct&Pointers CStruct&Function CUnions CstructExamples CProgrammingFiles CFilesInput/Output CFilesExamples AdditionalTopics CEnumeration CPreprocessors CStandardLibrary CProgrammingExamples RelatedTopics CPrecedenceAndAssociativityOfOperators CProgrammingOperators ConvertBinaryNumbertoDecimalandvice-versa Cenums ConvertBinaryNumbertoOctalandvice-versa MakeaSimpleCalculatorUsingswitch...case BitwiseOperatorsinCProgramming Inthistutorialyouwilllearnaboutall6bitwiseoperatorsinCprogrammingwithexamples. Inarithmetic-logicunit(whichiswithintheCPU),mathematicaloperationslike:addition,subtraction,multiplicationanddivisionaredoneinbit-level.Toperformbit-leveloperationsinCprogramming,bitwiseoperatorsareused. Operators Meaningofoperators & BitwiseAND | BitwiseOR ^ BitwiseXOR ~ Bitwisecomplement << Shiftleft >> Shiftright BitwiseANDoperator& TheoutputofbitwiseANDis1ifthecorrespondingbitsoftwooperandsis1.Ifeitherbitofanoperandis0,theresultofcorrespondingbitisevaluatedto0. LetussupposethebitwiseANDoperationoftwointegers12and25. 12=00001100(InBinary) 25=00011001(InBinary) BitOperationof12and25 00001100 &00011001 ________ 00001000=8(Indecimal) Example#1:BitwiseAND #include intmain() { inta=12,b=25; printf("Output=%d",a&b); return0; } Output Output=8 BitwiseORoperator| TheoutputofbitwiseORis1ifatleastonecorrespondingbitoftwooperandsis1.InCProgramming,bitwiseORoperatorisdenotedby|. 12=00001100(InBinary) 25=00011001(InBinary) BitwiseOROperationof12and25 00001100 |00011001 ________ 00011101=29(Indecimal) Example#2:BitwiseOR #include intmain() { inta=12,b=25; printf("Output=%d",a|b); return0; } Output Output=29 BitwiseXOR(exclusiveOR)operator^ TheresultofbitwiseXORoperatoris1ifthecorrespondingbitsoftwooperandsareopposite.Itisdenotedby^. 12=00001100(InBinary) 25=00011001(InBinary) BitwiseXOROperationof12and25 00001100 ^00011001 ________ 00010101=21(Indecimal) Example#3:BitwiseXOR #include intmain() { inta=12,b=25; printf("Output=%d",a^b); return0; } Output Output=21 Bitwisecomplementoperator~ Bitwisecomplimentoperatorisanunaryoperator(worksononlyoneoperand).Itchanges1to0and0to1.Itisdenotedby~. 35=00100011(InBinary) BitwisecomplementOperationof35 ~00100011 ________ 11011100=220(Indecimal) TwistinbitwisecomplementoperatorinCProgramming Thebitwisecomplementof35(~35)is-36insteadof220,butwhy? Foranyintegern,bitwisecomplementofnwillbe-(n+1).Tounderstandthis,youshouldhavetheknowledgeof2'scomplement. 2'sComplement Two'scomplementisanoperationonbinarynumbers.The2'scomplementofanumberisequaltothecomplementofthatnumberplus1.Forexample: DecimalBinary2'scomplement 000000000-(11111111+1)=-00000000=-0(decimal) 100000001-(11111110+1)=-11111111=-256(decimal) 1200001100-(11110011+1)=-11110100=-244(decimal) 22011011100-(00100011+1)=-00100100=-36(decimal) Note:Overflowisignoredwhilecomputing2'scomplement. Thebitwisecomplementof35is220(indecimal).The2'scomplementof220is-36.Hence,theoutputis-36insteadof220. BitwisecomplementofanynumberNis-(N+1).Here'show: bitwisecomplementofN=~N(representedin2'scomplementform) 2'complementof~N=-(~(~N)+1)=-(N+1) Example#4:Bitwisecomplement #include intmain() { printf("Output=%d\n",~35); printf("Output=%d\n",~-12); return0; } Output Output=-36 Output=11 ShiftOperatorsinCprogramming TherearetwoshiftoperatorsinCprogramming: Rightshiftoperator Leftshiftoperator. RightShiftOperator Rightshiftoperatorshiftsallbitstowardsrightbycertainnumberofspecifiedbits.Itisdenotedby>>. 212=11010100(Inbinary) 212>>2=00110101(Inbinary)[Rightshiftbytwobits] 212>>7=00000001(Inbinary) 212>>8=00000000 212>>0=11010100(NoShift) LeftShiftOperator Leftshiftoperatorshiftsallbitstowardsleftbyacertainnumberofspecifiedbits.Thebitpositionsthathavebeenvacatedbytheleftshiftoperatorarefilledwith0.Thesymboloftheleftshiftoperatoris<<. example> intmain() { intnum=212,i; for(i=0;i<=2;++i) printf("Rightshiftby%d:%d\n",i,num>>i); printf("\n"); for(i=0;i<=2;++i) printf("Leftshiftby%d:%d\n",i,num<



請為這篇文章評分?