Given two integers a and b, the task is to find the sum of a and b without using + or - operators.
Examples:
Input: a = 10, b = 30 Output: 40
Input: a = -1, b = 2 Output: 1
Approach:
The approach is to add two numbers using bitwise operations. Let's first go through some observations:
a & b will have only those bits set which are set in both a and b.
a ^ b will have only those bits set which are set in either a or b but not in both.
If we want to calculate the sum of a and b such that a and b has no common set bit, then a ^ b is same as a + b. So, we can say that a + b without carry = a ^ b.
How to handle the case when we have carry, that is a and b has common set bits?
To calculate the carry, we know that carry will only have the common set bits of a and b, shifted 1 place to the left. So, we can say that carry = (a & b) << 1.
Now, the problem is reduced to calculating the sum of (a + b without carry) and carry. This is again the same problem: sum of two numbers where the firstnumber = (a + b without carry) and second number = carry. So, we can repeatedly calculate carry and sum without carry, till carry is not reduced to 0.
Working:
Note: The above approach will work perfectly for languages like C++, Java, C# and JavaScript in which an integer has a size of 32 bits but in Python, we need to handle the overflow separately by creating a 32-bit mask of 1's.
Below is the implementation of the above algorithm:
C++
// C++ Program to add two numbers without using arithmetic operators #include<bits/stdc++.h>usingnamespacestd;// function to add two numbers without using arithmetic operatorsintsum(inta,intb){// Iterate till there is no carry while(b!=0){// carry contains common set bits of a and b, left shifted by 1intcarry=(a&b)<<1;// Update a with (a + b without carry)a=a^b;// Update b with carryb=carry;}returna;}intmain(){inta=-1,b=2;cout<<sum(a,b);return0;}
C
// C Program to add two numbers without using arithmetic operators #include<stdio.h>// function to add two numbers without using arithmetic operatorsintsum(inta,intb){// 32 bit mask in hexadecimallongmask=0xffffffff;// Iterate till there is no carry while((b&mask)!=0){// carry contains common set bits of a and b, left shifted by 1intcarry=((a&b)&mask)<<1;// Update a with (a + b without carry)a=a^b;// Update b with carryb=carry;}returna&mask;}intmain(){inta=-1,b=2;printf("%d\n",sum(a,b));return0;}
Java
// Java Program to add two numbers without using arithmetic operators classGfG{// function to add two numbers without using arithmetic operatorsstaticintsum(inta,intb){// Iterate till there is no carry while(b!=0){// carry contains common set bits of a and b, left shifted by 1intcarry=(a&b)<<1;// Update a with (a + b without carry)a=a^b;// Update b with carryb=carry;}returna;}publicstaticvoidmain(String[]args){inta=-1,b=2;System.out.println(sum(a,b));}}
Python
# Python Program to add two numbers without using arithmetic operators # function to add two numbers without using arithmetic operatorsdefsum(a,b):# 32 bit mask in hexadecimalmask=0xffffffff# Iterate till there is no carry while(b&mask)!=0:# carry contains common set bits of a and b, left shifted by 1carry=(a&b)<<1# Update a with (a + b without carry)a=a^b# Update b with carryb=carryreturna&maskifb>0elseaa=-1b=2print(sum(a,b))
C#
// C# Program to add two numbers without using arithmetic operators usingSystem;classGfG{// function to add two numbers without using arithmetic operatorsstaticintSum(inta,intb){// Iterate till there is no carry while(b!=0){// carry contains common set bits of a and b, left shifted by 1intcarry=(a&b)<<1;// Update a with (a + b without carry)a=a^b;// Update b with carryb=carry;}returna;}staticvoidMain(){inta=-1,b=2;Console.WriteLine(Sum(a,b));}}
JavaScript
// JavaScript Program to add two numbers without using arithmetic operators // function to add two numbers without using arithmetic operatorsfunctionsum(a,b){// Iterate till there is no carry while(b!==0){// carry contains common set bits of a and b, left shifted by 1letcarry=(a&b)<<1;// Update a with (a + b without carry)a=a^b;// Update b with carryb=carry;}returna;}consta=-1,b=2;console.log(sum(a,b));
Output
1
Time Complexity: O(max(number of bits in a, number of bits in b)) Auxiliary Space: O(1)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.