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).
Method 1:
C++
#include <iostream>
using namespace std;
int add( int a, int b)
{
for ( int i = 1; i <= b; i++)
a++;
return a;
}
int main()
{
int a = add(10, 32);
cout << a;
return 0;
}
|
C
#include <stdio.h>
int add( int a, int b)
{
for ( int i = 1; i <= b; i++)
a++;
return a;
}
int main()
{
int a = add(10, 32);
printf ( "%d" , a);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int add( int a, int b)
{
for ( int i = 1 ; i <= b; i++)
a++;
return a;
}
public static void main(String[] args)
{
int a = add( 10 , 32 );
System.out.print(a);
}
}
|
Python3
def add(a, b):
for i in range ( 1 , b + 1 ):
a = a + 1
return a
a = add( 10 , 32 )
print (a)
|
C#
using System;
public class GFG {
static int add( int a, int b) {
for ( int i = 1; i <= b; i++)
{
a++;
}
return a;
}
public static void Main(String[] args)
{
int a = add(10, 32);
Console.Write(a);
}
}
|
Javascript
<script>
function add(a , b)
{
for (i = 1; i <= b; i++)
{
a++;
}
return a;
}
var a = add(10, 32);
document.write(a);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int Add( int x, int y)
{
while (y != 0)
{
unsigned carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
int main()
{
cout << Add(15, 32);
return 0;
}
|
C
#include<stdio.h>
int Add( int x, int y)
{
while (y != 0)
{
unsigned carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
int main()
{
printf ( "%d" , Add(15, 32));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int Add( int x, int y)
{
while (y != 0 )
{
int carry = x & y;
x = x ^ y;
y = carry << 1 ;
}
return x;
}
public static void main(String arg[])
{
System.out.println(Add( 15 , 32 ));
}
}
|
Python3
def Add(x, y):
while (y ! = 0 ):
carry = x & y
x = x ^ y
y = carry << 1
return x
print (Add( 15 , 32 ))
|
C#
using System;
class GFG
{
static int Add( int x, int y)
{
while (y != 0)
{
int carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
public static void Main()
{
Console.WriteLine(Add(15, 32));
}
}
|
Javascript
<script>
function Add(x, y) {
while (y != 0)
{
let carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
document.write(Add(15, 32));
</script>
|
PHP
<?php
function Add( $x , $y )
{
while ( $y != 0)
{
$carry = $x & $y ;
$x = $x ^ $y ;
$y = $carry << 1;
}
return $x ;
}
echo Add(15, 32);
?>
|
Time Complexity: O(log y)
Auxiliary Space: O(1)
Last Updated :
29 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...