Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ever any performance different between Java >> and >>> right shift operators?

Is there ever reason to think the >> (signed) and >>> (unsigned) right bit-shift operators in Java would perform differently? I can't detect any difference on my machine.

This is purely an academic question; it's never going to be the bottleneck I'm sure. I know: it's best to write what you mean foremost; use >> for division by 2, for example.

I assume it comes down to which architectures have which operations implemented as an instruction.

like image 955
Sean Owen Avatar asked Mar 26 '10 20:03

Sean Owen


People also ask

Is there a difference between the >> and >>> operators in Java?

Difference between >> and >>> operator. Both >> and >>> are used to shift the bits towards the right. The difference is that the >> preserve the sign bit while the operator >>> does not preserve the sign bit. To preserve the sign bit, you need to add 0 in the MSB.

What is the use of >>> operator in Java?

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.

What do you mean by >>> operator in Java a left shift operator B right shift operator C Zero fill right shift d zero fill left shift?

The >> operator is a signed right shift operator and >>> is an unsigned right shift operator. The left operands value is moved right by the number of bits specified by the right operand.

Is Bitshifting faster than division?

Bit-shifting is still faster, but for non-power-of-two mul/div by the time you do all your shifts and add the results it's slower again.


1 Answers

No. Your compiler will translate these to bytecode and the JVM will interpret the bytecode for your architecture. I think it is safe to assume that your architecture has an instruction set which includes both operations be done in few clock cycles.

Anyway, there is a difference in the behavior of these operators, so it isn't like you can just interchange them.

like image 157
Tim Bender Avatar answered Oct 30 '22 04:10

Tim Bender