Subtract two numbers without using arithmetic operators

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to Subtract two numbers
// without using arithmetic operators
#include<stdio.h>
  
int subtract(int x, int y)
{
    // Iterate till there
    // is no carry
    while (y != 0)
    {
        // borrow contains common 
        // set bits of y and unset
        // bits of x
        int borrow = (~x) & y;
  
        // Subtraction of bits of x 
        // and y where at least one
        // of the bits is not set
        x = x ^ y;
  
        // Borrow is shifted by one 
        // so that subtracting it from
        // x gives the required sum
        y = borrow << 1;
    }
    return x;
}
  
// Driver Code
int main()
{
    int x = 29, y = 13;
    printf("x - y is %d", subtract(x, y));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to subtract two Number
// without using arithetic operater
import java.io.*;
  
class GFG 
{
    static int subtract(int x, int y)
    {
          
    // Iterate till there
    // is no carry
    while (y != 0)
    {
        // borrow contains common 
        // set bits of y and unset
        // bits of x
        int borrow = (~x) & y;
  
        // Subtraction of bits of x 
        // and y where at least one
        // of the bits is not set
        x = x ^ y;
  
        // Borrow is shifted by one 
        // so that subtracting it from 
        // x gives the required sum
        y = borrow << 1;
    }
      
    return x;
}
      
    // Driver Code
    public static void main (String[] args) 
    {
        int x = 29, y = 13;
          
        System.out.println("x - y is "
                        subtract(x, y));
    }
}
  
// This code is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def subtract(x, y):
  
    # Iterate till there
    # is no carry
    while (y != 0):
      
        # borrow contains common 
        # set bits of y and unset
        # bits of x
        borrow = (~x) & y
          
        # Subtraction of bits of x
        # and y where at least one
        # of the bits is not set
        x = x ^ y
  
        # Borrow is shifted by one 
        # so that subtracting it from 
        # x gives the required sum
        y = borrow << 1
      
    return x
  
  
# Driver Code
x = 29
y = 13
print("x - y is",subtract(x, y))
  
# This code is contributed by
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to subtract two Number
// without using arithetic operater
using System;
  
class GFG {
      
    static int subtract(int x, int y)
    {
          
        // Iterate till there
        // is no carry
        while (y != 0)
        {
              
            // borrow contains common 
            // set bits of y and unset
            // bits of x
            int borrow = (~x) & y;
      
            // Subtraction of bits of x 
            // and y where at least one
            // of the bits is not set
            x = x ^ y;
      
            // Borrow is shifted by one 
            // so that subtracting it from 
            // x gives the required sum
            y = borrow << 1;
        }
          
        return x;
    }
      
    // Driver Code
    public static void Main () 
    {
        int x = 29, y = 13;
          
        Console.WriteLine("x - y is "
                        subtract(x, y));
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to subtract two Number
// without using arithetic operater
  
function subtract($x, $y)
{
      
    // Iterate till there is no carry
    while ($y != 0)
    {
          
        // borrow contains common set 
        // bits of y and unset
        // bits of x
        $borrow = (~$x) & $y;
  
        // Subtraction of bits of x 
        // and y where at least
        // one of the bits is not set
          
        $x = $x ^ $y;
  
        // Borrow is shifted by one so
        // that subtracting it from
        // x gives the required sum
          
        $y = $borrow << 1;
    }
    return $x;
}
  
        // Driver Code
        $x = 29; $y = 13;
        echo "x - y is ", subtract($x,$y);
  
// This code is contributed by Ajit
?>

chevron_right



Output :

x - y is 16

Following is recursive implementation for the same approach.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
int subtract(int x, int y)
{
    if (y == 0)
        return x;
    return subtract(x ^ y, (~x & y) << 1);
}
  
// Driver program
int main()
{
    int x = 29, y = 13;
    printf("x - y is %d", subtract(x, y));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java  Program to subtract two Number
// without using arithetic operater
// Recursive implementation.
class GFG {
  
    static int subtract(int x, int y)
    {
  
        if (y == 0)
            return x;
  
        return subtract(x ^ y, (~x & y) << 1);
    }
  
    // Driver program
    public static void main(String[] args)
    {
        int x = 29, y = 13;
        System.out.printf("x - y is %d"
                            subtract(x, y));
    }
}
  
// This code is contributed by  
// Smitha Dinesh Semwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python  Program to
# subtract two Number
# without using arithmetic operator
# Recursive implementation.
  
def subtract(x, y):
  
    if (y == 0):
        return x
    return subtract(x ^ y, (~x & y) << 1)
  
# Driver program
x = 29
y = 13
print("x - y is", subtract(x, y))
  
# This code is contributed by
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to subtract two Number
// without using arithetic operater
// Recursive implementation.
using System;
  
class GFG {
  
    static int subtract(int x, int y)
    {
        if (y == 0)
            return x;
  
        return subtract(x ^ y, (~x & y) << 1);
    }
  
    // Driver program
    public static void Main()
    {
        int x = 29, y = 13;
        Console.WriteLine("x - y is "
                            subtract(x, y));
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
function subtract($x, $y)
{
    if ($y == 0)
        return $x;
    return subtract($x ^ $y
                   (~$x & $y) << 1);
}
  
// Driver Code
$x = 29; $y = 13;
echo "x - y is ", subtract($x, $y);
  
# This code is contributed by ajit 
?>

chevron_right



Output :

x - y is 16

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



My Personal Notes arrow_drop_up

Improved By : jit_t, vt_m



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.