Add two numbers without using arithmetic operators - GeeksforGeeks

27 Apr, 2022
C++
C
Java
Python3
C#
Javascript
#include <iostream>using namespace std; int add(int a, int b){    // for loop will start from 1 and move till the value of    // second number , first number(a) is incremented in for    // loop    for (int i = 1; i <= b; i++)        a++;    return a;} int main(){    // first number is 10 and second number is 32 , for loop    // will start from 1 and move till 32 and the value of a    // is incremented 32 times which will give us the total    // sum of two numbers     int a = add(10, 32);    cout << a;    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
#include <stdio.h> int add(int a, int b){    // for loop will start from 1 and move till the value of    // second number , first number(a) is incremented in for    // loop    for (int i = 1; i <= b; i++)        a++;    return a;} int main(){    // first number is 10 and second number is 32 , for loop    // will start from 1 and move till 32 and the value of a    // is incremented 32 times which will give us the total    // sum of two numbers     int a = add(10, 32);    printf("%d", a);    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
import java.util.*; class GFG {     static int add(int a, int b)    {        // for loop will start from 1 and move till the        // value of second number , first number(a) is        // incremented in for loop        for (int i = 1; i <= b; i++)            a++;        return a;    }     public static void main(String[] args)    {        // first number is 10 and second number is 32 , for        // loop will start from 1 and move till 32 and the        // value of a is incremented 32 times which will        // give us the total sum of two numbers        int a = add(10, 32);        System.out.print(a);    }} // This code is contributed by Aditya Kumar (adityakumar129)
# Python implementationdef add(a, b):    # for loop will start from 1 and move till the value of second number ,    # first number(a) is incremented in for loop    for i in range(1, b + 1):        a = a + 1    return a  # driver code# first number is 10 and second number is 32 , for loop# will start from 1 and move till 32 and the value of a# is incremented 32 times which will give us the total# sum of two numbersa = add(10, 32)print(a) # This code is contributed by Aditya Kumar (adityakumar129)
using System;public class GFG {   static int add(int a, int b) {    for (int i = 1; i <= b; i++) // for loop will start from 1 and move till the value of second      // number , first number(a) is incremented in for loop    {      a++;    }    return a;  }   public static void Main(String[] args)  {     int a = add(10, 32); // first number is 10 and second number is 32 , for loop will start    Console.Write(a); // from 1 and move till 32 and the value of a is incremented 32 times    // which will give us the total sum of two numbers  }} // This code is contributed by Rajput-Ji
<script>    function add(a , b)    {         // for loop will start from 1 and move till the value of second        // number , first number(a) is incremented in for loop        for (i = 1; i <= b; i++)        {            a++;        }        return a;    }     // first number is 10 and second number is 32 , for loop will start    var a = add(10, 32);         // from 1 and move till 32 and the value of a is incremented 32 times    // which will give us the total sum of two numbers    document.write(a); // This code is contributed by Rajput-Ji</script>
Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, .. etc).Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits. 
Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result. 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ Program to add two numbers// without using arithmetic operator#include <bits/stdc++.h>using namespace std; int Add(int x, int y){    // Iterate till there is no carry    while (y != 0)    {        // carry should be unsigned to        // deal with -ve numbers        // carry now contains common        //set bits of x and y        unsigned carry = x & y;         // Sum of bits of x and y where at        //least one of the bits is not set        x = x ^ y;         // Carry is shifted by one so that adding        // it to x gives the required sum        y = carry << 1;    }    return x;} // Driver codeint main(){    cout << Add(15, 32);    return 0;} // This code is contributed by rathbhupendra
// C Program to add two numbers// without using arithmetic operator#include<stdio.h> int Add(int x, int y){    // Iterate till there is no carry     while (y != 0)    {        // carry now contains common        //set bits of x and y        unsigned carry = x & y;          // Sum of bits of x and y where at        //least one of the bits is not set        x = x ^ y;         // Carry is shifted by one so that adding        // it to x gives the required sum        y = carry << 1;    }    return x;} int main(){    printf("%d", Add(15, 32));    return 0;}
// Java Program to add two numbers// without using arithmetic operatorimport java.io.*; class GFG{    static int Add(int x, int y)    {        // Iterate till there is no carry        while (y != 0)        {            // carry now contains common            // set bits of x and y            int carry = x & y;             // Sum of bits of x and            // y where at least one            // of the bits is not set            x = x ^ y;             // Carry is shifted by            // one so that adding it            // to x gives the required sum            y = carry << 1;        }        return x;    }         // Driver code    public static void main(String arg[])    {        System.out.println(Add(15, 32));    }} // This code is contributed by Anant Agarwal.
# Python3 Program to add two numbers# without using arithmetic operatordef Add(x, y):     # Iterate till there is no carry    while (y != 0):             # carry now contains common        # set bits of x and y        carry = x & y         # Sum of bits of x and y where at        # least one of the bits is not set        x = x ^ y         # Carry is shifted by one so that          # adding it to x gives the required sum        y = carry << 1         return x print(Add(15, 32)) # This code is contributed by# Smitha Dinesh Semwal
// C# Program to add two numbers// without using arithmetic operatorusing System; class GFG{    static int Add(int x, int y)    {        // Iterate till there is no carry        while (y != 0)        {            // carry now contains common            // set bits of x and y            int carry = x & y;             // Sum of bits of x and            // y where at least one            // of the bits is not set            x = x ^ y;             // Carry is shifted by            // one so that adding it            // to x gives the required sum            y = carry << 1;        }        return x;    }         // Driver code    public static void Main()    {        Console.WriteLine(Add(15, 32));    }} // This code is contributed by vt_m.
<?php// PHP Program to add two numbers// without using arithmetic operator function Add( $x, $y){         // Iterate till there is    // no carry    while ($y != 0)    {                 // carry now contains common        //set bits of x and y        $carry = $x & $y;         // Sum of bits of x and y where at        //least one of the bits is not set        $x = $x ^ $y;         // Carry is shifted by one        // so that adding it to x        // gives the required sum        $y = $carry << 1;    }    return $x;}     // Driver Code    echo Add(15, 32); // This code is contributed by anuj_67.?>
<script> // Javascript Program to add two numbers// without using arithmetic operator        function Add(x, y) {        // Iterate till there is no carry          while (y != 0)        {            // carry now contains common             //set bits of x and y            let carry = x & y;                 // Sum of bits of x and y where at             //least one of the bits is not set            x = x ^ y;                // Carry is shifted by one so that adding            // it to x gives the required sum            y = carry << 1;        }        return x;    }        //driver code    document.write(Add(15, 32));  // This code is contributed by Surbhi Tyagi</script>
Output : 
47
Time Complexity: O(log y)
Auxiliary Space: O(1)
Following is the recursive implementation for the same approach.
C++
C
Java
Python3
C#
Javascript
int Add(int x, int y){    if (y == 0)        return x;    else        return Add( x ^ y,(unsigned) (x & y) << 1);} // This code is contributed by shubhamsingh10
int Add(int x, int y){    if (y == 0)        return x;    else        return Add( x ^ y, (unsigned)(x & y) << 1);}
static int Add(int x, int y){  if (y == 0)    return x;  else    return Add(x ^ y, (x & y) << 1);} // This code is contributed by subham348
def Add(x, y):       if (y == 0):        return x    else        return Add( x ^ y, (x & y) << 1)     # This code is contributed by subhammahato348
static int Add(int x, int y){  if (y == 0)    return x;  else    return Add(x ^ y, (x & y) << 1);} // This code is contributed by subhammahato348
function Add(x, y){    if (y == 0)        return x;    else        return Add(x ^ y, (x & y) << 1);} // This code is contributed by Ankita saini
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
rathbhupendra
surbhityagi15
subham348
subhammahato348
ankita_saini
SHUBHAMSINGH10
piyusht20
mozasuvesh
souravmahato348
sayam10
Rajput-Ji
GauravRajput1
shinjanpatra
adityakumar129
Bitwise-XOR
Bit Magic
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
Cyclic Redundancy Check and Modulo-2 Division
C++ Data Types
Set in C++ Standard Template Library (STL)
Merge two sorted arrays
Program to find GCD or HCF of two numbers
Modulo Operator (%) in C/C++ with Examples