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.  Could you please tell me if there is a cleaner solution?

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

        if(a&lt;b) return a;
        boolean isNeg=false,sign=true,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);
            int s=add(a,-b);

            while(Math.abs(s)&gt;=Math.abs(b)){
                b=add(b,b);
                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=a^b;
                b=(a^b)&amp;b;
                b=b&lt;&lt;1;
            }while(Math.abs(b)!=0);

            return a;
        }
First of all, please keep your indentation consistent; it's all over the place here.  Java code is supposed to be indented by four spaces.  Also, try not to put whitespace in random places.  Keep things consistent.

Also, here are some additional cleanliness tips:


Don't declare or initialize multiple variables on a single line:


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



Have each variable on separate lines, which helps with maintenance and readability:

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


If you ever need additional boolean variables, having more lines would be better than having a longer single line across the screen.
Your bitwise operations can be simplified a bit:


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



If you're doing a bitwise (or a mathematical) operation where the same variable is on both sides of the = sign, you can simply them as such:

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