0
\$\begingroup\$

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);
            int s=add(a,-b);

            while(Math.abs(s)>=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 && !sign){
                quotient=add(0,-quotient);
             }

            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;
        }
\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 29, 2014 at 18:57
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 29, 2014 at 18:59
  • \$\begingroup\$ 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; \$\endgroup\$ Commented Jun 29, 2014 at 19:31
  • \$\begingroup\$ @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). \$\endgroup\$
    – sreeprasad
    Commented Jun 29, 2014 at 20:08
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 29, 2014 at 20:37

1 Answer 1

4
\$\begingroup\$

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;
    
\$\endgroup\$
3
  • \$\begingroup\$ "supposed to be indented by four spaces" - or one tab! \$\endgroup\$ Commented Jun 29, 2014 at 18:54
  • \$\begingroup\$ Or two spaces, or one, or three. Just be consistent! I myself prefer 3.14159 spaces. \$\endgroup\$ Commented Jun 29, 2014 at 19:02
  • \$\begingroup\$ @DavidHarkness: True, but I believe the standard mentions four spaces. Perhaps I should've provided a link to that as well. \$\endgroup\$
    – Jamal
    Commented Jun 29, 2014 at 19:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.