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.
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.
Left shift operator shifts the bits of the number towards left a specified number of positions. The symbol for this operator is <<.
>> 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).
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With