What does the >>
sign mean in Java? I had never seen it used before but came across it today. I tried searching for it on Google, but didn't find anything useful.
The >> operator is the bitwise right shift operator.
In Java, Greater Than or Equal To Relational Operator is used to check if first operand is greater than or equal to the second operand. In this tutorial, we will learn how to use the Greater Than or Equal To Operator in Java, with examples. The symbols used for Greater Than or Equal To operator is >= .
Left shift operator shifts the bits of the number towards left a specified number of positions. The symbol for this operator is <<.
The symbols used for Greater Than operator is > . Greater Than operator takes two operands: left operand and right operand as shown in the following. The operator returns a boolean value of true if x is greater than y , or false if not.
The >>
operator is the bitwise right shift operator.
Simple example:
int i = 4; System.out.println(i >> 1); // prints 2 - since shift right is equal to divide by 2 System.out.println(i << 1); // prints 8 - since shift left is equal to multiply by 2
Negative numbers behave the same:
int i = -4; System.out.println(i >> 1); // prints -2 System.out.println(i << 1); // prints -8
Generally speaking - i << k
is equivalent to i*(2^k)
, while i >> k
is equivalent to i/(2^k)
.
In all cases (just as with any other arithmetic operator), you should always make sure you do not overflow your data type.
This is the bit shift operator. Documentation
The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
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