10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15.19 Shift Operators EXPRESSIONS<br />

502<br />

15.19 Shift Operators<br />

What, I say, is to become of those wretches?<br />

. . . What more can you say to them than “shift for yourselves?”<br />

—<strong>The</strong> American Crisis<br />

<strong>The</strong> shift operators include left shift , and unsigned right<br />

shift >>>; they are syntactically left-associative (they group left-to-right). <strong>The</strong> lefthand<br />

operand of a shift operator is the value to be shifted; the right-hand operand<br />

specifies the shift distance.<br />

ShiftExpression:<br />

AdditiveExpression<br />

ShiftExpression > AdditiveExpression<br />

ShiftExpression >>> AdditiveExpression<br />

<strong>The</strong> type of each of the operands of a shift operator must be a type that is convertible<br />

(§5.1.8) to a primitive integral type, or a compile-time error occurs.<br />

Binary numeric promotion (§5.6.2) is not performed on the operands; rather,<br />

unary numeric promotion (§) is performed on each operand separately. <strong>The</strong> type<br />

of the shift expression is the promoted type of the left-hand operand.<br />

If the promoted type of the left-hand operand is int, only the five lowestorder<br />

bits of the right-hand operand are used as the shift distance. It is as if the<br />

right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1)<br />

with the mask value 0x1f. <strong>The</strong> shift distance actually used is therefore always in<br />

the range 0 to 31, inclusive.<br />

If the promoted type of the left-hand operand is long, then only the six lowest-order<br />

bits of the right-hand operand are used as the shift distance. It is as if the<br />

right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1)<br />

with the mask value 0x3f. <strong>The</strong> shift distance actually used is therefore always in<br />

the range 0 to 63, inclusive.<br />

At run time, shift operations are performed on the two’s complement integer<br />

representation of the value of the left operand.<br />

<strong>The</strong> value of ns is n right-shifted s bit positions with sign-extension. <strong>The</strong><br />

resulting value is n 2 . For nonnegative values of n, this is equivalent to truncating<br />

integer division, as computed by the integer division operator /, bytwoto<br />

the power s.<br />

<strong>The</strong> value of n>>>s is n right-shifted s bit positions with zero-extension. If n<br />

is positive, then the result is the same as that of n>>s; ifn is negative, the result is<br />

s<br />

⁄<br />

DRAFT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!