What does >> and >>> mean in Java? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between >>> and >>
What does “>>>” in java mean?
What does >> and >>> mean in Java?
Why does -1 >> 2 and -1 >>> 2 have different results?

>> is a signed right shift operator which shifts a bit pattern to the right.
>>> is an unsigned right shift operator which shifts a zero into the leftmost position. Please refer to the Oracle Docs.

In java, there are 2 types of right shifts. >>> will attach 0's to fill the empty spaces for both positive and negative numbers (logical shift right) while >> will attach 1's if negative and 0's if positive (sign extension).

Related

Is there a reason why I am getting this unexpected result for >> operator? [duplicate]

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

When we use this type of syntax :- int a[]=new int[3<<1]; [duplicate]

This question already has answers here:
How does bitshifting work in Java?
(10 answers)
Closed 2 years ago.
Actually I know the declaration of array in java but here I am unable to understand the use of this syntax:
int a[] = new int[3 << 1];
In place of a size for the array, here << this symbol is used for?
The << symbol is known as the "left shift operator." As you can probably guess, it gets its name from shifting bit positions to the left.
There's a good example posted by another user at the following:
How do shift operators work in Java?
Right shift and left shift operators shift your number to the right of left in their binary representation. If you left shift by one the number 3 3<<1 this would mean that:
The binary for 3 which is 011 gets shifted to the left(basically a 0 is added in the front) 0110 and now your number is 6.
The opposite thing would happen with right shift: If you have the number 3 011 and you right shift it by 1 3>>1 the first bit will get chopped off 01 and you'll end up with the number 1.
Shift operators are useful for multiplying or dividing by powers of 2, and it's a bit faster than just saying 3*2.
<< is the left shift operator and belongs to the bitwise and bit shift operators. For a detailed explanation of how each bitwise and bit shift operator works, please refer to this answer by #DerekPark. Thus, 3 << 1 is an arthmetic expression that gets evaluated before the array is created. 3 << 1 evaluates to 6, thus an int[] of size 6 gets created.
Ideone Demo
<< is the bitwise left shift operator and is often used for efficiently multiplying for specific powers of two, as bitwise operators tend to be faster. x << n is usually equivalent to x*2n, except for cases involving int overflow.
3 in binary is represented as 11 and after shifting to the left by one, it becomes 110, which is 6 in decimal. It can be seen intuitively that shifting by n places to the left is equivalent to multiplying by two n times, as each shift adds a binary digit to the right, which increases the value of every other digit by a factor of 2.

What is >> in java? [duplicate]

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

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

Difference between >>> and >> operators [duplicate]

This question already has answers here:
Difference between >>> and >>
(9 answers)
Closed 5 years ago.
If the shifted number is positive >>> and >> work the same.
If the shifted number is negative >>> fills the most significant bits with 1s whereas >> operation shifts filling the MSBs with 0.
Is my understanding correct?
If the negative numbers are stored with the MSB set to 1 and not the 2s complement way that Java uses the the operators would behave entirely differently, correct?
The way negative numbers are represented is called 2's complement. To demonstrate how this works, take -12 as an example. 12, in binary, is 00001100 (assume integers are 8 bits though in reality they are much bigger). Take the 2's complement by simply inverting every bit, and you get 11110011. Then, simply add 1 to get 11110100. Notice that if you apply the same steps again, you get positive 12 back.
The >>> shifts in zero no matter what, so 12 >>> 1 should give you 00000110, which is 6, and (-12) >>> 1 should give you 01111010, which is 122. If you actually try this in Java, you'll get a much bigger number since Java ints are actually much bigger than 8 bits.
The >> shifts in a bit identical to the highest bit, so that positive numbers stay positive and negative numbers stay negative. 12 >> 1 is 00000110 (still 6) and (-12) >> 1 would be 11111010 which is negative 6.
Definition of the >>> operator in the Java Language Specification:
The value of n>>>s is n right-shifted s bit positions with zero-extension. If n is positive, then the result is the same as that of n>>s; if n is negative, the result is equal to that of the expression (n>>s)+(2<<~s) if the type of the left-hand operand is int, and to the result of the expression (n>>s)+(2L<<~s) if the type of the left-hand operand is long.
Just the opposite, the >>> fills with zeros while >> fills with ones if the h.o bit is 1.

Categories

Resources