简体   繁体   English

使用按位运算符将两个整数相除

[英]Divide two integers using bitwise operators

I have to divide two numbers by just using the bitwise operators. 我必须仅使用按位运算符将两个数相除。 My code is as follows: 我的代码如下:

public class Solution {
    public int divide(int dividend, int divisor) {
        int c = 0, sign = 0;
        if (dividend < 0) {
            dividend = negate(dividend);
            sign^=1;
        }
        if (divisor < 0) {
            divisor = negate(divisor);
            sign^=1;
        }
        if ( divisor != 0){
           while (dividend >= divisor){
                dividend = sub(dividend, divisor);
                c++;
            } 
        }

        if (sign == 1){
            c = negate(c);
        }
        return c;
    }

    private int negate(int number){
        return add(~number,1);
    }
    private int sub(int x, int y){
        return add(x,negate(y));
    }
    private int add(int x, int y){
        while ( y != 0){
          int carry = x&y;
          x = x^y;
          y = carry << 1;
        }
        return x;
    }
}

I am getting a time out exception while running the code : 我在运行代码时遇到超时异常:

Last executed input:
2147483647
1

I added an extra check in the code like this : 我在这样的代码中添加了额外的检查:

if (divisor == 1){
    return dividend;
}

but then on running the code again, I am getting a Time out exception like this: 但是然后再次运行代码时,出现了如下超时异常:

Last executed input:
2147483647
2

Can you help me where I am going wrong with the code? 您能帮我解决代码错误吗?

Since you are using a naïve implementation of the arithmetical operations, I assume it just takes too long, since it has to do roughly 1 billion of subtractions, each of which is a loop of 16 iterations on average. 由于您使用的是朴素的算术实现,因此我假设它花费的时间太长,因为它必须进行约10亿次减法,每个减法平均循环16次迭代。 So it'd be about 5 seconds on a 3GHz CPU, if every inner step took 1 cycle. 因此,如果每个内部步骤花费1个周期,则在3GHz CPU上大约需要5秒钟。 But since it obviously takes more, especially considering it's in java, I'd say you can easily expect something like a 1 minute running time. 但是由于显然需要更多的时间,尤其是考虑到它是在Java中运行的,所以我想您可以轻松地期望运行1分钟左右的时间。 Perhaps java has a timeout check for the environment you are running it in. 也许Java对您正在其中运行的环境进行了超时检查。

I have to admit I haven't thoroughly verified your code and assumed it worked correctly algorithm-wise. 我必须承认,我还没有完全验证您的代码,并假设它在算法方面正常工作。

Think of how long division works, the pen-and-paper method for division we've all learnt at school. 想一想除法要持续多久 ,我们在学校都学过的纸笔除法方法。

Of course we've learnt it in base 10, but is there any reason why the same algorithm wouldn't work in base 2? 当然,我们已经在10基础中学习了它,但是有什么理由为什么相同的算法在2基础中不起作用?

No, in fact it's easier because when you're doing the division step for each digit, the result is simply 1 if the part of the dividend above the divisor is greater than or equal to the divisor, and 0 otherwise. 不,实际上,这很容易,因为当您对每个数字进行除法步骤时,如果除数之上的被除数部分大于或等于除数,则结果为1,否则为0。

And shifting the divisor and the result come out of the box too. 移位除数和结果也很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM