Write a function subtract(x, y) that returns x-y where x and y are integers. The function should not use any of the arithmetic operators (+, ++, –, -, .. etc).
The idea is to use bitwise operators. Addition of two numbers has been discussed using Bitwise operators. Like addition, the idea is to use subtractor logic.
The truth table for the half subtractor is given below.
X Y Diff Borrow
0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 0
From the above table one can draw the Karnaugh map for “difference” and “borrow”.
So, Logic equations are:
Diff = y ⊕ x
Borrow = x' . y
Source: Wikipedia page for subtractor
Following is implementation based on above equations.
C++
#include <iostream>
using namespace std;
int subtract( int x, int y)
{
while (y != 0)
{
int borrow = (~x) & y;
x = x ^ y;
y = borrow << 1;
}
return x;
}
int main()
{
int x = 29, y = 13;
cout << "x - y is " << subtract(x, y);
return 0;
}
|
C
#include<stdio.h>
int subtract( int x, int y)
{
while (y != 0)
{
int borrow = (~x) & y;
x = x ^ y;
y = borrow << 1;
}
return x;
}
int main()
{
int x = 29, y = 13;
printf ( "x - y is %d" , subtract(x, y));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int subtract( int x, int y)
{
while (y != 0 )
{
int borrow = (~x) & y;
x = x ^ y;
y = borrow << 1 ;
}
return x;
}
public static void main (String[] args)
{
int x = 29 , y = 13 ;
System.out.println( "x - y is " +
subtract(x, y));
}
}
|
Python3
def subtract(x, y):
while (y ! = 0 ):
borrow = (~x) & y
x = x ^ y
y = borrow << 1
return x
x = 29
y = 13
print ( "x - y is" ,subtract(x, y))
|
C#
using System;
class GFG {
static int subtract( int x, int y)
{
while (y != 0)
{
int borrow = (~x) & y;
x = x ^ y;
y = borrow << 1;
}
return x;
}
public static void Main ()
{
int x = 29, y = 13;
Console.WriteLine( "x - y is " +
subtract(x, y));
}
}
|
PHP
<?php
function subtract( $x , $y )
{
while ( $y != 0)
{
$borrow = (~ $x ) & $y ;
$x = $x ^ $y ;
$y = $borrow << 1;
}
return $x ;
}
$x = 29; $y = 13;
echo "x - y is " , subtract( $x , $y );
?>
|
Javascript
<script>
function subtract(x, y)
{
while (y != 0)
{
let borrow = (~x) & y;
x = x ^ y;
y = borrow << 1;
}
return x;
}
let x = 29, y = 13;
document.write( "x - y is " + subtract(x, y));
</script>
|
Output :
x - y is 16
Time Complexity: O(log y)
Auxiliary Space: O(1)
Following is recursive implementation for the same approach.
C++
#include <iostream>
using namespace std;
int subtract( int x, int y)
{
if (y == 0)
return x;
return subtract(x ^ y, (~x & y) << 1);
}
int main()
{
int x = 29, y = 13;
cout << "x - y is " << subtract(x, y);
return 0;
}
|
C
#include<stdio.h>
int subtract( int x, int y)
{
if (y == 0)
return x;
return subtract(x ^ y, (~x & y) << 1);
}
int main()
{
int x = 29, y = 13;
printf ( "x - y is %d" , subtract(x, y));
return 0;
}
|
Java
class GFG {
static int subtract( int x, int y)
{
if (y == 0 )
return x;
return subtract(x ^ y, (~x & y) << 1 );
}
public static void main(String[] args)
{
int x = 29 , y = 13 ;
System.out.printf( "x - y is %d" ,
subtract(x, y));
}
}
|
Python3
def subtract(x, y):
if (y = = 0 ):
return x
return subtract(x ^ y, (~x & y) << 1 )
x = 29
y = 13
print ( "x - y is" , subtract(x, y))
|
C#
using System;
class GFG {
static int subtract( int x, int y)
{
if (y == 0)
return x;
return subtract(x ^ y, (~x & y) << 1);
}
public static void Main()
{
int x = 29, y = 13;
Console.WriteLine( "x - y is " +
subtract(x, y));
}
}
|
PHP
<?php
function subtract( $x , $y )
{
if ( $y == 0)
return $x ;
return subtract( $x ^ $y ,
(~ $x & $y ) << 1);
}
$x = 29; $y = 13;
echo "x - y is " , subtract( $x , $y );
# This code is contributed by ajit
?>
|
Javascript
<script>
function subtract(x , y)
{
if (y == 0)
return x;
return subtract(x ^ y, (~x & y) << 1);
}
var x = 29, y = 13;
document.write( "x - y is " +
subtract(x, y));
</script>
|
Output :
x - y is 16
Time Complexity: O(log y)
Auxiliary Space: O(log y)
This article is contributed Dheeraj. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!