&是什么QUOT;>>>"在java中是什么意思? [英] what does ">>>" mean in java?
问题描述
我发现这个code找到<一个副本href=\"http://stackoverflow.com/questions/12372385/binary-search-olog-n-algorithm-to-find-duplicate-in-sequential-list?rq=1\">SO张贴在这里。
但我不明白这是什么意思行 INT中旬=(低+高)GT;&GT;&GT; 1;
私有静态诠释findDuplicate(INT []数组){
INT低= 0;
INT高= array.length - 1; 而(低&LT; =高){
INT中旬=(低+高)GT;&GT;&GT; 1;
的System.out.println(中);
INT midVal =阵列[MID]; 如果(midVal ==中旬)
低=中旬+ 1;
其他
高=中旬 - 1;
}
高回报;
}
的&GT;&GT;&GT;
运算符是的无符号的右位移位在Java中运营商。它有效地把通过 2
操作数右操作数的电源,或者只是 2
在这里。
之间的差异&GT;&GT;
和&GT;&GT;&GT;
转向负时,只会出现数字。在&GT;&GT;
运营商移动一个 1
位进入最显著位,如果它是一个 1
和&GT;&GT;&gt;在<$ C $
班C> 0 不管。
更新:
让平均 1
和 2147483647
( Integer.MAX_VALUE的
)。我们可以很容易地算一算:
(1 + 2147483647)/ 2 =2分之2147483648= 1073741824
现在,用code (低+高)/ 2
,这些都是涉及到的位:
1:00000000 00000000 00000000 00000001
+2147483647:01111111 11111111 11111111 11111111
================================================
-2147483648:千万00000000 00000000 00000000 //溢出
/ 2
================================================
-1073741824:11000000 00000000 00000000 00000000 //符号除法,同&GT;&GT; 1。
让我们把转移到&GT;&GT;&GT;
:
1:00000000 00000000 00000000 00000001
+2147483647:01111111 11111111 11111111 11111111
================================================
-2147483648:千万00000000 00000000 00000000 //溢出
&GT;&GT;&GT; 1
================================================
1073741824:01000000 00000000 00000000 00000000 //无符号右移。
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;
}
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.
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.
UPDATE:
Let's average 1
and 2147483647
(Integer.MAX_VALUE
). We can do the math easily:
(1 + 2147483647) / 2 = 2147483648 / 2 = 1073741824
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.
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.
这篇关于&是什么QUOT;&GT;&GT;&GT;&QUOT;在java中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!