Bitwise operators in C with Examples - Linux Hint

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

Bitwise AND Operator · Bitwise OR Operator · Bitwise XOR Operator · Bitwise NOT Operator · Bitwise Right and Left Shift Operators · Conclusion. Thebitwiseoperatorshavebeenusedtomanipulatedataonlyatthebitlevel.Bit-levelcomputingwouldbedonewhenyouexecutebitwiseoperations.Itcomprisestwonumbers,oneofwhichis0andtheotheroneis1.It’smainlyusedtospeedupmathematicalcalculations.InsidetheClanguage,weuseseveralsortsofbitwiseoperatorsasbelow.Let’sdiscusseachoneofthemonebyone.Atthetimeofimplementingthisguidearticle,wehavebeenworkingonUbuntu20.04Linuxsystem.WehavesuccessfullyloggedinfromthesystemandinstalledtheGCCcompilertocompileClanguagecode.Withoutacompiler,weareunabletoexecuteourCscript.Afterthat,wehaveusedtheCtrl+Alt+Tkeyshortcuttoopentheterminalshell,aswehavebeendoingeveryimplementationandexecutiononashell. BitwiseANDOperator So,ourfirstexamplewouldbeofbitwiseANDoperator.Whenthematchingbitsoftwonumbersoroperandsare1,theoperatorANDis1.Ifoneoftheoperand’sbitsis0,theoutputofsuchassociatedbitisalso0.Openyourterminal,andusethetouchquerytocreateaC-typefileinitasbelow. $touchmain.c YoucanfindyourfileinyourHomedirectory.Openthisfilebyfollowingthebelowinstruction. $nanomain.c NowthefilehasbeenopenedintheGNUeditor,writethebelowsimpleCcode.Thiscodecontainsaprimaryfunctionwiththelibraryincludedforstandardinputandoutput.Afterthat,wehavedeclaredanintegertypevariable“a”intheprimaryfunctionandassignitavalue.WehavethenputANDoperatorconditiononthevariable“a”alongwithanumber1.TheANDwillbecalculated,anditwilltellwhethertheoutputANDisevenorodd,andtheresultwillbeprintedout.PleasesavethefilebyCtrl+SandcloseitonceupdatedbyCtrl+X. Nowcompilethecodebygcccompilerinstructionasbelow.Ifgcccommandsoutputnothing,thismeansthatthecodeiscorrect.Nowexecutethefileby“a.out”instructionasbelow.TheoutputshowsthattheANDoperatorsprintOddasaresult. $gccmain.c $./a.out Let’stakeanotherANDoperator.OpenthesamefileagaintoupdatetheCcodeinit. $nanomain.c ThefileisopenedinGNUeditor.Let’supdatethecodewiththebelow-shownscript.Wetooktwointegertypevariables,“a”and“b.”Assignedboththevariableswithintegervalues.Intheprintstatement,wehaveusedtheAND“&”operatorbetweenbothoperandstoseetheANDresultonbothintegers.Itwillshowtheresultintheshell.Saveandclosethefile. CompileyournewlyupdatedfilewithGCCcompilerandrunitonaterminal.TheoutputshowstheANDresult,“2”intheterminalaspresentedinthepicture. $gccmain.c $./a.out BitwiseOROperator Inthissection,wewillbediscussingthefunctionofORbitwiseoperator.Whenatleastaminimumofonematchingbitamongtwonumbersis1,theresultofbitwiseORis1.Thebit-wiseORoperatorisusuallyrepresentedby“|”inClanguage.Openyourfileagaintoupdatethecode. $nanomain.c Wehaveinitializedtwointegertypevariables,“a”and“b,”withintegertypevaluesassignedtothem. Intheprintstatement,wehaveusedthe“|”operatortoapplyORbetweentheoperands“a”and“b.”Thentheresulthasbeenprintedoutviaprintfstatement,andtheprimaryfunctionendshere.Saveyourcodeandquitthefile. Compilationofthefilemain.chasbeendoneusingthegcccompilerviatheshell.Afterthecompilationgetssuccessful,wewillberunningthefileusingthe“a.out”fileinourterminal.TheoutputisshowingtheresultoftheORoperatoronbothoperandsasbelow. $gccmain.c $./a.out BitwiseXOROperator ThissectioncontainstheexampleoftheXORbitwiseoperator.Whentherespectivebitsoftwonumbersaredifferent,theoutputofbitwiseXORyields1.ThesymbolforXORis“^.”Hence,openyourfileonceagainusingthebelowcommand. $nanomain.c Updatethecodebytakingtheexactintegersandvalues“a”and“b.”Thedifferenceisassameasintheprintfstatement.Wehavechangedthesymbolbetweenboththeintegeroperandsandput“^.” ThissymbolrepresentsXOR,anditwillcalculatetheXORonboththeoperandsandshowstheresultintheterminal. Again,compilethemain.cfilefirst,andthenrunsthecodeonceagain.Compilationandexecutiongetsuccessful,anditreturns29duetoXOroperationonbothoperands. $gccmain.c $./a.out BitwiseNOTOperator ThebitwiseNOToperatorhasalsocalledthecomplementoperator.Theunarycomplementoperatorseemstobeabitwiseoperatorthatworksonsolitaryonenumberoroperand.Ittransforms1into0and0into1.Thesymbol“~symbolizesit.”OpenyourfileonceagaintoimplementtheNOToperator. $nanomain.c Thistimewehaveupdatedthesymbolto“~,”representingtheNOToperatororcomplement.Wehavegivenboththevariables,butbothhavenothingtodowiththeoperator. Compilationandexecutiongetsuccessfulandreturn“-26”asacomplementto“-25”. $gccmain.c $./a.out BitwiseRightandLeftShiftOperators Therightshiftoperatormoveseverybittotherightbyagivenamountofbits.“>>”isthesymbolforit.Theleftshiftoperatormoveseverybittotheleftbyafixedamountofbits.Thebitlocationswhichtheleftshiftoperatorhasabandonedwillbereplacedwith0.Theleftshiftoperatorhasbeenrepresentedbythesymbol“<<. inthisexample afterthecompilationofcode example belowisthebonusexampletoillustratetheworkingofalltheoperatorsata executionoftheabovecodeshowsthefollowingresult. conclusion wehavecoveredallthebitwiseoperatorsinourarticle abouttheauthor aqsayasin iamaself-motivatedinformationtechnologyprofessionalwithapassionforwriting.iamatechnicalwriterandlove towriteforalllinuxflavorsandwindows. viewallposts relatedlinuxhintposts two-dimensionalarrayincrelationaloperatorsincpointersinclogicaloperatorsincarrayinccommandlineargume ntuseofstrlen>



請為這篇文章評分?