Bitwise operators in C with Examples - Linux Hint
文章推薦指數: 80 %
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>
延伸文章資訊
- 1Bitwise Operator in C - javatpoint
Bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are writte...
- 2Bitwise Operators in C: AND, OR, XOR, Shift & Complement
Bitwise operators are special operator set provided by 'C.' · They are used in bit level programm...
- 3Bitwise operations in C - Wikipedia
In the C programming language, operations can be performed on a bit level using bitwise operators...
- 4Bitwise Operators in C [With Coding Example] | upGrad blog
- 5Bitwise operations in C - Wikipedia