Division without / operator I created a method that does division without the / operator. I believe this code is a bit slow and can be improved, but I am not sure how to do so. I'm not so good with shift operators (&gt;&gt; and &gt;&gt;&gt;), but I believe that this will help a bit. How can I change my code to increase the speed of the division. I am open the changing my logic as well.

public class DivisionReplace {
    public static void divide(int N, int D) {
        int result = 0;

        if (D == 0) {
            System.out.println("Cannot divide by 0");
        }
        else if (N == 0) {
            System.out.println(0);
        }
        else if (N == D) {
            System.out.println(1);
        }
        else if (N &gt; 0 &amp;&amp; D &gt; 0 &amp;&amp; N &lt; D) {
            System.out.println(0);
        }
        else {
            // both negative
            if (N &lt; 0 &amp;&amp; D &lt; 0) {
                while (N &lt;= D) {
                    N += -1 * D;
                    result++;
                }
                System.out.println(result);
            }
            // either N or D negative
            else if (N &lt; 0 || D &lt; 0) {
                if (N &lt; 0) {
                    N = -1 * N;
                }
                else {
                    D = -1 * D;
                }
                while (N &gt;= D) {
                    N -= D;
                    result--;
                }
                System.out.println(result);
            }
            // both positive
            else {
                while (N &gt;= D) {
                    N -= D;
                    result++;
                }
                System.out.println(result);
            }
        }
    }

    public static void main(String[] args) {
        divide(-4,-10);
    }
}
A couple of comments first

It would be better to let your divide method should return an int, not do the output to System.out itself. Do the output in main like this:

public static void main(String[] args) {
    System.out.println(divide(-4,-10));
}


The variables N and D are short and don't follow the Java naming conventions. Parameter names should start with a lowercase letter and use camelCase. I would recommend calling them numerator and denominator.

If denominator == 0 is a quite exceptional case, I would recommend throwing an ArithmeticException stating that it was caused by division by zero.

You treat more special cases than you need to. I don't think you need to handle either numerator == denominator or numerator == 0 as a special case. It should be possible to handle that using the normal logic for division.

If you were doing divisions by a power of 2 (2, 4, 8, 16, etc.) you could use right shift numerator &gt;&gt; 1, numerator &gt;&gt; 2 etc. However, your code should support more than just those cases which makes things more difficult.

In the end, you are trying to re-implement division by using repetitive subtraction. It will be slow, no matter what you do. If you want to be possibly somewhat faster for bigger numbers, or want to try another approach (I can't promise you it will be faster, but I believe that for big enough numbers it will be significantly faster), you could try to implement division by using one of the good old "pen and paper" ways. Think about the division 303364 / 596. Try to solve that by hand! and think about what it is that you are doing. (Hint: Also think about how you do multiplications by hand, how would you solve 583 * 42 ?)

However, in reality though, the fastest solution is to use the / operator in Java, but that wouldn't be a challenge now, would it? So I guess that's out of the question :)