编写一个函数Add()返回两个整数的和。该函数不应使用任何算术运算运算符(+,++,–,-,..等)。
通过对两个位执行XOR(^),可以获得两个位的总和。进位可以通过对两个位进行“与”(&)获得。
上面是简单的Half Adder逻辑,可用于将2个单个位相加。我们可以将此逻辑扩展为整数。如果x和y在同一位置没有设置位,则x和y的按位XOR(^)得出x和y的总和。为了并入公共置位位,还使用了按位与(&)。 x和y的按位与运算得出所有进位位。我们计算(x&y)<< 1并将其添加到x ^ y中以获得所需的结果。
C++
// C++ Program to add two numbers
// without using arithmetic operator
#include
using namespace std;
int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common
//set bits of x and y
int carry = x & y;
// Sum of bits of x and y where at
//least one of the bits is not set
x = x ^ y;
// Carry is shifted by one so that adding
// it to x gives the required sum
y = carry << 1;
}
return x;
}
// Driver code
int main()
{
cout << Add(15, 32);
return 0;
}
// This code is contributed by rathbhupendra
C
// C Program to add two numbers
// without using arithmetic operator
#include
int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common
//set bits of x and y
int carry = x & y;
// Sum of bits of x and y where at
//least one of the bits is not set
x = x ^ y;
// Carry is shifted by one so that adding
// it to x gives the required sum
y = carry << 1;
}
return x;
}
int main()
{
printf("%d", Add(15, 32));
return 0;
}
Java
// Java Program to add two numbers
// without using arithmetic operator
import java.io.*;
class GFG
{
static int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common
// set bits of x and y
int carry = x & y;
// Sum of bits of x and
// y where at least one
// of the bits is not set
x = x ^ y;
// Carry is shifted by
// one so that adding it
// to x gives the required sum
y = carry << 1;
}
return x;
}
// Driver code
public static void main(String arg[])
{
System.out.println(Add(15, 32));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 Program to add two numbers
# without using arithmetic operator
def Add(x, y):
# Iterate till there is no carry
while (y != 0):
# carry now contains common
# set bits of x and y
carry = x & y
# Sum of bits of x and y where at
# least one of the bits is not set
x = x ^ y
# Carry is shifted by one so that
# adding it to x gives the required sum
y = carry << 1
return x
print(Add(15, 32))
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# Program to add two numbers
// without using arithmetic operator
using System;
class GFG
{
static int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common
// set bits of x and y
int carry = x & y;
// Sum of bits of x and
// y where at least one
// of the bits is not set
x = x ^ y;
// Carry is shifted by
// one so that adding it
// to x gives the required sum
y = carry << 1;
}
return x;
}
// Driver code
public static void Main()
{
Console.WriteLine(Add(15, 32));
}
}
// This code is contributed by vt_m.
PHP
Javascript
C
int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1);
}
Java
static int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add(x ^ y, (x & y) << 1);
}
// This code is contributed by subham348
Python3
def Add(x, y):
if (y == 0):
return x
else
return Add( x ^ y, (x & y) << 1)
# This code is contributed by subhammahato348
C#
static int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add(x ^ y, (x & y) << 1);
}
// This code is contributed by subhammahato348
Javascript
function Add(x, y)
{
if (y == 0)
return x;
else
return Add(x ^ y, (x & y) << 1);
}
// This code is contributed by Ankita saini
输出 :
47
以下是相同方法的递归实现。
C
int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1);
}
Java
static int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add(x ^ y, (x & y) << 1);
}
// This code is contributed by subham348
Python3
def Add(x, y):
if (y == 0):
return x
else
return Add( x ^ y, (x & y) << 1)
# This code is contributed by subhammahato348
C#
static int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add(x ^ y, (x & y) << 1);
}
// This code is contributed by subhammahato348
Java脚本
function Add(x, y)
{
if (y == 0)
return x;
else
return Add(x ^ y, (x & y) << 1);
}
// This code is contributed by Ankita saini