Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, .. etc).
Method 1:
C++
| #include <iostream>usingnamespacestd;intadd(inta, intb){    // for loop will start from 1 and move till the value of    // second number , first number(a) is incremented in for    // loop    for(inti = 1; i <= b; i++)        a++;    returna;}intmain(){    // 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    inta = add(10, 32);    cout << a;    return0;}// This code is contributed by Aditya Kumar (adityakumar129) | 
C
| #include <stdio.h>intadd(inta, intb){    // for loop will start from 1 and move till the value of    // second number , first number(a) is incremented in for    // loop    for(inti = 1; i <= b; i++)        a++;    returna;}intmain(){    // 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    inta = add(10, 32);    printf("%d", a);    return0;}// This code is contributed by Aditya Kumar (adityakumar129) | 
Java
| importjava.util.*;classGFG {    staticintadd(inta, intb)    {        // for loop will start from 1 and move till the        // value of second number , first number(a) is        // incremented in for loop        for(inti = 1; i <= b; i++)            a++;        returna;    }    publicstaticvoidmain(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        inta = add(10, 32);        System.out.print(a);    }}// This code is contributed by Aditya Kumar (adityakumar129) | 
Python3
| # Python implementationdefadd(a, b):    # for loop will start from 1 and move till the value of second number ,    # first number(a) is incremented in for loop    fori inrange(1, b +1):        a =a +1    returna# 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) | 
C#
| usingSystem;publicclassGFG {  staticintadd(inta, intb) {    for(inti = 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++;    }    returna;  }  publicstaticvoidMain(String[] args)   {    inta = 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  | 
Javascript
| <script>    functionadd(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++;        }        returna;    }    // first number is 10 and second number is 32 , for loop will start    vara = 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> | 
42
Time Complexity: O(b)
Auxiliary Space: O(1)
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++ Program to add two numbers // without using arithmetic operator #include <bits/stdc++.h>usingnamespacestd;intAdd(intx, inty) {     // 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;     }     returnx; } // Driver codeintmain() {     cout << Add(10, 32);     return0; } // This code is contributed by rathbhupendra | 
C
| // C Program to add two numbers// without using arithmetic operator#include<stdio.h>intAdd(intx, inty){    // 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;    }    returnx;}intmain(){    printf("%d", Add(15, 32));    return0;} | 
Java
| // Java Program to add two numbers// without using arithmetic operatorimportjava.io.*;classGFG {    staticintAdd(intx, inty)    {        // Iterate till there is no carry        while(y != 0)         {            // carry now contains common            // set bits of x and y            intcarry = 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;        }        returnx;    }        // Driver code    publicstaticvoidmain(String arg[])     {        System.out.println(Add(15, 32));    }}// This code is contributed by Anant Agarwal. | 
Python3
| # Python3 Program to add two numbers# without using arithmetic operatordefAdd(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        returnxprint(Add(15, 32))# This code is contributed by# Smitha Dinesh Semwal | 
C#
| // C# Program to add two numbers// without using arithmetic operatorusingSystem;classGFG {    staticintAdd(intx, inty)    {        // Iterate till there is no carry        while(y != 0)         {            // carry now contains common            // set bits of x and y            intcarry = 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;        }        returnx;    }        // Driver code    publicstaticvoidMain()     {        Console.WriteLine(Add(15, 32));    }}// This code is contributed by vt_m. | 
Javascript
| <script>// Javascript Program to add two numbers // without using arithmetic operator       functionAdd(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;         }         returnx;     }        //driver code    document.write(Add(15, 32));  // This code is contributed by Surbhi Tyagi </script> | 
PHP
| <?php// PHP Program to add two numbers// without using arithmetic operatorfunctionAdd( $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    echoAdd(15, 32);// This code is contributed by anuj_67.?> | 
42
Time Complexity: O(log y)
Auxiliary Space: O(1)
Following is the recursive implementation for the same approach.
C++
| intAdd(intx, inty){    if(y == 0)        returnx;    else        returnAdd( x ^ y,(unsigned) (x & y) << 1);}// This code is contributed by shubhamsingh10 | 
C
| intAdd(intx, inty){    if(y == 0)        returnx;    else        returnAdd( x ^ y, (unsigned)(x & y) << 1);} | 
Java
| staticintAdd(intx, inty){  if(y == 0)    returnx;  else    returnAdd(x ^ y, (x & y) << 1);}// This code is contributed by subham348 | 
Python3
| defAdd(x, y):      if(y ==0):        returnx    else:        returnAdd( x ^ y, (x & y) << 1)    # This code is contributed by subhammahato348 | 
C#
| staticintAdd(intx, inty){  if(y == 0)    returnx;  else    returnAdd(x ^ y, (x & y) << 1);}// This code is contributed by subhammahato348 | 
Javascript
| functionAdd(x, y){    if(y == 0)        returnx;    else        returnAdd(x ^ y, (x & y) << 1);}// This code is contributed by Ankita saini | 
Time Complexity: O(log k), where k=x&y
Auxiliary Space: O(log k).
Another Method:
This method also add 2 numbers without using any arithmetic operators.
Steps:
Follow the given steps to solve the problem:
- First calculate 2 to the power given numbers.
- Then just use log (base 2) which returns their sum.
- Finally return the ans.
Below is the Implementation of the above approach:
C++
| // CPP code of the above approach#include <bits/stdc++.h>usingnamespacestd;// Function to add 2 numbers using log and powerintAdd(inta, intb){    intans = log2(pow(2, a) * pow(2, b));    returnans;}intmain(){    inta = 10, b = 5;    cout << Add(a, b) << endl;    return0;}// This code is contributed by Susobhan Akhuli | 
Java
| // Java code of the above approachimportjava.lang.Math;publicclassGFG {    // Function to add 2 numbers using log and power    staticintadd(inta, intb)    {        intans = (int)(Math.log(Math.pow(2, a)                                 * Math.pow(2, b))                        / Math.log(2));        returnans;    }    publicstaticvoidmain(String[] args)    {        inta = 10, b = 5;        System.out.println(add(a, b));    }}// This code is contributed by Susobhan Akhuli | 
15
Time Complexity: O(log N), for pow function.
Auxiliary Space: O(1).
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

 
                                    







