I am new to java, and came to know that there are two Right Shift operators in java >>
and >>>
, but only one left shift operator <<
. Why is it so?
Java supports two types of right shift operators. 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.
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.
The bitwise shift operators are the right-shift operator ( >> ), which moves the bits of an integer or enumeration type expression to the right, and the left-shift operator ( << ), which moves the bits to the left.
The left shift operator moves all bits by a given number of bits to the left. The right shift operator moves all bits by a given number of bits to the right. It is the same as the signed right shift, but the vacant leftmost position is filled with 0 instead of the sign bit.
Because logical and arithmetic left-shift operations are identical (from wikipedia).
Notice what happens to sign bit (left-most bit) in both left shifts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With