tags:

views:

875

answers:

6

What does the double greater than sign mean in Java? I had never seen it used befor but came accross it today and I tried googling it, but google hates punctuation (which is really annoying some times).

+13  A: 

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.

Clint
It can also occur when closing nested generic arguments. Unlike C++98, Java goes out of its way to effectively interpret the two characters as separate symbols in a context dependent manner, rather than forcing a space character between the two.
Tom Hawtin - tackline
+3  A: 

That is a right bit shift.

mandaleeka
A: 

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... :) )

jacobangel
+4  A: 

It shifts the bits...

heres some info on java operators

For example

101  = 5
Shifting out the right "1"
10 = 2
Shifint the other way...
1010 = 10
Chris Klepeis
+1 for table of Java operators
rascher
+1  A: 

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.)

John Rudy
+10  A: 

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.

Yuval A
If you give an example with negative numbers, you'll get my upvote.
Michael Myers
updated with negatives
Yuval A
My memory was faulty; I thought >> was the one that acted "weird" with negative numbers. +1 for jogging my memory.
Michael Myers
Nope, bitwise operations on most CPUs compensate for the MSB (the sign bit for signed values). If MSB was 0 (positive value), it will remain 0 during >>. IF MSB was 1 (negative), it will remain 1.
Yuval A
Yes, I was thinking that "unsigned right shift" meant that it ignored the sign and shifted all the *other* bits, when it really means that it treats the sign bit like any other bit.
Michael Myers