What does ">>>" in java mean? - java

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.

Related

Unsigned right shift in Java

Simple question: why if I apply unsigned right shift in Java to byte variable (and short as well) it threats it as int:
byte x = -1;
System.out.println(x >> 2);
System.out.println(x >>> 1);
System.out.println(Integer.MAX_VALUE);
Console output:
-1
2147483647
2147483647
One can only use the shift operators on ints and longs in Java (just like all other numeric operators), thus the byte is automatically cast to an int before shifting it. This also happens with the arithmetical right shift, but -1 >> 2 is -1 no matter what type -1 is, because the binary representation 111...111 shifted right arithmetically is still 111...111, while shifted logically it becomes 011...111, i.e. the maximum value of the shifted type.
PS: An arithmetic shift is a signed shift, and a logical shift is an unsigned shift.

Which bit actually shift the << operator?

I confused with an operator which provides arithmetic shifts <<, >> in Java
For example I have this binary number:
0000 0101 // It is actually 5 in dec system
And now I want to shift this number to the left by 2 positions. I am doing this ( this is only concept of shifting bits) :
0000 0101 << 2
And now I do not know: if I need to shift high bit by 2 positions and fill with zero in right side OR I need to shift whole number (101) by 2 positions?
Second option :)
For instance, 0110001 << 2 = 1000100
The other operators are:
signed right shift (>>).
0011001 >> 2 = 0000110
1011001 >> 2 = 1110110
The leftmost bit is used as left padding. This is done to propagate the sign bit (highest bit).
unsigned right shift (>>>)
1110001 >> 2 = 0011100
Since the number is considered unsigned, there is nothing to propagate, just pad with zeros!

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

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

"<<" operator in java

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

What is the difference between the Java operators >> and >>>? [duplicate]

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).

Categories

Resources