Implementing division without addition, multiplication, division or subtraction - follow-up This is a follow-up to this question:

Division without using addition, multiplication or division

Could you please tell me if there is better way of performing division without +,-, * or /?

I've tested this code for different values of a and b.  I've also tested working for:


divide(8,3)
divide(5,10)
divide(9,6)
divide(42,6)


Could you please tell me if there is a cleaner solution?

private static int divide(int a , int b){

    boolean isNeg=false;
    boolean sign=true;
    boolean bothNegative=false;
    int quotient=1;


    if(a&lt;0 &amp;&amp; b&lt;0){
        bothNegative=true;
    }
    else if(a&lt;0 || b&lt;0) {

        isNeg=true;
        sign=true;

    }

    a=Math.abs(a);
    b=Math.abs(b);


    if(a&lt;b) return 0;
    else{

        int s=add(a,-b);
        int divisor =b;

        while(Math.abs(s)&gt;=Math.abs(divisor)){
            b=add(b,divisor);
            s=add(a,-b);
            quotient=add(quotient,1);
             if(isNeg){
                sign=!sign;
            }
        }

        if(bothNegative){
            return quotient;

        }else if(isNeg &amp;&amp; !sign){
            quotient=add(0,-quotient);
         }

        return quotient;
    }

}


private static int add(int a , int b){

    do{

        a^=b;
        b&amp;=(a^b);
        b&lt;&lt;=1;

     }while(Math.abs(b)!=0);

    return a;
}
Seems like the approach you've taken is to implement the + operator yourself. And when using the + operator on negative numbers, you've implemented your own - operator.

I have to congratulate you for being able to implement these operators from scratch. Good job.



A slight bug is that your code returns incorrect result for divide(-42, 6)

I believe the source of this bug is that this code is inside the while:

if (isNeg) {
    sign=!sign;
}


Put it outside the while and it seems to work (at least for my test case)



if (a &lt; b) return 0;
else{


You can save yourself some trouble indentation by removing that else. As you return, you can keep on going without adding indentation. (Which is why I love early returns)



Your spacing is a bit off, the easiest way to fix this is to press Ctrl + Shift + F if you're using Eclipse, or Alt + Shift + F if you're using Netbeans.

For example, this line:

while(Math.abs(s)&gt;=Math.abs(divisor)){


Would look better like this:

while (Math.abs(s) &gt;= Math.abs(divisor)) {




quotient = add(0, -quotient);


As you're using a - sign there already, why not replace all this with -quotient?



You have three boolean values related to the positive/negative issue:

boolean isNeg = false;
boolean sign = true;
boolean bothNegative = false;


Only one would be enough:

boolean swapSign = a &lt; 0 ^ b &lt; 0;


This is using the boolean XOR operator (^), so this will be true if a is negative or b is negative, but not if both are negative.



int quotient = 1;
if (a &lt; b) {
    return 0;
}


You're using a &lt; b as a special case, but that's not necessary.

int s;


That's not a readable name.

You can get rid of several unnecessary variables by directly operating on the a and b values. As they're ints, they're value-typed and therefore copied and not sent by reference, which means that you can modify them however you'd like inside your method. They can be used just like normal local variables.

The main part of your method can be simply this:

a = Math.abs(a);
b = Math.abs(b);

int result = 0;
while (a &gt;= b) {
    a = add(a, -b);
    result = add(result, 1);
}




Your logic for what to return is a bit bloated:

if (bothNegative) {
    return quotient;
}
else if (isNeg &amp;&amp; !sign) {
    quotient = add(0, -quotient);
}
return quotient;


This can be replaced with, by using the single swapSign variable:

if (swapSign) {
    return -result;
}
else {
    return result;
}


Or even simpler, using the ternary operator:

return swapSign ? -result : result;




End result:

private static int divide(int a, int b) {
    boolean swapSign = a &lt; 0 ^ b &lt; 0;
    a = Math.abs(a);
    b = Math.abs(b);
    int result = 0;
    while (a &gt;= b) {
        a = add(a, -b);
        result = add(result, 1);
    }
    return swapSign ? -result : result;
}