-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivideTwoIntegers.java
More file actions
60 lines (57 loc) · 1.45 KB
/
DivideTwoIntegers.java
File metadata and controls
60 lines (57 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class DivideTwoIntegers {
public int divide(int dividend, int divisor) {
int result;
if (divisor == 0) {
return 0;
}
result = (int)helper((long)Math.abs((long)dividend), (long)Math.abs((long)divisor));
if(((dividend ^ divisor) >>> 31) == 1) {
result = 0 - result;
}
return result;
}
public long helper(long dividend, long divisor) {
//System.out.println("dividend = " + dividend + "; divisor = " + divisor);
if (dividend < divisor) {
return 0;
}
long x = 1;
long sum = 0;
long temp = divisor;
// int out = 0;
// int in = 0;
while (dividend >= divisor) {
//out++;
while (true) {
//in++;
temp = temp << 1;
if (temp > dividend) {
temp = temp >> 1;
break;
}
else if (temp == dividend) {
x = x + x;
return sum + x;
}
else {
x = x + x;
}
}
dividend = dividend - temp;
temp = divisor;
sum += x;
x = 1;
}
//System.out.println("out = " + out + "; in = " + in);
return sum;
}
public void test() {
int a = -1010369383;
int b = -2147483648;
System.out.println(Math.abs((long)b));
System.out.println((long)b);
System.out.println((long)Math.abs(b));
System.out.println((long)Math.abs((long)b));
System.out.println(divide(a,b));
}
}