We all are well aware of basic arithmetic operations like addition, subtraction, multiplication, division, and modulus. But, The real problem arrives when are told to do this operation without using their native operation signs.
So here In this article, we will see how we can do basic decimal number addition without using a plus(+) sign.
As you might have guessed, this will be done using bit operations. Bit manipulation is found very prominent to perform arithmetic operations without using their native signs.
About Basic Base 10 and Base 2 Addition
Before moving to learn how to do additions using bits, we need to understand some crucial points about base 10 and base 2 additions.
Base 10 addition is what we do in our day to day life. It includes taking the sum of each pair of digits from the given decimal numbers. If the sum of digits is greater than 10, then we take its modulus with 10 and write the reminder. Then forward the carry as 1. In the next pair summation of digits, we also include carry in the addition. We do this till we reach the leftmost number of given decimal numbers. In this way, we perform the addition of two numbers in base 10 addition.
Below is an example of base 10 addition:
Base 2 addition is what computers do internally between binary numbers to give out the addition of given decimal numbers. For performing base 2 addition we need to convert decimal numbers to the corresponding binary number first. It involves adding binary numbers in a pairwise manner with carries from left to right.
Here in base 2 addition, the rules are little different from what we saw in case of base 10 addition of decimal numbers.
In base 2 addition 1+1 = 10. then you need to take its modulo with 10. so the number we write below digits is the reminder that is 0 and carry become 1.
In base 2 addition 1+1+1 = 11. after taking modulo with 10, we the sum become 1 and carry is also 1.
Below is an example of base 2 addition:
Addition Without Plus(+) Sign in C++
As you have seen how computers do the addition of numbers internally using base 2 addition of binary numbers.
For performing addition without plus sign in c++, our task is to simulate that base 2 addition using bit operations.
We are trying to simulate base 2 addition, not base 10 because in computer memory all decimal numbers are processed after conversion into binary form.
Logic Behind Addition With Bit Operations in C++
If you have closing analyzed the base 2 addition operations above, you found that we are basically performing three operations repeatedly.
First finding the sum of digits, second finding the carry, and third shifting the carry by one leftward.
So, we will use three bit operators to perform these three tasks:
- XOR(^) for adding binary digits
- 1+0 = 1 = 1^0
- 0+1 = 1 = 0^1
- 0+0 = 0 = 0^0
- 1+1 = 0 = 1^1
- AND(&) for finding carries
- 1+0 = carry(0) = 1&0
- 0+1 = carry(0) = 0&1
- 0+0 = carry(0) = 0&0
- 1+1 = carry(1) = 1&1
- Left shift operator(<<) for aligning carrier with previous digits
As you can see XOR operator can be used to simulate the addition. AND operator can be used to find the carry and left shift can be used to align the carry on right position.
We continue to do this process until the carry becomes zero.
Below is the image explaining how the addition is performed with these three bit operators:
C++ Code of Addition Without Plus Sign
Below is the complete C++ or cpp code to perform addition without plus sign in C++:
//C++ program to perform addition without plus sign
#include<bits/stdc++.h>
using namespace std;
int main(){
//Two variable a and b whose sum we will calculate without using plus sign
int a, b;
cin>>a>>b;
//int x: For storing xor(or sum)
//int carry: For storing carry at each step
int x = 0, carry = 0;
//Initial calculation of xor and carry has been done here to initiate
//the while loop safely
x = a^b;
carry = a&b;
carry = carry<<1;
//temporary variable to store the xor value in each iteration
int temp = 0;
while(carry>0){
temp = carry^x;
carry = carry&x;
carry<<=1; //same as: carry = carry<<1;
x = temp;
}
//x gives the final sum as carry become zero
cout<<"Sum of "<<a<<" and "<<b<<": "<<x;
return 0;
}
It will not be hard to understand the code if you have understood the logic behind it, written in the above paras.
Explanation of C++ Code to do Addition Without Plus Sign
This C++ code to carry out addition is very similar to base 2 addition. The key difference is that in base 2 operation we calculate the carry and use it as we move leftwards. But, here we are basically piling up the carry and taking summing it with the xor(sum) of binaries in each iteration till the carry becomes zero.
First, we have taken the input numbers in a and b integer variables.
Made two variables x and carry to store xor of numbers and carry of numbers in each iteration.
For starting the loop with positive carry condition, we have done the first iteration before the loop.
Then we entered into the loop. In each iteration, we first calculated the xor and stored it into temp. Temp is used to assign the xor value to x at the end. Then we did an AND operation of the existing carry and x to calculate the carry for the next iteration.
In the end, we left-shifted(<<) the carry to align it on the correct position. And assigned the temp value to x.
In this way when the carry becomes finally zero, we get the sum as x.
Another program to perform the same task of adding two numbers without using plus operator in C++.
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b;
cin>>a>>b;
int carry;
while(b > 0){
carry = a&b;
a = a^b;
b = carry<<1;
}
cout<<a;
return 0;
}
Another recursive program to perform the same task of adding two numbers without using plus operator in C++.
#include<bits/stdc++.h>
using namespace std;
int add(int a, int b){
if(b == 0)
return a;
else
return add(a^b, (a&b)<<1);
}
int main(){
int a, b;
cin>>a>>b;
cout<<add(a, b);
return 0;
}
I hope, this explanation bought clarity to understand how addition can be done without the plus(+) sign in C++.
If you have any doubts related to code or explanation feel to comment below.