+ 3
Java Operators: <<= , >>= , >>>=
Hey guys can you pls explain me what these 3 operators do <<= , >>= , >>>= ? I dont understand it from the internet because my english is not the best. Thank you very much
3 Answers
+ 5
<<= is a bitwise left shift assignment operator
>>= is a bitwise right shift assignment operator
>>>= is a bitwise unsigned right shift, or zero left fill assignment operator
int x = 2;
x<<=1;
x is now 4;
2 in binary is:
0010
if we shift it left by 1 we get
0100
int x = 2;
x>>=1;
x is 1;
2 in binary is:
0010
if we shift it right by 1 we get
0001
Another example using a negative number:
int x = -2;
x>>=1;
x is -1;
-2 in binary is:
1110
negative numbers in binary are left filled with 1
if we shift it right by 1 we get
1111
Below we will use the full 32 bits to better see what is happening. For the ones above just imagine that the remaining 28 bits to the left are the same as the left most bit used.
int x = -2;
x>>>=1;
x is 2147483647
-2 in binary is:
1111 1111 1111 1111 1111 1111 1111 1110
if we unsigned right shift it by 1 we get
0111 1111 1111 1111 1111 1111 1111 1111
The 0 on the left will flip this to a positive number and the remaining bits will give the new value of 2147483647
+ 1
Very nice
0
This is to much