- 締切済み
シフトで埋められる値
シフトで埋められるのが0なのか1なのかを調べたいのですが、JAVAは環境に依存しにくいということになっているので、実験して出た結果が仕様であるということで間違いないでしょうか? 既に実験されている方のサイトや、仕様の情報をご存知でしたら教えてください。
- みんなの回答 (1)
- 専門家の回答
みんなの回答
- sakusaker7
- ベストアンサー率62% (800/1280)
ほい、仕様。 Expressions http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121 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 . 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.)