mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
2215758b3b
- Adding Divide Two Integers (29.c) - Adding Plus One (66.c) - Adding Bitwise AND of Numbers Range (201.c) - Readme updated
36 lines
703 B
C
36 lines
703 B
C
int divide(int dividend, int divisor){
|
|
int sign = 1;
|
|
long int output = 0;
|
|
if (dividend < 0) {
|
|
sign *= -1;
|
|
|
|
} else {
|
|
dividend *= -1;
|
|
}
|
|
if (divisor < 0) {
|
|
sign *= -1;
|
|
|
|
} else {
|
|
divisor *= -1;
|
|
}
|
|
while (dividend <= divisor) {
|
|
long int tmp = 0;
|
|
long int div = divisor;
|
|
while (dividend <= div) {
|
|
tmp += (tmp+1);
|
|
dividend -= div;
|
|
div += div;
|
|
}
|
|
if (output >= INT_MAX) {
|
|
if (sign == -1) {
|
|
return INT_MIN;
|
|
} else {
|
|
return INT_MAX;
|
|
}
|
|
}
|
|
output += tmp;
|
|
}
|
|
|
|
return output * sign;
|
|
}
|