“>>>"是什么意思在java中是什么意思? [英] what does ">>>" mean in java?

查看:145
本文介绍了“>>>"是什么意思在java中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现此代码可以在 SO 在这里发帖.但我不明白这一行是什么意思 int mid = (low + high) >>>1;

I found this code to find duplicates in SO post here. but I dont understand what this line means int mid = (low + high) >>> 1;

private static int findDuplicate(int[] array) {
        int low = 0;
        int high = array.length - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            System.out.println(mid);
            int midVal = array[mid];

            if (midVal == mid)
                low = mid + 1;
            else
                high = mid - 1;
        }
        return high;
    }

推荐答案

>>> 操作符是 Java 中的无符号右移运算符.它有效地将操作数除以 2 到右操作数的幂,或者这里只是 2.

The >>> operator is the unsigned right bit-shift operator in Java. It effectively divides the operand by 2 to the power of the right operand, or just 2 here.

>>>>> 之间的区别只会在移动负数时出现.>>> 运算符将 1 位移入最高有效位,如果它是 1,并且 >>>;> 无论如何都会在 0 中移动.

The difference between >> and >>> would only show up when shifting negative numbers. The >> operator shifts a 1 bit into the most significant bit if it was a 1, and the >>> shifts in a 0 regardless.

更新:

让我们平均 12147483647 (Integer.MAX_VALUE).我们可以轻松地进行数学计算:

Let's average 1 and 2147483647 (Integer.MAX_VALUE). We can do the math easily:

(1 + 2147483647) / 2 = 2147483648 / 2 = 1073741824

现在,使用代码 (low + high)/2,这些是涉及的位:

Now, with the code (low + high) / 2, these are the bits involved:

          1: 00000000 00000000 00000000 00000001
+2147483647: 01111111 11111111 11111111 11111111
================================================
-2147483648: 10000000 00000000 00000000 00000000  // Overflow
/2
================================================
-1073741824: 11000000 00000000 00000000 00000000  // Signed divide, same as >> 1.

让我们转移"到>>>:

          1: 00000000 00000000 00000000 00000001
+2147483647: 01111111 11111111 11111111 11111111
================================================
-2147483648: 10000000 00000000 00000000 00000000  // Overflow
>>> 1
================================================
+1073741824: 01000000 00000000 00000000 00000000  // Unsigned shift right.

这篇关于“&gt;&gt;&gt;"是什么意思在java中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆