Add two numbers without using arithmetic operators
Last Updated :
11 Sep, 2024
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 first number = (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>
using namespace std;
// function to add two numbers without using arithmetic operators
int sum(int a, int b) {
// Iterate till there is no carry
while (b != 0) {
// carry contains common set bits of a and b, left shifted by 1
int carry = (a & b) << 1;
// Update a with (a + b without carry)
a = a ^ b;
// Update b with carry
b = carry;
}
return a;
}
int main() {
int a = -1, b = 2;
cout << sum(a, b);
return 0;
}
C
// C Program to add two numbers without using arithmetic operators
#include <stdio.h>
// function to add two numbers without using arithmetic operators
int sum(int a, int b) {
// 32 bit mask in hexadecimal
long mask = 0xffffffff;
// Iterate till there is no carry
while ((b & mask) != 0) {
// carry contains common set bits of a and b, left shifted by 1
int carry = ((a & b) & mask) << 1;
// Update a with (a + b without carry)
a = a ^ b;
// Update b with carry
b = carry;
}
return a & mask;
}
int main() {
int a = -1, b = 2;
printf("%d\n", sum(a, b));
return 0;
}
Java
// Java Program to add two numbers without using arithmetic operators
class GfG {
// function to add two numbers without using arithmetic operators
static int sum(int a, int b) {
// Iterate till there is no carry
while (b != 0) {
// carry contains common set bits of a and b, left shifted by 1
int carry = (a & b) << 1;
// Update a with (a + b without carry)
a = a ^ b;
// Update b with carry
b = carry;
}
return a;
}
public static void main(String[] args) {
int a = -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 operators
def sum(a, b):
# 32 bit mask in hexadecimal
mask = 0xffffffff
# Iterate till there is no carry
while (b & mask) != 0:
# carry contains common set bits of a and b, left shifted by 1
carry = (a & b) << 1
# Update a with (a + b without carry)
a = a ^ b
# Update b with carry
b = carry
return a & mask if b > 0 else a
a = -1
b = 2
print(sum(a, b))
C#
// C# Program to add two numbers without using arithmetic operators
using System;
class GfG {
// function to add two numbers without using arithmetic operators
static int Sum(int a, int b) {
// Iterate till there is no carry
while (b != 0) {
// carry contains common set bits of a and b, left shifted by 1
int carry = (a & b) << 1;
// Update a with (a + b without carry)
a = a ^ b;
// Update b with carry
b = carry;
}
return a;
}
static void Main() {
int a = -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 operators
function sum(a, b) {
// Iterate till there is no carry
while (b !== 0) {
// carry contains common set bits of a and b, left shifted by 1
let carry = (a & b) << 1;
// Update a with (a + b without carry)
a = a ^ b;
// Update b with carry
b = carry;
}
return a;
}
const a = -1, b = 2;
console.log(sum(a, b));
Time Complexity: O(max(number of bits in a, number of bits in b))
Auxiliary Space: O(1)
Similar Reads
To find sum of two numbers without using any operator Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used.Solution It's a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in
9 min read
How to sum two integers without using arithmetic operators in C/C++? Given two integers a and b, how can we evaluate the sum a + b without using operators such as +, -, ++, --, ...? Method 1 (Using pointers)An interesting way would be: CPP // May not work with C++ compilers and // may produce warnings in C. // Returns sum of 'a' and 'b' int sum(int a, int b) { char *
4 min read
C++ program to divide a number by 3 without using *, / , +, -, % operators For a given positive number we need to divide a number by 3 without using any of these *, /, +, â % operatorsExamples: Input : 48 Output : 16 Input : 16 Output : 5 Algorithm Take a number num, sum = 0while(num>3), shift the number left by 2 bits and sum = add(num >> 2, sum). Create a functi
2 min read
Add two numbers using ++ and/or -- Given two numbers, return a sum of them without using operators + and/or -, and using ++ and/or --.Examples: Input: x = 10, y = 5 Output: 15 Input: x = 10, y = -5 Output: 10 We strongly recommend you to minimize your browser and try this yourself first The idea is to do y times x++, if y is positive
4 min read
Addition of two numbers without carry You are given two positive numbers n and m. You have to find a simple addition of both numbers but with a given condition that there is not any carry system in this addition. That is no carry is added at higher MSBs.Examples : Input : m = 456, n = 854 Output : 200 Input : m = 456, n = 4 Output : 450
6 min read
Implement *, - and / operations using only + arithmetic operator Given two numbers, perform multiplication, subtraction, and division operations on them, using '+' arithmetic operator only. Operations can be performed as follows: Subtraction :- a - b = a + (-1)*b. Multiplication :- a * b = a + a + a ... b times. Division :- a / b = continuously subtract b from a
12 min read
Javascript program to swap two numbers without using temporary variable To swap two numbers without using a temporary variable, we have multiple approaches. In this article, we are going to learn how to swap two numbers without using a temporary variable. Below are the approaches used to swap two numbers without using a temporary variable: Table of Content Using Arithme
6 min read
Arithmetic operations with std::bitset in C++ A bitset is an array of boolean values, but each boolean value is not stored separately. Instead, bitset optimizes the space such that each bool takes 1-bit space only, so space taken by bitset say, bs is less than that of bool bs[N] and vector<bool> bs(N). However, a limitation of bitset is,
3 min read
Swap Two Numbers Without Using Third Variable Given two variables a and y, swap two variables without using a third variable. Examples: Input: a = 2, b = 3Output: a = 3, b = 2Input: a = 20, b = 0Output: a = 0, b = 20Input: a = 10, b = 10Output: a = 10, b = 10Table of ContentUsing Arithmetic OperatorsUsing Bitwise XORBuilt-in SwapUsing Arithmeti
6 min read
Multiply two integers without using multiplication, division and bitwise operators, and no loops By making use of recursion, we can multiply two integers with the given constraints. To multiply x and y, recursively add x y times. Approach: Since we cannot use any of the given symbols, the only way left is to use recursion, with the fact that x is to be added to x y times. Base case: When the nu
9 min read