What is >> in java? [duplicate] - java

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

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.

Difficult formula

I am new to bytes and bits and I'm also not very good in maths either. I kind of understand the bitwise operators, but I don't know how to solve math equations/formulas with 2 variables. I'm not sure this is the proper place to ask this but anyway.
I have an formula like this:
(Adr_MSB & 0x3F) << (8 + Adr_LSB)
Now what I want is that I would get an integer (for example 33) and the code would transform it into Adr_MSB and Adr_LSB (which are bytes).
It should work up to 128 (ok I guess it will be 127).
I know that this question might sound dumb or something, but I just don't know enough maths to solve this.
Thanks for all help.
EDIT: By experimenting I figured out, that Adr_MSB is a multiplier (e.g. if it's 10 the result is 10 times biger than if it's 1).
From what I understand
the (Adr_MSB & 0x3F) part of the takes the last six bits of the Adr_MSB and returns corresponding integer.
Eg: 125 (1111101) will return 61 (111101)
Note: This step is removing all bits other than last 6 bits, these bits are lost. Hence Lossless inverse function is not possible.
The (8 + Adr_LSB) just adds 8 to Adr_LSB.
<< is a bit wise Left Shift Operator.
Eg. 61 << 3 = 488 . Since 61 is 111101, adding three zeros to the right (Left Shifting three times) will give 111101000 which is 488.
Effective inverse of the expression (Adr_MSB & 0x3F) << (8 + Adr_LSB) to be applied to given number x
Take first six bits from x and convert it to int. This will be Adr_MSB.
Count the rest of the bits. Subtract 8 from this count and it will be Adr_LSB.
Does the following represent what you are looking for?
((Adr_MSB & 0x3F) << 8) | Adr_LSB
Would this work?
int x = 33;
byte Adr_MSB = (byte)x;
byte Adr_LSB = (byte)24;
System.out.println((Adr_MSB & 0x3F) << (8 + Adr_LSB));
Adr_LSB can also be -8.
Since you do bitwise AND on Adr_MSB and 111111, you have only six consecutive bits available to represent the number, so not all integers can be represented just by shifting those 6 bits. This solution works for x up to , so... you can argue that is not a good solution, but it is a start.

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

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

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

Categories

Resources