Bitwise recursive addition of two integers in C - Tutorialspoint
文章推薦指數: 80 %
Bitwise recursive addition of two integers in C ; Step 1 − Find XOR of a and b i.e. a^b and store it in the result variable. ; Step 2 − Check if ...
Home
CodingGround
Jobs
Whiteboard
Tools
Business
Teachwithus
TrendingCategories
DataStructure
Networking
RDBMS
OperatingSystem
Java
iOS
HTML
CSS
Android
Python
CProgramming
C++
C#
MongoDB
MySQL
Javascript
PHP
SelectedReading
UPSCIASExamsNotes
Developer'sBestPractices
QuestionsandAnswers
EffectiveResumeWriting
HRInterviewQuestions
ComputerGlossary
WhoisWho
BitwiserecursiveadditionoftwointegersinC
CServerSideProgrammingProgramming
Inthisproblem,wearegiventwonumbers.OurtaskistocreateaCprogramfortheBitwiserecursiveadditionoftwointegers.ThelogictofindthesumusingtheBitwiseoperationsissimilartowhatweusedtodowhenwewereinpreschool.Forfindingthesum,weusedtoaddeachdigitofthenumberandifacarryisthere,weaddittothenextdigit.Wewilldoasimilarthing,findthesumusingtheXORoperatorandcheckforthecarryusingtheANDoperation.Ifthereisacarrywewilladditbacktothenumberotherwisenot.ThisisthelogicofaHalf-Adder whichyoumighthavelearnedindigitalElectronics.Referhere…Now,Thesumiscalculatedusinga^bi.e.anXORbandweneedtocheckforanextracarrythatneedstobepropagatedifthefirstbitofbothissetorso.Andweneedtoaddanextrasetbittothenumber.So,abitalgorithmwillbeStep1−FindXORofaandbi.e.a^bandstoreitintheresultvariable.Step2−Checkif{(a&b)<<1}==0Step2.1−Ifitisequalto0,thenprinttheresult,itisthefinalresult.Step2.2−Ifitisnotequalto0,thengotostep1,witha={(a&b)<<1}andb=result.ExampleProgramtoillustratetheworkingofthealgorithm− LiveDemo#include
延伸文章資訊
- 1Bitwise Operators in C Programming - Programiz
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Progra...
- 2C++ Program to Perform Addition Operation Using Bitwise ...
Bitwise operators are used to perform bitwise operations. That implies the manipulation of bits. ...
- 3Add two numbers without using arithmetic operators
If x and y don't have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum...
- 4Bitwise operation - Wikipedia
Truth table for all binary logical operators
- 5Addition using Bitwise Operations - OpenGenus IQ
Adding two numbers using bitwise operators · Get two positive numbers a and b as input · Then che...