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 ){
        }else if(a<0||b<0) {



            int s=add(a,-b);


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

            return quotient;

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

            return a;
  • \$\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


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:


    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;
  • \$\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.