Java Tutorials : Java Operators Explained
3. Shift operators - <<, >>, >>>
- << Performs a signed left shift. 0 bits are
brought in from the right. Sign bit (MSB) is preserved.
Value becomes old value * 2 ^ x where x is no of bits
- >> Performs a signed right shift. Sign bit
is brought in from the left. (0 if positive, 1 if negative.
Value becomes old value / 2 ^ x where x is no of bits
shifted. Also called arithmetic right shift.
- >>> Performs an unsigned logical right
shift. 0 bits are brought in from the left. This operator
exists since Java doesn't provide an unsigned data type
(except char). >>> Changes the sign of a negative
number to be positive. So don't use it with negative
numbers, if you want to preserve the sign. Also don't
use it with types smaller than int. (Since types smaller
than int are promoted to an int before any shift operation
and the result is cast down again, so the end result
- Shift operators can be applied to only integral
- -1 >> 1 is -1, not 0. This differs from simple
division by 2. We can think of it as shift operation
- 1 << 31 will become the minimum value that
an int can represent. (Value becomes negative, after
this operation, if you do a signed right shift sign
bit is brought in from the left and the value remains
- Negative numbers are represented in two's complement
notation. (Take one's complement and add 1 to get two's
- Shift operators never shift more than the number
of bits the type of result can have. (i.e. int 32, long
64) RHS operand is reduced to RHS % x where x is no
of bits in type of result.
4. Comparison operators :all return
4.1 Ordinal comparisons - <, <=, > , >=
- Only operate on numeric types. Test the relative
value of the numeric operands.
- Arithmetic promotions apply. char can be compared
4.2 Object type comparison - instanceof