Bitwise Operators - C++ - Codecademy

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

C++ supports different types of bitwise operators that can perform operations on integers at bit-level. SkiptoContentMiniArrowDownIconC++Concepts→C++ConceptsMiniChevronDownIconArraysBitwiseOperatorsClassesCommentsConditionalsConstructorsDataTypesEncapsulationEnumsErrorsExceptionsFilesFunctionsInheritanceLoopsMacrosMapsMathFunctionsMethodsObjectsOperatorsPointersPolymorphismQueuesRandomReferencesSetsSmartPointersStringsStructuresSwitchUserInputVariablesVectorsBacktoC++Docs/C++/BitwiseOperatorsBitwiseOperatorsC++supportsdifferenttypesofbitwiseoperatorsthatcanperformoperationsonintegersatbit-level.Supportedtypesofbitwiseoperatorsinclude: &BitwiseAND |BitwiseOR <>BitwiseRightShift ~BitwiseComplement ^BitwiseXOR &BitwiseAND ThebitwiseANDoperator,&: Returns1ifboththebitsare1(1,1). Else((1,0),(0,1),(0,0)),itreturns0. Forexample: 4=100(base-2binarysystem) 5=101(base-2binarysystem) 100 101 ___ 100,whichisequalto4inthebase-10decimalsystem #include intmain(){std::cout< intmain(){std::cout< intmain(){std::cout<>BitwiseRightShift Therightshiftoperatorshiftsallthebitstowardstherightsidebythenumberofbitsspecifiedanddiscardstherightmostbits. Forexample: 4=100(base-2binarysystem) 4>>1 0+100-0 Intheexampleabove,0isaddedtotheleftsideof4(100inbase-2).Then,therightmostbitisremoved,whichyields 10(base-2)or2(base-10). 4<<2 00+100+00 Intheexampleabove,00isaddedtotheleftsideof4(100inbase-2).Then,thetworightmostbitsareremoved,whichyields 1(base-2)or1(base-10). #include intmain(){std::cout<>1)<>2)<-6inbase-10binarysystem. unsigned(5)=00000000000000000000000000000101inbase-2binarysystem ~5=11111111111111111111111111111010inbase-2binarysystem =>4294967290inbase-10binarysystem Thefirstbitofasignedintegerisusedtodeterminewhethertheresultispositiveornegative.Therefore,asigned5andanunsigned5yielddifferentresults. #include intmain(){std::cout< intmain(){std::cout<



請為這篇文章評分?