public static int divide(int dividend, int divisor) {//AC by consulting
        int ret = 0;
        long x = Math.abs((long)dividend);
        long y = Math.abs((long)divisor);

        while (x >= y) {
            int counter = 0;
            while (x >= (y << counter)) {
                counter++;
            }
            x -= y << (counter-1);
            ret += 1 << (counter-1);
        }

        if (dividend > 0 && divisor > 0) return ret;
        else if (dividend < 0 && divisor < 0) return ret;
        else return -ret;
    }