I saw some articles telling about how to use bit-wise operation to achieve add operation.
Here the explanation for the function.
int add(int x, int y){
int a,b;
do{
a = x & y;// a holds the carry (a承接进位)
b = x ^ y;// b holds the sum without carry (b承接无进位的和)
x = a<<1; // perform carry, x becomes carry (进位,并用x承接进位)
y = b;
}while(x) // until carry = 0 (直到无进位)
return y;
}
No comments:
Post a Comment