不使用算术运算符减去两个数

编写一个函数减法(x,y),返回x-y,其中x和y是整数。该函数不应使用任何算术运算符(+、++、–、…等)。 这个想法是使用位运算符。 使用位运算符讨论了两个数的加法 .就像加法一样,这个想法是使用 减法器 思维方式

null

下面给出了半减法器的真值表。

X     Y     Diff     Borrow0     0     0     00     1     1     11     0     1     01     1     0     0

从上表中,我们可以绘制“差异”和“借用”的卡诺图。 所以,逻辑方程是:

    Diff   = y ⊕ x    Borrow = x' . y 

资料来源: Wikipedia减法器页面

以下是基于上述等式的实现。

C++

// C++ program to Subtract two numbers
// without using arithmetic operators
#include <iostream>
using namespace std;
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;
cout << "x - y is " << subtract(x, y);
return 0;
}
// This code is contributed by shivanisinghss2110


C

// 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;
}


JAVA

// Java Program to subtract two Number
// without using arithmetic operator
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 arithmetic operator
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

<?php
// PHP Program to subtract two Number
// without using arithmetic operator
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
?>


Javascript

<script>
// JavaScript program to Subtract two numbers
// without using arithmetic operators
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
let 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
let x = 29, y = 13;
document.write( "x - y is " + subtract(x, y));
// This code is contributed by Surbhi Tyagi.
</script>


输出:

x - y is 16

时间复杂度:O(logy)

辅助空间:O(1)

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

C++

#include <iostream>
using namespace std;
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;
cout << "x - y is " << subtract(x, y);
return 0;
}
// this code is contributed by shivanisinghss2110


C

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


JAVA

// Java  Program to subtract two Number
// without using arithmetic operator
// 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 arithmetic operator
// 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

<?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
?>


Javascript

<script>
// javascript  Program to subtract two Number
// without using arithmetic operator
// Recursive implementation.
function subtract(x , y)
{
if (y == 0)
return x;
return subtract(x ^ y, (~x & y) << 1);
}
// Driver program
var x = 29, y = 13;
document.write( "x - y is " +
subtract(x, y));
// This code is contributed by Princi Singh
</script>


输出:

x - y is 16

时间复杂度:O(logy)

辅助空间:O(对数y)

这篇文章是有贡献的 Dheeraj 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享