Chapter 7: Integer Arithmetic Rotations
Shifts and Rotations
Overview of Integer Arithmetic
Discussed in class: Integer arithmetic, focused on shift and rotate operations.
Importance of shifts and rotates for efficient arithmetic operations.
Structure of the class: This session covers shifts; another session will cover multiplication and division.
Shift and Rotate Operations
Shift Operations: Involves moving bits horizontally either left or right in a binary number.
Left Shift: Multiplies the number by 2 for each shift.
Right Shift: Divides the number by 2 for each shift.
Rotation Operations: Circularly shifts bits, retaining the original bit count.
Rotating left brings the leftmost bit to the rightmost position.
Rotating right brings the rightmost bit to the leftmost position.
Detailed Explanation of Shifts
Left Shift (SHL):
Example with a byte: Starting with 0001 (1 in decimal), if shifted left, it becomes 0010 (2), then 0100 (4), then 1000 (8), etc.
Each left shift effectively multiplies by 2.
Right Shift (SHR):
Example with 0001 (1 in decimal): If shifted right, it becomes 0000 (0), if further right it remains 0000.
Each right shift effectively divides by 2, rounding down.
Types of Shifts
Logical Shift:
Ignoring the sign bit, treats all bits uniformly (0s filled in).
Used for unsigned numbers.
Arithmetic Shift:
Considers the sign of the number.
When right shifting, the sign bit is preserved to maintain the number's sign (e.g., shifting -8 (11111000) will fill with 1 on the left if it's a negative number).
Important Points on Shifting
Carry Flag:
When the leftmost or rightmost bit is shifted out, it typically stores that bit in the carry flag for later use.
Limits of Shifting:
Can only shift up to the number of bits in the register (e.g., 0-7 for an 8-bit register).
Shifting too far leads to loss of data and improper results.
Rotations
Left Rotate (ROL):
Involves taking the most significant bit (MSB) and moving it to the least significant bit (LSB) position, essentially wrapping around.
Right Rotate (ROR):
Takes the least significant bit (LSB) and places it in the MSB position, overturning the bits circularly.
No loss of information while rotating as each bit re-enters the number system, unlike shifting.