43

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.

hologram
  • 533
  • 2
  • 5
  • 21
Petey B
  • 11,439
  • 25
  • 81
  • 101
  • Didn't we just have a question on >> vs. >>>? – Michael Myers Jun 26 '09 at 20:06
  • 2
    if you like the double, you will love the triple. see http://stackoverflow.com/questions/1034640/javas-versus-operator – akf Jun 26 '09 at 20:06
  • Yeah, searching symbols on google is very annoying; trying to look up ">>" even with the quotes and advanced search found nothing, google just ignored it. – Da-Jin Apr 03 '14 at 23:44
  • If you have trouble searching symbols on Google, you can try searching using this search engine: http://symbolhound.com/ gladly accepts symbols – Jomar Sevillejo Feb 27 '15 at 04:32

7 Answers7

76

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 Adam
  • 161,610
  • 92
  • 305
  • 395
  • 2
    If you give an example with negative numbers, you'll get my upvote. – Michael Myers Jun 26 '09 at 20:09
  • My memory was faulty; I thought >> was the one that acted "weird" with negative numbers. +1 for jogging my memory. – Michael Myers Jun 26 '09 at 20:31
  • 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 Adam Jun 26 '09 at 20:33
  • 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 Jun 26 '09 at 20:38
  • @YuvalAdam What is the purpose of the bitwise shift (this is something I've wondered for a while) given that it is equivalent to *2 and /2 (or powers of)? – Richard Tingle May 27 '13 at 17:34
  • @RichardTingle, it's just another operator you can use. In some cases, it makes semantic sense to use bitwise shifts, such as when actually working with bitmasks. – Yuval Adam May 28 '13 at 08:52
  • Why does it always return integer instead of float? For example: 2 >> 2 must return 2/(2^2) = 2/4 = 0.5, so it returns 0 – nikoloza Jul 03 '14 at 23:16
  • So, instead of using * and / they're using >> and << ? Nice way to confuse new programmers for no reasons. – frosty Feb 02 '16 at 13:53
38

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
  • 8,988
  • 1
  • 26
  • 40
  • 1
    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 Jun 26 '09 at 20:26
18

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
BrutalDev
  • 6,181
  • 6
  • 58
  • 72
Chris Klepeis
  • 9,783
  • 16
  • 83
  • 149
7

The Right Shift:

The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. Its general form :value >> num Here, num specifies the number of positions to right-shift the value in value. That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num. The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8:

int a = 32;
a = a >> 2; // a now contains 8

When a value has bits that are “shifted off,” those bits are lost. For example, the next code fragment shifts the value 35 to the right two positions, which causes the two low-order bits to be lost, resulting again in a being set to 8.

int a = 35;
a = a >> 2; // a still contains 8

Looking at the same operation in binary shows more clearly how this happens:

00100011 35 >> 2
00001000 8

Each time you shift a value to the right, it divides that value by two—and discards any remainder. You can take advantage of this for high-performance integer division by 2. Of course, you must be sure that you are not shifting any bits off the right end. When you are shifting right, the top (leftmost) bits exposed by the right shift are filled in with the previous contents of the top bit. This is called sign extension and serves to preserve the sign of negative numbers when you shift them right. For example, –8 >> 1 is –4, which, in binary, is

11111000 –8 >>1
11111100 –4

It is interesting to note that if you shift –1 right, the result always remains –1, since sign extension keeps bringing in more ones in the high-order bits. Sometimes it is not desirable to sign-extend values when you are shifting them to the right. For example, the following program converts a byte value to its hexadecimal string representation. Notice that the shifted value is masked by ANDing it with 0x0f to discard any sign-extended bits so that the value can be used as an index into the array of hexadecimal characters.

// Masking sign extension.
class HexByte {
  static public void main(String args[]) {
    char hex[] = {
      '0', '1', '2', '3', '4', '5', '6', '7',
      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
    };
  byte b = (byte) 0xf1;
 System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}

Here is the output of this:

b = 0xf1
DPP
  • 12,716
  • 3
  • 49
  • 46
5

That is a right bit shift.

mandaleeka
  • 6,577
  • 1
  • 27
  • 34
1

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
  • 6,896
  • 2
  • 34
  • 35
1

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

Community
  • 1
  • 1
John Rudy
  • 37,282
  • 14
  • 64
  • 100