I used to use the >> operator for right shifting. Now I've just replaced it with >>> and found the same result. So I can't figure out whether these two are fundamentally equal or not.
The >> operator is a signed right shift operator and >>> is an unsigned right shift operator. The left operands value is moved right by the number of bits specified by the right operand.
&& is used to perform and operation means if anyone of the expression/condition evaluates to false whole thing is false. || is used to perform or operation if anyone of the expression/condition evaluates to true whole thing becomes true. so it continues till the end to check atleast one condition to become true.
It is a binary AND Operator and copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100. Whereas && is a logical AND operator and operates on boolean operands. If both the operands are true, then the condition becomes true otherwise it is false.
>>
is arithmetic (signed) right-shift, >>>
is logical (unsigned) right-shift, as described in the Java tutorial. Try them on a negative value, and you will see a difference.
The first operator sign-extends the value, shifting in a copy of the sign bit; the second one always shifts in a zero.
The reason for this is to emulate unsigned integers for the purpose of doing bit operations, partially compensating for the lack of unsigned integral types in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With