Double Greater Than Sign (>>) in Java?

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.

Leave a Comment