public int divide(int dividend, int divisor) {
        if (divisor == 0) throw new ArithmeticException("Divisor is zero.");
        if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; // one corner case

        boolean isNeg = dividend < 0 != divisor < 0;
        long a = Math.abs((long) dividend);
        long b = Math.abs((long) divisor);
        int cnt = 0;
        while (a >= b) {
            int i = 0;
            while (a >= (b << i)) {
                ++i;
            }
            a -= (b << (i - 1));
            cnt += (1 << (i - 1));
        }
        return isNeg ? -cnt : cnt;
    }