2019-04-13 17:13发布 生成海报
a=n&(n-1);//去掉n中最右边的1; b=n&(-n);//得到n中最右边的1 -n==~n+1; -n==~(n-1);
/*在正式应用中一定要注意边界问题,包括上边界和下边界*/ #include #include using namespace std; int add(int a, int b) { if (a==0) { return b; } int carry = (a&b)<<1; int adder = a^b; return add(carry, adder); } int substract(int a, int b) { return add(a, -b); } int mulit(int a, int b) { bool _ = (a > 0)^(b > 0); a = abs(a); b = abs(b); int ans=0; map bitmap; for (size_t i = 0; i < 32; i++) { bitmap[1 << i] = i; } while (b>0) { int carry = bitmap[b&(-b)]; b = b ^ (b&(-b)); ans += a << carry; } return _ ? -ans : ans; } int divide(int a, int b) { bool _ = (a > 0)^(b > 0); a = abs(a); b = abs(b); int ans = 0; while (a >= b) { int m = 1; int nb = b; while ((nb<<1)结果: