# Division without using addition, multiplication or division [closed]

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<b) return a;
boolean isNeg=false,sign=true,bothNegative=false;
int quotient=1;

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

isNeg=true;
sign=true;

}

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

while(Math.abs(s)>=Math.abs(b)){
if(isNeg){
sign=!sign;
}
}

if(bothNegative){
return quotient;
}else if(isNeg && !sign){
}

return quotient;

}
private static int add(int a , int b){
do{
a=a^b;
b=(a^b)&b;
b=b<<1;
}while(Math.abs(b)!=0);

return a;
}

• When you tested this code for different values, what values did you test? Were the results correct? Even the most simplest tests I'm doing returns incorrect results. Code Review is for reviewing working code (you get the expected results), your code does not seem to be working. Commented Jun 29, 2014 at 18:57
• Some tests I did: divide(5, 10) = 5, divide(9, 6) = 1, divide(7, 2) = 2, divide(42, 6) = 3. Only one of these seemed to be correct. Commented Jun 29, 2014 at 18:59
• I have to add though: If you're allowed to use -, the easiest way would be to do while (a > b) { a -= b; i++; } return i; Commented Jun 29, 2014 at 19:31
• @SimonAndréForsberg : Thank you ! I missed those test cases. I have corrected my code to pass your tests. My tests were for 1.divide(8,3) 2.divide(6,5) 3.divide(-6,-3). Commented Jun 29, 2014 at 20:08
• As you already have accepted an answer here and that we don't like invalidating answers, I'd recommend you post your fixed and improved code in a new question. Commented Jun 29, 2014 at 20:37

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)&b;
b=b<<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 &= (a^b);
b <<= 1;

• "supposed to be indented by four spaces" - or one tab! Commented Jun 29, 2014 at 18:54
• Or two spaces, or one, or three. Just be consistent! I myself prefer 3.14159 spaces. Commented Jun 29, 2014 at 19:02
• @DavidHarkness: True, but I believe the standard mentions four spaces. Perhaps I should've provided a link to that as well. Commented Jun 29, 2014 at 19:13