"Byte x is initially equal to 1011
What will be the sign of x after x>>2 operation?"
What will be the sign of x after x>>2 operation?"
Немного теории:
------------------------------------------------------------------------------------
Операторы сдвига включают левый сдвиг '<<', знаковый правый сдвиг '>>', и беззнаковый правый сдвиг '>>>' они синтаксически лево-ассоциативны (они следуют слева на право). Операнд с правой стороны оператора сдвига - это значение для сдвига; с правой стороны - операнд, определяющий дистанцию сдвига.
Операнды должны быть примитивных типов, иначе случиться ошибка компиляции. (дальше не асилил...)
Binary numeric promotion (§5.6.2) is not performed on the operands; rather, unary numeric promotion (§) is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand.
If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.
If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x3f. The shift distance actually used is therefore always in the range 0 to 63, inclusive.
At run time, shift operations are performed on the two's complement integer representation of the value of the left operand.
The value of n'<<'s is n left-shifted s bit positions; this is equivalent (even if overflow occurs) to multiplication by two to the power s.
The value of n>>s is n right-shifted s bit positions with sign-extension. The resulting value is ⌊n/2s⌋. For nonnegative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s.
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. The added term (2<<~s) or (2L<<~s) cancels out the propagated sign bit. (Note that, because of the implicit masking of the right-hand operand of a shift operator, ~s as a shift distance is equivalent to 31-s when shifting an int value and to 63-s when shifting a long value.)
Операнды должны быть примитивных типов, иначе случиться ошибка компиляции. (дальше не асилил...)
Binary numeric promotion (§5.6.2) is not performed on the operands; rather, unary numeric promotion (§) is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand.
If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.
If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x3f. The shift distance actually used is therefore always in the range 0 to 63, inclusive.
At run time, shift operations are performed on the two's complement integer representation of the value of the left operand.
The value of n'<<'s is n left-shifted s bit positions; this is equivalent (even if overflow occurs) to multiplication by two to the power s.
The value of n>>s is n right-shifted s bit positions with sign-extension. The resulting value is ⌊n/2s⌋. For nonnegative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s.
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. The added term (2<<~s) or (2L<<~s) cancels out the propagated sign bit. (Note that, because of the implicit masking of the right-hand operand of a shift operator, ~s as a shift distance is equivalent to 31-s when shifting an int value and to 63-s when shifting a long value.)
-----------------------------------------------------------------------------------
В итоге получаем, что оператор >> меняет знаковый бит, поэтому значение 1011 станет 1110 после данной операции.
источник: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.19
Комментариев нет:
Отправить комментарий