📌  相关文章
📜  减去两个数字而不使用算术运算运算符

📅  最后修改于: 2021-04-26 08:53:04             🧑  作者: Mango

编写一个返回xy的函数减去(x,y),其中x和y是整数。该函数不应使用任何算术运算运算符(+,++,–,-,..等)。
这个想法是使用按位运算运算符。已经使用位运算符讨论了两个数的加法运算。像加法一样,其思想是使用减法器逻辑。
下面给出了半减法器的真值表。

X     Y     Diff     Borrow
0     0     0     0
0     1     1     1
1     0     1     0
1     1     0     0

从上表可以得出“差”和“借”的卡诺图。
因此,逻辑方程为:

Diff   = y ⊕ x
    Borrow = x' . y 

资料来源:维基百科减法器页面
以下是基于以上等式的实现。

C
// C program to Subtract two numbers
// without using arithmetic operators
#include
 
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;
}


Java
// 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


Python3
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


C#
// 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.


PHP


Javascript


C
#include
 
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;
}


Java
// 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.


Python3
# 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


C#
// 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.


PHP


Javascript


输出 :

x - y is 16

以下是相同方法的递归实现。

C

#include
 
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;
}

Java

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

Python3

# 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

C#

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

的PHP


Java脚本


输出 :

x - y is 16