This is a C++ Program to perform addition using bitwise operators. Using AND and XOR operators addition can be done, where carry is given by AND between two operands and result can be given by XOR between two operands.
Here is source code of the C++ Program to Perform Addition Operation Using Bitwise Operators. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int add(int x, int y)
{
int carry;
while (y != 0)
{
carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
int main(int argc, char **argv)
{
cout << "Enter the numbers to be added:";
int x, y;
cin >> x >> y;
cout << "The Summation is: " << add(x, y);
}
Output:
$ g++ BitWiseAddition.cpp $ a.out Enter the numbers to be added:23 24 The Summation is: 47
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C++ Internship
- Practice Programming MCQs
- Check C++ Books