This question already has answers here:
Closed 10 years ago.
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.
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.
Bitwise shifting. Please see the official docs here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
Related
This question already has answers here:
Difference between >>> and >>
(9 answers)
Closed 1 year ago.
I have a long which is made with this code:
long binary = 0b1L<< 63;
This gives the expected result, a one at the 63rd index:
1000000000000000000000000000000000000000000000000000000000000000
But when I take this long, and apply the >> operator to it, it gives me an unexpected result.
For example, when I call this code snippet on the above binary:
long newBinary = binary >>8;
It shifts right the right amount, but it fills the leading zeros with ones:
1111111110000000000000000000000000000000000000000000000000000000
Is there a specific reason why this is happening?
>> is the signed right shift operator, which fills the leftmost position with the sign bit (leftmost bit of the original number) each time. On the other hand, the unsigned right shift operator, >>>, always fills with 0.
Steps:
1000000000000000000000000000000000000000000000000000000000000000
Shift right 8 positions:
000000001000000000000000000000000000000000000000000000000000000
Fill shifted bits on the left with original sign bit (1):
111111111000000000000000000000000000000000000000000000000000000
See also: Bitwise and Bit Shift Operators
im trying to translate this code to python, but im having a hard time doing so, don't worry about the index values and variable names, I just want to know what the ">>>" part does exactly, perhaps a python equivalent, but an explanation would be great:
target[0] = (char)(source[sourceIndex] >>> 2);
target[1] = (char)((source[sourceIndex] & 3) << 4 | source[sourceIndex + 1] >>> 4);
target[2] = (char)((source[sourceIndex + 1] & 0xf) << 2 | source[sourceIndex + 2] >>> 6);
target[3] = (char)(source[sourceIndex + 2] & 0x3f);
Any help would be appreciated
It's an "unsigned right shift".
So, if your number (x) is 11110000 (in binary).
x >>> 1 will be 01111000 (in binary).
This is opposed to x >> 1 which will result in 11111000 (in binary).
The >> tries to preserve the "sign bit" but the >>> does not.
Note: I've assumed a 8-bit integer (or a byte in Java). The same thing holds for 2-byte and 4-byte integers.
The "<<<" and ">>" are bit shift operators. Specifically,
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.
—— from The Java™ Tutorials - Bitwise and Bit Shift Operators
Thats the unsigned right shift operator. It's a bitwise operator that shifts a zero into the leftmost bit of your operand. Here - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Double Greater Than Sign (>>) in Java?
What does the following achieve ?
int num = (10-2) >> 1;
Found this very helpful -
What are bitwise shift (bit-shift) operators and how do they work?
Shift right. It moves all the bits of the number right by the number specified
10-2 = 8 = 1000 in binary
8 >> 1 = 1000 >> 1 = 100 in binary = 4
>> is just an operator to perform bitwise right shift
its bitwise right shift in java.
it shifts all binary digits,1 place towards right and pastes a zero at the leftmost end.
(10-2)=8 which is 1000 in binary,
now right shifting each digit and pasting 0 at the leftmost position, it gives 0100 which equals 4.
Right shift the indicated number of bits.
http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
First result on google for ">> operator java"
See the documentation here on Bitwise shift right
Fallowing statement is from Character class of java:
(1 << Character.PARAGRAPH_SEPARATOR)) >> type
PARAGRAPH_SEPARATOR is a byte and type is an integer.
The operators in this sentence, what do they do? how and where I can use those operators?
Here is the oracles java.lang.Character doc. Nearly all the methods in the class uses those operators.
They are bit-shift operators. << shifts the bits "left" (towards the most-significant bit), and vice-versa for >>. Shifting left or right by n bits is pretty much the same as multiplying or dividing, respectively, by 2n.
See #axtavt's comment for an explanation of how these operators are being used in this context.
These are the bitwise shift operators.
If you left shift the following byte:
00000001
you would get:
00000010
I.e. the pattern has "shifted" to the left and zeros fill in on the right. So if you apply the right shift operator >> on that result, you'll get the original byte again.
You'll notice that the decimal values of these numbers are 1 and 2. If you shift left once again you'll get:
00000100 = 4
So you see that shifting to the left multiplies the number by two (given that it doesn't overflow), while right shifting divides by two. This happens very efficiently in most computers. So that's one example of how you might use these operators in a practical way.
Bitwise operators: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
<< is the left shift operator: It shifts the binary number stored in the computer left. For example, 9 in binary is 1001. 9 << 2 makes 100100 in binary (36), because it shifts it left and adds 0s at the end. 1 << n is the same thing as Math.pow(2, n) except it is way faster and better in general, as well as returning int, not double.
>> is right shift. It shifts it right, and discards empty bits. 13 is 1101 in binary, so 13 >> 1 is 110 in binary, or 6 normally.
It works as a highly optimized multiple comparison. 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.
for more detail,You can visit below link .
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
This question already has answers here:
Difference between >>> and >>
(9 answers)
Closed 5 years ago.
What is the difference between the operator >> and >>>?
>>> right shifts and fills with 0 at the left end, while >> fills with the sign bit.
This makes a difference for the signed integral types (i.e. all but byte), where a negative value has a set sign bit.
>> Signed right shift operator and >>> unsigned right shift operator
Signed Right Shift Operator
The right shift >> operator shifts
the left operand to the right side
with sign extension by the number of
bits specified by its right operand.
This means that a value at n place
gets shifted to the right causing the
n high order bits that contains the
same value as that of unshifted value.
This operator never throws an
exception.
Unsigned Right Shift Operator
The unsigned right shift >>> operator
shifts a zero into the leftmost
position however the leftmost position
after ">>" depends on sign extension.
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.
From http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html
Here is an explanation with examples:
http://www.roseindia.net/java/master-java/bitwise-bitshift-operators.shtml
>> fills in the sign the sign bit on the left (i.e. fills in 1 for negative values, 0 for positive), whereas >>> doesn't (always 0). This is convenient when shifting negative values. There is no <<<, since the sign bit is on the left and << thus already behaves like <<< would (filling in zeros, nothing else).