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\$

closed as off-topic by Simon Forsberg, David Harkness, palacsint, Mathieu Guindon, Marc-Andre Jun 29 '14 at 19:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Simon Forsberg, David Harkness, palacsint, Mathieu Guindon, Marc-Andre
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\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\$ – Simon Forsberg Jun 29 '14 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\$ – Simon Forsberg Jun 29 '14 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\$ – Simon Forsberg Jun 29 '14 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 Jun 29 '14 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\$ – Simon Forsberg Jun 29 '14 at 20:37
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\$
  • \$\begingroup\$ "supposed to be indented by four spaces" - or one tab! \$\endgroup\$ – Simon Forsberg Jun 29 '14 at 18:54
  • \$\begingroup\$ Or two spaces, or one, or three. Just be consistent! I myself prefer 3.14159 spaces. \$\endgroup\$ – David Harkness Jun 29 '14 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 Jun 29 '14 at 19:13

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