标签:gnu add minus class 运算 int nbsp span 实现
int Add(int a, int b) { int sum = 0; while(b!=0) { sum = a^b; b = ((a&b)<<1); a = sum; } return sum; }
int NegNum(int b) { return Add(~b, 1); }
int Minus(int a, int b) { return Add(a, NegNum(b)); }
bool isNeg(int a) { return a<0; }
int Multi(int a, int b) { int x = isNeg(a) ? NegNum(a) : a; int y = isNeg(b) ? NegNum(b) : b; int res = 0; while(y!=0) { if((y&1)!=0) res = Add(res, x); x <<= 1; y >>= 1; } return isNeg(a)==isNeg(b) ? res : NegNum(res); }
int Div(int a, int b) { int x = isNeg(a) ? NegNum(a) : a; int y = isNeg(b) ? NegNum(b) : b; int res = 0; for(int i=31; i>=0; i=Minus(i,1)) { if((x>>i) >= y) { res |= (1<<i); x = Minus(x, y<<i); } } return isNeg(a)==isNeg(b) ? res : NegNum(res); }
标签:gnu add minus class 运算 int nbsp span 实现
原文地址:https://www.cnblogs.com/randyniu/p/9497519.html