“>>>”在java中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19058859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what does ">>>" mean in java?
提问by eagertoLearn
I found this code to find duplicates in SOpost here.
but I dont understand what this line means int mid = (low + high) >>> 1;
我发现此代码可以在此处的SO帖子中查找重复项。但我不明白这条线是什么意思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;
}
采纳答案by rgettman
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.
该>>>
操作是在Java中无符号的右位移运算符。它有效地将操作数除以2
右操作数的幂,或者只是2
在这里。
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.
>>
和之间的差异>>>
只会在移动负数时出现。该>>
运营商转变一个1
位成为最显著位,如果它是一个1
,而>>>
在转变0
不管。
UPDATE:
更新:
Let's average 1
and 2147483647
(Integer.MAX_VALUE
). We can do the math easily:
让我们求平均值1
和2147483647
( Integer.MAX_VALUE
)。我们可以很容易地进行数学计算:
(1 + 2147483647) / 2 = 2147483648 / 2 = 1073741824
Now, with the code (low + high) / 2
, these are the bits involved:
现在,使用代码(low + high) / 2
,这些是涉及的位:
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.
Let's make the "shift" to >>>
:
让我们“转移”到>>>
:
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.
回答by Peter Lawrey
The significance of
的意义
int mid = (low + high) >>> 1;
is; by using the unsigned shift, it avoids overflows which result in a negative number. This is needed as Java doesn't support unsigned int
values. (BTW char
is unsigned)
是; 通过使用无符号移位,它避免了导致负数的溢出。这是必需的,因为 Java 不支持unsigned int
值。(顺便说一句char
,未签名)
The traditional way to write this was
传统的写法是
int mid = (low + high) / 2; // don't do this
however this could overflow for larger sums and you get a negative number for the mid.
但是,这可能会因金额较大而溢出,并且中位数为负数。
e.g.
例如
int high = 2100000000;
int low = 2000000000;
System.out.println("mid using >>> 1 = " + ((low + high) >>> 1));
System.out.println("mid using / 2 = " + ((low + high) / 2));
prints
印刷
mid using >>> 1 = 2050000000
mid using / 2 = -97483648
clearly the second result is incorrect.
显然,第二个结果是不正确的。
回答by Subhranshu
its a bitwise operator .. It works on the bit value . Suppose if A holds 60 then A>>>2 will give 15 (bit value 0000 1111)
它是一个按位运算符 .. 它适用于位值。假设如果 A 持有 60 那么 A>>>2 将给出 15(位值 0000 1111)
Its actual name is "Shift Zero right Operator" here the left operands value is moved right by the number of bits specified (2 in this case) by the right operand and shifted values are filled up with zeros(0000).
它的实际名称是“右移零运算符”,此处左操作数的值向右移动右操作数指定的位数(在本例中为 2),移位后的值用零 (0000) 填充。