Information Technology Reference
In-Depth Information
Shift Operators
The bitwise shift operators shift the bit pattern either right or left a specified number of posi-
tions, with the vacated bits filled with 0s. The shift operators are listed in Table 8-13.
The shift operators are binary and left-associative. The syntax of the bitwise shift operators
is shown here. The number of positions to shift is given by Count .
Operand << Count // Left shift
Operand >> Count // Right shift
Table 8-13. The Shift Operators
Operator
Name
Description
<<
Left shift
Shifts the bit pattern left by the given number of positions. The bits
shifted off the left end are lost. Bit positions opening up on the right are
filled with 0s.
>>
Right shift
Shifts the bit pattern right by the given number of positions. Bits shifted
off the right end are lost. If the left-most bit of the operand is a 1, bit
positions opening up on the left are filled with 1s. Otherwise, they are
filled with 0s.
Figure 8-6 shows how the expression 14 << 3 would be evaluated in a byte . This operation
causes the following:
￿
Each of the bits in the operand (14) is shifted three places to the left.
￿
The three bit positions vacated on the right end are filled with 0s.
￿
The resulting value is 112.
Search WWH ::




Custom Search