Add two numbers without using arithmetic operators

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 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
int main() 
    cout << Add(15, 32); 
    return 0; 
  
// This code is contributed by rathbhupendra

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
        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;
}
  
int main()
{
    printf("%d", Add(15, 32));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to add two numbers
// without using arithmetic operator
import 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to add two numbers
# without using arithmetic operator
def 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to add two numbers
// without using arithmetic operator
using 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right


Output :

47

Following is the recursive implementation for the same approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

int Add(int x, int y)
{
    if (y == 0)
        return x;
    else
        return Add( x ^ y, (x & y) << 1);
}

chevron_right


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Improved By : vt_m, rathbhupendra