User Guide
52 General-Purpose Programming
AMD64 Technology 24592—Rev. 3.15—November 2009
3.3.7 Rotate and Shift
The rotate and shift instructions perform cyclic rotation or non-cyclic shift, by a given number of bits
(called the count), in a given byte-sized, word-sized, doubleword-sized or quadword-sized operand.
When the count is greater than 1, the result of the rotate and shift instructions can be considered as an
iteration of the same 1-bit operation by count number of times. Because of this, the descriptions below
describe the result of 1-bit operations.
The count can be 1, the value of the CL register, or an immediate 8-bit value. To avoid redundancy and
make rotation and shifting quicker, the count is masked to the 5 or 6 least-significant bits, depending
on the effective operand size, so that its value does not exceed 31 or 63 before the rotation or shift takes
place.
Rotate
• RCL—Rotate Through Carry Left
• RCR—Rotate Through Carry Right
• ROL—Rotate Left
• ROR—Rotate Right
The RCx instructions rotate the bits of the first operand to the left or right by the number of bits
specified by the source (count) operand. The bits rotated out of the destination operand are rotated into
the carry flag (CF) and the carry flag is rotated into the opposite end of the first operand.
The ROx instructions rotate the bits of the first operand to the left or right by the number of bits
specified by the source operand. Bits rotated out are rotated back in at the opposite end. The value of
the CF flag is determined by the value of the last bit rotated out. In single-bit left-rotates, the overflow
flag (OF) is set to the XOR of the CF flag after rotation and the most-significant bit of the result. I n
single-bit right-rotates, the OF flag is set to the XOR of the two most-significant bits. Thus, in both
cases, the OF flag is set to 1 if the single-bit rotation changed the value of the most-significant bit (sign
bit) of the operand. The value of the OF flag is undefined for multi-bit rotates.
Bit-rotation instructions provide many ways to reorder bits in an operand. This can be useful, for
example, in character conversion, including cryptography techniques.
Shift
• SAL—Shift Arithmetic Left
• SAR—Shift Arithmetic Right
• SHL—Shift Left
• SHR—Shift Right
• SHLD—Shift Left Double
• SHRD—Shift Right Double