Shift Operators

   

There are three shift operators in Java:

  • Left Shift: <<

  • Signed Right Shift: >>

  • Unsigned Right Shift: >>>

The shift operators move (shift) all the bits in a number to the left or the right. The left operand is the value to be shifted and the right operand specifies the number of bit positions to shift. You can apply these operators to any integer type, but both operands are promoted to at least an int. In fact, you should limit your use of the shift operators to variables of type int or long to avoid any bit truncation effects resulting from promotions of intermediate values in your expressions.

Note

The right operand of a shift operator can be any integer value, but this can be misleading. Any shift value larger than the number of bits allocated to the result of the operation is moduloed down by this same number of bits. For example, requesting a shift of an int (32-bit operand will yield a 32-bit result) by 33 bits is the same as requesting a shift of 1 bit (33 % 32 = 1). Similarly, any request to shift a long by more than 64 bits will be moduloed down by 64. This results from the use of only the 5 (for an int ) or 6 (for a long ) lowest order bits of the right operand of a shift operator.


Left Shift

The << operator shifts the left operand to the left by the number of bits given by the right operand. For example, consider the expression:

 17 << 2 

Remember that integer literals are interpreted as type int, so the value 17 is treated as a 32-bit number. However, the upper 24 bits of 17 are all 0s, so only consider the lower 8 bits:

 00010001 

The << operator inserts 0s into the lower bits as it shifts and drops the same number of upper bits (all 0s here). This is equivalent to multiplying the left operand by 2 n where n is the right operand. Shifting the value by 2 yields

 01000100 

which is the decimal value 68 (17 * 2 2 = 68).

Caution

The twos -complement representation used for negative numbers makes it possible for the left shift operator to change positive numbers into negative and vice versa when dealing with large left operands or shift values. For example, left shifting “17 by 27 bits produces a positive number.


Signed Right Shift

The >> operator shifts the left operand to the right by the number of bits given by the right operand. In this case, it is the lower bits that are lost as a value is shifted. This operator differs from that found in C/C++ in how the upper bits are treated. The signed right shift operator applies sign extension , meaning that the upper bits that are inserted as a value is shifted are either all 0s or all 1s. The bit value inserted is the same as the uppermost bit of the left operand. Remember that in twos-complement, a 1 in the uppermost bit signifies a negative number. The result is that applying >> always produces a value that has the same sign as the original value.

Unsigned Right Shift

Although the integer types in Java are signed, the >>> operator allows you to shift values as if they were unsigned. This operator behaves identically to the C/C++ right shift operator and ignores the fact that the uppermost bit is a sign bit. When a value is shifted, the lower bits are lost and 0s are inserted into the upper bits.

The following table shows two 8-bit quantities , 31 and “17, and the results of applying each of the three shift operators to them:

Quantity x x<<2 x>>2 x>>>2
31 00011111 01111100 00000111 00000111
“17 11101111 10111100 11111011 00111011

Note

The shift operators, just like the bitwise operators, support shorthand assignment. You can use <<=, >>=, or >>>= to perform a shift operation and assign the result back to the left operand. You should never use the >>>= operator with a byte or short because it will promote the operand to an int and then assign a truncated result back into the original operand.


   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net