在此 java 代码中查找错误以进行右循环移位(Codility)

Finding the bug in this java code for right cyclic shift (Codility)

最近我遇到了一个 codility 问题,它说下面的代码有一个 bug
所以,代码问题是我们有一个来自 N[29]... N[0] 的 30 位无符号整数并且执行右循环移位 (>>) 应该给我们一个像 N[0]N[29]... N[1].

这样的数字

我们的目标是找到从给定数字产生最大值的右移次数。

For Example:
for N = 9736 (00 0000 0000 0000 0010 0110 0000 1000)
9736 >> 1 = 4868 -> 00 0000 0000 0000 0001 0011 0000 0100
.
.
.
9736 >> 11 = 809500676 -> 11 0000 0100 0000 0000 0000 0000 0100
.
.
till 30 (as we have 30 bits integers)

从上面的第 11 次迭代示例中,我们收到了 9736 的最大可能数。 因此答案 = 11

给定代码:

 int shift(int N) {
        int largest = 0;
        int shift = 0;
        int temp = N;

        for (int i = 1; i < 30; ++i) {
            int index = (temp & 1);
            temp = ((temp >> 1) | (index << 29));
          
            if (temp > largest) {
                largest = temp;
                shift = i;
            }
        }

        return shift;
    }

N is in range [0... 1,073,741,823]

我试过了,但找不到这里的错误或失败的测试用例。

0b10000...0000x20000000)失败,因为最大值是shift==0

最简单的解决方案是将 largest 定义为 N 而不是 0