# Division without using division, multiplication, or modulus operators

Implement division of two positive integers without using the division, multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder.

My solution

const division = (dividend, divisor) => {
let remainder = null;
let quotient = 1;
const sign = ((dividend > 0 && divisor < 0) ||
(dividend < 0 && divisor > 0)) ?
~1 : 1;

let tempdividend = Math.abs(dividend);
let tempdivisor = Math.abs(divisor);

if (tempdivisor === tempdividend) {
remainder = 0;
return sign;
} else if (tempdividend < tempdivisor) {
remainder = dividend < 0 ?
sign < 0 ? ~tempdividend : tempdividend :
tempdividend;
return 0;
}
while (tempdivisor << 1 <= tempdividend) {
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
}

quotient = dividend < 0 ?
(sign < 0 ? ~quotient : quotient) + division(~(tempdividend-tempdivisor), divisor) :
(sign < 0 ? ~quotient : quotient) + division(tempdividend-tempdivisor, divisor);
return quotient;
}

• Your function does not work, very sloppy. I am amazed you found two arguments that would return the correct result. division(-1,1) returns -2, -2 / -1 returns 3 and worst any value divide 0 does not return at all. Apr 18, 2019 at 23:50
• It works for positive integers (as the task ask for). But you are right about the other stuff. Will fix it. @Blindman67 Apr 19, 2019 at 8:56

Is bitwise operation mandatory? There's a much simpler way

const divide = (dividend, divisor) => {
let quotient = 0, neg = false;

if( (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ){ neg = true; }

dividend = Math.abs(dividend);
divisor = Math.abs(divisor);

if(dividend < divisor) {return 0;}
else if(dividend > 0 && divisor != 0){
while(dividend >= divisor){
dividend -= divisor;
++quotient;
}
} else { // handle what you want to do for those cases..}

return neg ? -quotient : quotient;
}


You get your quotient and remainder is ignored. Just do a check if dividend is negative or divisor is 0.

• Welcome to Code Review! Better check dividend sign before comparing to divisor. Apr 18, 2019 at 20:01
• Also first if statement should be < not > and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if its divide(Number.MAX_SAFE_INTEGER, -1)` (about 22 years on average device) There is a point where complexity provides practical solutions. Apr 18, 2019 at 23:49
• @greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion. Apr 19, 2019 at 4:15
• @Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then. Apr 19, 2019 at 4:23
• @Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think. Apr 19, 2019 at 15:50