Shift Operations
Shifting a binary number left or right by a fixed number of positions is one of the most efficient operations in digital circuit design. In digital hardware, a constant shift requires zero logic gates. The operation re-routes physical copper interconnect wires. Signal wire $i$ connects directly to input pin $i + k$, while vacant bit positions connect to ground or power. This article examines the operational principles of Logical Shifts, Arithmetic Shifts, and Circular Rotations.
Principles of Positional Binary Shifts
Binary numbers use positional base-2 notation:
$$V = \sum_{i=0}^{N-1} b_i \cdot 2^i$$
Shifting every bit one position toward the most significant bit (MSB) changes the value to:
$$V’ = \sum_{i=0}^{N-1} b_i \cdot 2^{i+1} = 2 \times V$$
Therefore, shifting left by $k$ positions multiplies the binary value by $2^k$.
Shifting right by $k$ positions divides the binary value by $2^k$.
Microprocessor architectures use shift operations for address calculations (such as base + 4 * index), floating-point mantissa alignment, digital filters, and cryptographic hash functions.
Mathematical Properties of Binary Shifts
For an $N$-bit value $x$ and shift amount $k$:
$$x \ll k = x \cdot 2^k$$
$$x \gg k = \lfloor x / 2^k \rfloor$$
While left shifts operate identically for unsigned and signed numbers, right shifts require distinct logical and arithmetic implementations.
Logical Shift Left (LSL)
A Logical Shift Left (LSL) moves each bit $k$ positions toward the MSB. The circuit inserts 0 into the vacated $k$ least significant bit positions. Bits shifted beyond position $N-1$ are discarded or captured in the processor Carry flag.
Table 1 demonstrates an LSL operation by 2 positions on an 8-bit word:
| Operational State | Binary Value | Unsigned Interpretation | Signed Interpretation |
|---|---|---|---|
| Initial Value | 0011_0100 |
$52_{10}$ | $+52_{10}$ |
| After LSL 2 | 1101_0000 |
$208_{10}$ ($52 \times 4$) | $-48_{10}$ (Signed Overflow) |
The unsigned result is correct ($52 \times 4 = 208$). However, the signed result overflows because bit 7 flipped to 1. An unsigned left shift is exact only when the upper $k$ bits are 0. A signed left shift is exact only when the upper $k + 1$ bits are identical.
Logical Shift Right (LSR) Versus Arithmetic Shift Right (ASR)
Right shifts divide numbers by powers of two. The handling of the vacated MSB positions distinguishes logical from arithmetic right shifts.
1. Logical Shift Right (LSR)
A Logical Shift Right moves bits toward the LSB and inserts logic 0 into the vacated upper bit positions. This operation performs correct division by $2^k$ for unsigned numbers. It corrupts negative signed two’s complement numbers:
$$1111_1000_2 \ (-8_{10}) \xrightarrow{\text{LSR 1}} 0111_1100_2 \ (+124_{10})$$
Inserting 0 into the sign bit converts the negative value into a positive integer.
2. Arithmetic Shift Right (ASR)
An Arithmetic Shift Right duplicates the existing sign bit ($b_{N-1}$) into all vacated upper bit positions (sign extension). Sign extension preserves the mathematical polarity of signed two’s complement numbers. ASR performs signed division by $2^k$, rounding toward negative infinity ($\lfloor x / 2^k \rfloor$):
$$1111_1000_2 \ (-8_{10}) \xrightarrow{\text{ASR 1}} 1111_1100_2 \ (-4_{10})$$
Table 2 compares LSR and ASR across positive and negative values:
| Input Value | Binary Pattern | LSR 1 (Unsigned Division) | ASR 1 (Signed Division) |
|---|---|---|---|
| $+12_{10}$ | 0000_1100 |
0000_0110 ($+6_{10}$) |
0000_0110 ($+6_{10}$) |
| $-8_{10}$ | 1111_1000 |
0111_1100 ($+124_{10}$, Incorrect) |
1111_1100 ($-4_{10}$, Correct) |
| $-1_{10}$ | 1111_1111 |
0111_1111 ($+127_{10}$, Incorrect) |
1111_1111 ($-1_{10}$, Correct) |
For positive operands, ASR and LSR produce identical results. For negative operands, ASR must be used to preserve two’s complement arithmetic integrity.
Circular Bitwise Rotations (ROL and ROR)
A circular rotation shifts bits without losing information:
- Rotate Left (ROL): Bits shift toward the MSB. Bits exiting position $N-1$ wrap around to enter position 0.
- Rotate Right (ROR): Bits shift toward the LSB. Bits exiting position 0 wrap around to enter position $N-1$.
- Rotate Through Carry (RRX): Includes the processor Carry flag in the circular shift path, forming an $(N+1)$-bit rotation chain.
Figure 1 illustrates the four primary shift and rotate operations on an 8-bit word:
Cryptographic algorithms (including AES, SHA-256, and ChaCha20) use circular rotations extensively to diffuse bit entropy without data loss.
Hardware Implementation Constraints
Table 3 compares the hardware requirements of constant versus dynamic shifts:
| Operational Mode | Implementation Circuitry | Logic Gate Count | Propagation Delay |
|---|---|---|---|
| Constant Shift / Rotate | Hardwired metal interconnect routing | 0 gates | 0 ps |
| Dynamic / Variable Shift ($k$) | Logarithmic Barrel Shifter | $O(N \log N)$ multiplexers | $\lceil \log_2 N \rceil$ MUX delays |
When synthesizing code with a fixed shift amount (such as assign y = a << 3;), synthesis compilers generate direct wiring with zero gate delay.
Constant Multiplication Synthesis Optimization
Compilers and logic synthesis tools replace expensive multiplications by constants with sequences of shifts and additions. For example, multiply variable $x$ by constant 10:
$$x \times 10 = x \times (8 + 2) = (x \ll 3) + (x \ll 1)$$
The circuit requires two hardwired shifts and a single binary adder, consuming significantly less silicon area than a full hardware multiplier.
Similarly: $$x \times 12 = (x \ll 3) + (x \ll 2)$$ $$x \times 15 = (x \ll 4) - x$$
Practice Exercises
Problem 1: Apply ASR 3 and LSR 3 to the 8-bit word $1011_0000_2$. State the resulting binary patterns, calculate their decimal values, and identify which operation is valid for signed numbers.
View Solution
Interpret the input:
- Unsigned: $1011_0000_2 = 176_{10}$.
- Signed Two’s Complement: $1011_0000_2 = -128 + 32 + 16 = -80_{10}$.
Execute Shifts:
- LSR 3: Shift right 3 positions and insert 0s at the MSB: $$\text{LSR 3}(1011_0000_2) = 0001_0110_2 = 16 + 4 + 2 = 22_{10}$$ Valid for unsigned numbers ($176 / 8 = 22$).
- ASR 3: Shift right 3 positions and replicate the sign bit ($b_7 = 1$): $$\text{ASR 3}(1011_0000_2) = 1111_0110_2 = -128 + 64 + 32 + 16 + 4 + 2 = -10_{10}$$ Valid for signed numbers ($-80 / 8 = -10$).
Problem 2: Synthesize multiplication $y = x \times 14$ using only constant shifts, additions, and subtractions for an 8-bit unsigned input $x$. Specify the required output word width.
View Solution
Decompose constant 14 into powers of two: $$14 = 16 - 2 = 2^4 - 2^1$$ $$y = (x \ll 4) - (x \ll 1)$$
Hardware implementation requires:
- Two hardwired shift routing blocks ($x \ll 4$ and $x \ll 1$).
- One binary subtractor.
Word width calculation:
- Maximum input: $x_{\text{max}} = 255_{10}$.
- Maximum output: $y_{\text{max}} = 255 \times 14 = 3{,}570_{10}$.
- Because $2^{11} = 2{,}048 < 3{,}570 < 4{,}096 = 2^{12}$, output vector $y$ requires 12 bits (
[11:0]).
Problem 3: Rotate the 4-bit word $A = 1011_2$ right by one position four successive times. Trace each step and state the final result.
View Solution
Trace rotation steps:
- Initial state: $1011_2$.
- Step 1 (ROR 1): Bit 0 (1) moves to bit 3 $\implies 1101_2$.
- Step 2 (ROR 1): Bit 0 (1) moves to bit 3 $\implies 1110_2$.
- Step 3 (ROR 1): Bit 0 (0) moves to bit 3 $\implies 0111_2$.
- Step 4 (ROR 1): Bit 0 (1) moves to bit 3 $\implies 1011_2$.
Rotating an $N$-bit word by $N$ positions returns the word to its original binary state.
In the next article, we design the Barrel Shifter to execute multi-bit dynamic shifts in a single clock cycle.