Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do << or >>> in java mean? [duplicate]

Tags:

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.

like image 318
AJMansfield Avatar asked Nov 14 '12 21:11

AJMansfield


People also ask

What does >>> mean in Java?

The >>> operator is the unsigned right bit-shift operator in Java. It effectively divides the operand by 2 to the power of the right operand, or just 2 here.

What is << used for in Java?

Left shift operator shifts the bits of the number towards left a specified number of positions. The symbol for this operator is <<.

What is the difference between >>> and >> in Java?

>> is arithmetic shift right, >>> is logical shift right. In an arithmetic shift, the sign bit is extended to preserve the signedness of the number. For example: -2 represented in 8 bits would be 11111110 (because the most significant bit has negative weight).

What does the << operator represent?

The left shift operator ( << ) shifts the first operand the specified number of bits, modulo 32, to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.


2 Answers

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

like image 52
kosa Avatar answered Oct 24 '22 05:10

kosa


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.

like image 36
PermGenError Avatar answered Oct 24 '22 05:10

PermGenError