Vik*_*ana 22
在c程序中使用此功能
int Add(int a, int b)
{
while (b)
{
// carry now contains common set bits of "a" and "b"
int carry = a & b;
// Sum of bits of "a" and "b" where at least one of the bits is not set
a = a ^ b;
// Carry is shifted by one so that adding it to "a" gives the required sum
b = carry << 1;
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
zav*_*avg 11
使用按位^和&运算符和递归
int add(int x, int y){
return y == 0 ? x : add( x ^ y, (x & y) << 1);
}
Run Code Online (Sandbox Code Playgroud)
PS:它是vikas提出的算法的递归版本.
使用Anti Log()你可以做到这一点
Temp= Anti Log(a)* Anti Log(b);
a+b value is equals to log(Temp);
Run Code Online (Sandbox Code Playgroud)
适用于不是双精度的整数.
在Java中使用递归 -
public static int add(int a, int b){
if(b == 0) return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return add(sum, carry);
}
Run Code Online (Sandbox Code Playgroud)
在C-
int add(int a, int b){
if(b == 0) return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return add(sum, carry);
}
Run Code Online (Sandbox Code Playgroud)