Skip to content

Commit 6a04e64

Browse files
committed
modify code
1 parent c762ab7 commit 6a04e64

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/class05/Code03_BitAddMinusMultiDiv.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static int div(int a, int b) {
4141
int x = isNeg(a) ? negNum(a) : a;
4242
int y = isNeg(b) ? negNum(b) : b;
4343
int res = 0;
44-
for (int i = 31; i > negNum(1); i = minus(i, 1)) {
44+
for (int i = 31; i >= 0; i = minus(i, 1)) {
4545
if ((x >> i) >= y) {
4646
res |= (1 << i);
4747
x = minus(x, y << i);
@@ -51,17 +51,20 @@ public static int div(int a, int b) {
5151
}
5252

5353
public static int divide(int dividend, int divisor) {
54-
if (divisor == Integer.MIN_VALUE) {
55-
return dividend == Integer.MIN_VALUE ? 1 : 0;
56-
}
57-
if (dividend == Integer.MIN_VALUE) {
54+
if (dividend == Integer.MIN_VALUE && divisor == Integer.MIN_VALUE) {
55+
return 1;
56+
} else if (divisor == Integer.MIN_VALUE) {
57+
return 0;
58+
} else if (dividend == Integer.MIN_VALUE) {
5859
if (divisor == negNum(1)) {
5960
return Integer.MAX_VALUE;
61+
} else {
62+
int ans = div(add(dividend, 1), divisor);
63+
return add(ans, div(minus(dividend, multi(ans, divisor)), divisor));
6064
}
61-
int res = div(add(dividend, 1), divisor);
62-
return add(res, div(minus(dividend, multi(res, divisor)), divisor));
65+
} else {
66+
return div(dividend, divisor);
6367
}
64-
return div(dividend, divisor);
6568
}
6669

6770
}

0 commit comments

Comments
 (0)