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.
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.
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.
It shifts the bits...
heres some info on java operators
For example
101 = 5
Shifting out the right "1"
10 = 2
Shifting the other way...
1010 = 10
I believe it's the bit shifting operator. As in moves all 1s and 0s one position right. (I think you can imagine what << does... :) )
As others have noted, this is the right bit-shift. You'll see it in many of the so-called "C-style" languages.
For massively detailed information about bit-shifting provided by your fellow StackOverflow users, check out a question I posted ages ago, which helped me finally get it: Absolute Beginner's Guide to Bit-Shifting. (The folks who posted there were kind enough to go into great depth on the subject, which I hope will help you as well.)