Shift Instructions (Short Notes)
Shift instructions manipulate the bit pattern of an operand by shifting it by a specified count. At a high level, left shifts move bits toward more significant positions, while right shifts move bits toward less significant positions. Many ISAs distinguish logical shifts (fill with zeros) from arithmetic shifts (preserve the sign bit for signed values).2
Key bit-fill rule (logical vs arithmetic)
- Logical shift fills vacated bit positions with (best for unsigned values).2
- Arithmetic shift preserves sign for two’s-complement signed integers.2
Important side effects also commonly include Carry Flag and sometimes Overflow Flag being affected by shift/rotate operations.
Footnotes
-
Shift and Rotate Instructions (Lecture PDF) - Defines shifting, lists x86 shift/rotate mnemonics, and contrasts logical vs arithmetic filling; notes flag effects. ↩ ↩2 ↩3 ↩4
-
SAL/SAR/SHL/SHR — Shift (x86 reference) - Describes CF behavior and SAR vs SHR sign/zero filling; notes SAR vs IDIV rounding difference. ↩
-
x86 Assembly/Shift and Rotate (Wikibooks) - Explains logical vs arithmetic shifts and sign-bit preservation. ↩ ↩2
Common instruction families and mnemonics (examples)
x86 family (SHL/SHR, SAL/SAR, rotations)
x86 uses these common shift/arith-right/logical-right mnemonics:
x86 behavior details (conceptual):
- For a right shift, the least significant bit shifted out typically goes into CF.2
- For SAR the vacated MSB is set/cleared to match the original sign bit.2
- For SHR the vacated MSB is cleared to .2
ARM family (LSL/LSR/ASR/ROR)
ARM shift operations typically include:
- LSL (often described as multiplying by powers of two).2
- LSR (described as unsigned division by a power of two).2
- ASR (sign-preserving right shift, for signed values).2
- ROR and related rotate/extended forms.
MIPS family (SLL/SRL/SRA; logical vs arithmetic)
MIPS shift instructions include:
Footnotes
-
SAL/SAR/SHL/SHR — Shift (x86 reference) - Describes CF behavior and SAR vs SHR sign/zero filling; notes SAR vs IDIV rounding difference. ↩ ↩2 ↩3 ↩4
-
Shift and Rotate Instructions (Lecture PDF) - Defines shifting, lists x86 shift/rotate mnemonics, and contrasts logical vs arithmetic filling; notes flag effects. ↩ ↩2 ↩3 ↩4
-
x86 Assembly/Shift and Rotate (Wikibooks) - Explains logical vs arithmetic shifts and sign-bit preservation. ↩ ↩2 ↩3 ↩4
-
Shift operations - Arm Compiler armasm User Guide (ARM) - Describes register shift operations and shift length concept. ↩ ↩2 ↩3
-
ASR, LSL, LSR, ROR, and RRX - Arm support (ARM) - States LSL multiplies by powers of two and LSR divides unsigned by power of two; covers related operations. ↩ ↩2 ↩3 ↩4
-
MIPS Instruction Reference (Goucher) - Gives SLL description: zeroes shifted in and syntax . ↩
-
Engineering LibreTexts: Shift Operations (MIPS) - Defines sll/sllv/srl and shift amount behavior; describes zero-filling for logical shifts. ↩
-
MIPS Instruction Reference (Goucher) - Gives SRA description: sign bit is shifted in; includes operation/syntax. ↩
How to interpret a shift instruction (quick workflow)
- 1Step 1
Check mnemonic: logical (fills with 0) vs arithmetic (preserves sign bit) vs rotate (wraps bits). Logical vs arithmetic difference is explicitly described in standard references.3
Footnotes
-
Shift and Rotate Instructions (Lecture PDF) - Defines shifting, lists x86 shift/rotate mnemonics, and contrasts logical vs arithmetic filling; notes flag effects. ↩
-
x86 Assembly/Shift and Rotate (Wikibooks) - Explains logical vs arithmetic shifts and sign-bit preservation. ↩
-
SAL/SAR/SHL/SHR — Shift (x86 reference) - Describes CF behavior and SAR vs SHR sign/zero filling; notes SAR vs IDIV rounding difference. ↩
-
- 2Step 2
Left shifts move toward MSB; right shifts move toward LSB. The shift amount is given either as an immediate or a register field depending on the ISA.2
Footnotes
-
MIPS Instruction Reference (Goucher) - Gives SLL description: zeroes shifted in and syntax . ↩
-
MIPS Instruction Reference (Goucher) - Gives SRA description: sign bit is shifted in; includes operation/syntax. ↩
-
- 3Step 3
For logical shifts, vacated positions are filled with zeros; for arithmetic right shifts, vacated positions are filled with the sign bit.2
Footnotes
-
Shift and Rotate Instructions (Lecture PDF) - Defines shifting, lists x86 shift/rotate mnemonics, and contrasts logical vs arithmetic filling; notes flag effects. ↩
-
x86 Assembly/Shift and Rotate (Wikibooks) - Explains logical vs arithmetic shifts and sign-bit preservation. ↩
-
- 4Step 4
On x86, shift/rotate instructions affect flags such as Carry (CF), with the last shifted-out bit commonly ending in CF.2
Footnotes
-
Shift and Rotate Instructions (Lecture PDF) - Defines shifting, lists x86 shift/rotate mnemonics, and contrasts logical vs arithmetic filling; notes flag effects. ↩
-
SAL/SAR/SHL/SHR — Shift (x86 reference) - Describes CF behavior and SAR vs SHR sign/zero filling; notes SAR vs IDIV rounding difference. ↩
-
- 5Step 5
Pick logical shifts for unsigned bit patterns and arithmetic shifts for signed two’s-complement values (especially for right shifts).3
Footnotes
-
SAL/SAR/SHL/SHR — Shift (x86 reference) - Describes CF behavior and SAR vs SHR sign/zero filling; notes SAR vs IDIV rounding difference. ↩
-
x86 Assembly/Shift and Rotate (Wikibooks) - Explains logical vs arithmetic shifts and sign-bit preservation. ↩
-
MIPS Instruction Reference (Goucher) - Gives SRA description: sign bit is shifted in; includes operation/syntax. ↩
-
Pro Tip
For signed numbers on two’s complement, use an arithmetic right shift (e.g., x86 SAR or MIPS SRA) to keep the sign meaningful. Logical right shifts (e.g., x86 SHR) are appropriate for unsigned interpretation.3
Footnotes
-
SAL/SAR/SHL/SHR — Shift (x86 reference) - Describes CF behavior and SAR vs SHR sign/zero filling; notes SAR vs IDIV rounding difference. ↩
-
x86 Assembly/Shift and Rotate (Wikibooks) - Explains logical vs arithmetic shifts and sign-bit preservation. ↩
-
MIPS Instruction Reference (Goucher) - Gives SRA description: sign bit is shifted in; includes operation/syntax. ↩
Common pitfall: “division by 2^k” depends on signedness
Right shifting is often described as division by , but the exact behavior differs for signed values. For example, x86 notes that using SAR for negative values is not the same result as IDIV (rounding differs). Prefer the arithmetic operator/intent intended by the ISA rather than assuming equivalence.
Footnotes
-
SAL/SAR/SHL/SHR — Shift (x86 reference) - Describes CF behavior and SAR vs SHR sign/zero filling; notes SAR vs IDIV rounding difference. ↩
How shift instructions relate to bit-level computation
Represent data as bits
Bit patternDecide whether the operand should be treated as signed (two’s complement) or unsigned for the purpose of the shift type."
Shift/rotate
Bit movementMove bits left/right by the shift length; rotate wraps bits if using a rotate instruction."
Fill policy
Vacated bitsLogical shifts fill with zeros; arithmetic right shifts fill with the original sign bit."
Side effects
Flags (where defined)Some architectures (e.g., x86) define flag effects such as Carry receiving a shifted-out bit."
Short notes: “Shift instructions” (what to write in an exam)
(a) Shift instructions
A shift instruction changes the position of bits inside an operand by a fixed number of positions. Common categories are:
- Logical shift: vacated bits become (e.g., x86 SHR, SHL; ARM [LSR]/[LSL]; MIPS [SRL]/[SLL]).4
- Arithmetic shift: particularly for right shifts, the vacated MSB is filled with the sign bit (e.g., x86 SAR; MIPS SRA).3
- Rotate instruction: bits shifted out on one side re-enter on the other side (e.g., x86 ROR, ROL).
Shift instructions often affect CPU flags such as Carry, depending on the instruction set architecture.2
Footnotes
-
Shift and Rotate Instructions (Lecture PDF) - Defines shifting, lists x86 shift/rotate mnemonics, and contrasts logical vs arithmetic filling; notes flag effects. ↩ ↩2 ↩3
-
x86 Assembly/Shift and Rotate (Wikibooks) - Explains logical vs arithmetic shifts and sign-bit preservation. ↩ ↩2
-
MIPS Instruction Reference (Goucher) - Gives SLL description: zeroes shifted in and syntax . ↩
-
Engineering LibreTexts: Shift Operations (MIPS) - Defines sll/sllv/srl and shift amount behavior; describes zero-filling for logical shifts. ↩
-
SAL/SAR/SHL/SHR — Shift (x86 reference) - Describes CF behavior and SAR vs SHR sign/zero filling; notes SAR vs IDIV rounding difference. ↩ ↩2
-
MIPS Instruction Reference (Goucher) - Gives SRA description: sign bit is shifted in; includes operation/syntax. ↩
Logical vs arithmetic right shift (conceptual fill behavior)
Shows the fill rule for vacated MSB positions when shifting right.
Quick reference (common mnemonics & intent)
Shift and Rotate Instructions (overview)
Shift instructions self-check
Knowledge Check
Which instruction type preserves the sign bit when shifting right a two’s-complement signed integer?