31

Possible Duplicate:
What does >> and >>> mean in Java?

I ran across some unfamiliar symbols in some java code, and while the code compiles and functions correctly, I am confused as to what exactly the angle brackets are doing in this code. I found the code in com.sun.java.help.search.BitBuffer, a fragment of which is below:

public void append(int source, int kBits)
    {
        if (kBits < _avail)
        {
            _word = (_word << kBits) | source;
            _avail -= kBits;
        }
        else if (kBits > _avail)
        {
            int leftover = kBits - _avail;
            store((_word << _avail) | (source >>> leftover));
            _word = source;
            _avail = NBits - leftover;
        }
        else
        {
            store((_word << kBits) | source);
            _word = 0;
            _avail = NBits;
        }
    }

What do those mysterious looking brackets do? It almost looks like c++ insertion/extraction, but I know that Java doesn't have anything like that.

Also, I tried googling it, but for some reason Google seems to not see the angle brackets, even if I put them in quotes.

MD XF
  • 7,860
  • 7
  • 40
  • 71
AJMansfield
  • 4,039
  • 3
  • 29
  • 50
  • 3
    They are Bit-Shift operators, read about it [here](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) and more detailed [here](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html) – jlordo Nov 14 '12 at 21:10
  • Just to add, >>> right shifting of bits means dividing the number by no of bit shifts raised to the power of 2 and << left shifting of bits means multiplying the number by no of bit shifts raised to the power of 2 – Aarish Ramesh Sep 20 '17 at 08:19

3 Answers3

47

They are Bitwise Bit shift operators, they operate by shifting the number of bits being specified . Here is tutorial on how to use them.

The signed left shift operator "<<" shifts a bit pattern to the left

The signed right shift operator ">>" shifts a bit pattern to the right.

The unsigned right shift operator ">>>" shifts a zero into the leftmost position

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
8

straight from ORACLE DOC.

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.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
4

Bitwise shifting. Please see the official docs here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Kristof Jozsa
  • 6,612
  • 4
  • 28
  • 32