开发者

what's the usage about '>>>' operator in java? [duplicate]

This question already has answers here: Closed 12 years ago. 开发者_Python百科

Possible Duplicate:

Java's >> versus >>> Operator?

Hi,

I know >> or << can improve the performance , but what's the purpose about >>> operator ?

ex PriorityQueue class in JDK source file,

 private void heapify() {
    for (int i = (size >>> 1) - 1; i >= 0; i--)
        siftDown(i, (E) queue[i]);
}

Don't tell me how >>> works , just why I use it .

Thank you


The >> operator preserves the left most bit, but >>> does not.

This means if you shift a negative number with >> it stays negative, but if you use >>> it does not.

So use >> for mathematic operations, and >>> for bit bases operations.


Use <<, >> and >>> for operations on integral types that represent bit patterns.

DO NOT use them to "speed up" multiplication and division. The chances are that it won't make any difference, and it may actually make your code slower.

The Java JIT compiler should be capable of generating machine code for simple arithmetic expressions that is optimal for the hardware that it is currently running on.

If you implement your arithmetic using clever masking and shifting, there is a chance that 1) the code won't be optimal for the machine you are running on, 2) the JIT optimizer won't figure out that you are actually doing arithmetic ... and therefore won't be able to optimize. The end result will be slower code.


Difference between >> and >>> operators is that >> is for unsigned shift. It means that >>> clears most left bit when >> preserves value of this bit (because in fact this bit means negative value) Example: you have value FFFFFFFEh = -2 (signed) then:

-2 >> 1 = FFFFFFFF = -1 // >> preserves highest bit value; note, this wrong if we treat FFFFFFFF as unsigned value

-2 >>> 1 = 7FFFFFFF // >>> clears highest bit

so if you want to operate with unsigned values you should use >>> instead of >>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜