不考虑数据溢出
public class AddMinusMultiDivideByBit {
public static int add(int a, int b) {//加
int sum = a;
while (b != 0) {
sum = a ^ b;//无进位的加法
b = (a & b) << 1;//通过并集实现进位
a = sum;
}
return sum;
}
public static int negNum(int n) {
return add(~n, 1);//取反
}
public static int minus(int a, int b) {//减
return add(a, negNum(b));
}
public static int multi(int a, int b) {//乘
int res = 0;
while (b != 0) {
if ((b & 1) != 0) {
res = add(res, a);
}
a <<= 1;
b >>>= 1;
}
return res;
}
public static boolean isNeg(int n) {
return n < 0;
}
public static 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 > -1; i = minus(i, 1)) {
if ((x >> i) >= y) {
res |= (1 << i);
x = minus(x, y << i);
}
}
return isNeg(a) ^ isNeg(b) ? negNum(res) : res;
}
public static int divide(int a, int b) {//除
if (b == 0) {
throw new RuntimeException("divisor is 0");
}
if (a == Integer.MIN_VALUE && b == Integer.MIN_VALUE) {
return 1;
} else if (b == Integer.MIN_VALUE) {
return 0;
} else if (a == Integer.MIN_VALUE) {
int res = div(add(a, 1), b);
return add(res, div(minus(a, multi(res, b)), b));
} else {
return div(a, b);
}
}
public static void main(String[] args) {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
System.out.println("a = " + a + ", b = " + b);
System.out.println(add(a, b));
System.out.println("=========");
System.out.println(minus(a, b));
System.out.println("=========");
System.out.println(multi(a, b));
System.out.println("=========");
System.out.println(divide(a, b));
System.out.println("=========");
a = Integer.MIN_VALUE;
b = 32;
System.out.println(divide(a, b));
}
}