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).
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
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
I recently used the shift operators in Java and noticed that the >> operator does not have the same meaning as >> in C. In Java >> is Signed shift that keeps the first bit at the same value. In Java the equivalent to C shift is the >>> operator. The left shift operator (<<) is the same as in C and just shifts ignoring the first bit.
The things I wondered are
Why make this change?
Why is the notation not consistent so >> and << are signed shift and >>> and <<< are unsigned?
Is there any use for a signed shift operator?
There is never any need for a sign-aware left shift, since 2:s complement representation stores the sign in the most significant bit.
There's no difference between a value shifted one bit to the left in some kind of "sign-aware" manner, there's nothing you can do differently. Shift the bits to the left, insert a 0 in the least significant bit, and you're done.
With signed numbers, shifting right is not so clear-cut, which is why there are two operators.
As far as I know the meaning of >> and >>> has always been the same in Java.
Why make this change?
Machine independence. The meaning of >> is somewhat implementation dependent in C.
Signed left shift and unsigned left shift are exactly equivalent, so there's no need to have a separate operator.
On the other hand, Java has no unsigned types, so it can't depend on the type to figure out what shift to use.
For reference, I think you have it backwards -- >> is signed right shift, >>> is unsigned right shift.
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.
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