Worked examples — Barrel shifters
This page is the worked-example drill floor for the barrel-shifter topic. The parent note built the machine; here we run every kind of input through it until no scenario can surprise you. If a symbol looks unfamiliar, the parent note and Multiplexers, Binary Number Representation, Shift and Rotate Operations define it — but every worked case below re-earns what it uses.
The scenario matrix
Every case a shift/rotate problem can throw at you falls into one of these cells. The examples afterwards are each tagged with the cell they cover.
| # | Case class | What is special about it | Covered by |
|---|---|---|---|
| A | Zero shift () | all sliders OFF — degenerate identity | Ex 1 |
| B | Single power-of-two shift () | exactly one slider ON | Ex 2 |
| C | Multi-bit shift ( has several 1-bits) | several sliders ON, sum decomposition | Ex 3 |
| D | Maximum shift () | all sliders ON, edge of the range | Ex 4 |
| E | Over-shift / out of range () | control width overflow, all bits leave | Ex 5 |
| F | Rotate (wrap-around) | bits wrap instead of vanishing | Ex 6 |
| G | Right vs left direction | index goes not | Ex 7 |
| H | Real-world word problem | address scaling / multiply-by-power-of-two | Ex 8 |
| I | Exam twist — hardware counting | muxes + delay, not the data | Ex 9 |
| J | Exam twist — arithmetic (sign) shift | vacated bits copy the sign bit, not 0 | Ex 10 |
We use one convention throughout, taken straight from the parent's [!formula] callout:
Here means "NOT " (the slider is off), means "slider on, pull the shifted bit", and is logical OR — exactly the 2:1 multiplexer equation .

The figure above shows the four-stage skeleton once, so later examples can just say "stage 2 is ON".
Example 1 — Cell A: the zero shift (degenerate identity)
Forecast: guess before reading — does anything move at all?
- Write in binary. control bits. , so . Why this step? The control bits are the binary digits of the shift amount; three bits cover –.
- Check each slider. → every stage is in pass-through mode. Why this step? From the formula, gives — the wire just carries the bit straight through.
- Result: . Why this step? No slider is on, so no displacement is added; is the identity of "sum of powers of two".
Verify: interpret as a number: . Left shift by 0 = . Output . ✅ The degenerate case just returns the input.
Example 2 — Cell B: a single power-of-two shift
Forecast: how many sliders switch on for ?
- Binary of . , so . Only stage 2 is ON (it jumps by ). Why this step? is itself a power of two, so exactly one bit is set — exactly one slider fires.
- Apply stage 2: (fill 0 when ). Why this step? Left shift by 4 pulls each output bit from four positions lower; the bottom four positions have no source, so they become 0.
- Slide the bits. Input
0000 0011→ the two set bits at positions 0,1 move to positions 4,5:0011 0000. Why this step? Bit at position lands at ; , .
Verify: . Output 0011 0000 . ✅
Example 3 — Cell C: multi-bit shift (sum of powers)
Forecast: which stages light up? Sum them before you read.
- Binary of . . , so . Why this step? Stages ON = 8, 4, 1; stage 2 (value 4… wait, that is ) — recount: bits set are . Sum . ✅
- Stage 0 (shift 1):
...0001→...0010. Why this step? pulls ; the lone bit moves up one. - Stage 1 (pass, ):
...0010unchanged. Why this step? Slider off = wire straight through. - Stage 2 (shift 4):
...0010→...0010 0000(bit at pos 1 → pos 5). Why this step? pulls . - Stage 3 (shift 8): bit at pos 5 → pos 13:
0010 0000 0000 0000. Why this step? pulls ; .

Verify: . Output has a single 1 at position 13 . ✅
Example 4 — Cell D: the maximum shift
Forecast: at maximum shift, where does the bottom bit end up — and what fills behind it?
- Binary of . , so : all three sliders ON (jumps 1+2+4 = 7). Why this step? is the all-ones control pattern — the edge of the addressable range.
- Cascade: bit at pos 0 → after +1 → pos1 → after +2 → pos3 → after +4 → pos7. Why this step? Displacements add: .
- Fill behind it with 0 (logical shift, vacated indices out of range). Why this step? Rule 1: pulled index gives .
Verify: . Output 1000 0000 . ✅ Maximum single-cycle shift reaches exactly the top bit.
Example 5 — Cell E: over-shift beyond the word
Forecast: needs 4 bits but the control bus is only 3 wide — what gets lost?
- Control-width overflow. needs 4 bits, but only exist. The top bit () has no wire. Why this step? Parent [!mistake]: shift-amount width is , not the data width.
- What the hardware sees. Only the low 3 bits arrive: , so . It shifts by 1, not 9. Why this step? Truncating a binary number to bits is the same as taking it .
- Semantic result. For a logical shift by , every data bit leaves the word → the mathematically correct answer is all zeros. Real ISAs handle this two ways: either mask the amount (, giving the shift-by-1 above) or saturate to 0. Why this step? Every source bit's destination index is out of range → rule 1 → 0.
Verify (masked model): if input were 0000 0001 and amount masked to 1, output = 0000 0010 = 2. And , . ✅ (Saturated model would give 0.)
Example 6 — Cell F: rotate right (wrap-around)
Forecast: in a rotate nothing is lost — so where does the top bit land?
- Binary of . . , so : rotate by 1, then by 2. Why this step? Same decomposition — but now the edge rule is wrap, not zero-fill.
- Rotate right by 1: .
1000→0100. Why this step? Right rotate pulls from one position higher; the bit leaving the bottom re-enters the top via . - Rotate right by 2: apply again.
0100→0001. Why this step? ; total wrap displacement .

Verify: rotate-right-by-3 of 1000 = rotate-left-by-1 = 0001. Direct check: 1000→0100→0010→0001. ✅ No bits vanished — the hallmark of rotate.
Example 7 — Cell G: direction — left vs right on the same input
Forecast: one output should be a bigger number, one smaller — which is which?
- Left by 2 (): .
0001 1000→0110 0000. Why this step? Left pulls from a lower index → bits move up → multiply. - Value check left: .
0110 0000. ✅ - Right by 2 (, direction reversed): , fill top with 0.
0001 1000→0000 0110. Why this step? Right pulls from a higher index → bits move down → divide. - Value check right: .
0000 0110. ✅
Verify: left gives , right gives ; and . The direction flips the sign of the index displacement. ✅
Example 8 — Cell H: real-world word problem (address scaling)
Forecast: why << 3 and not a multiply? (Hint: .)
- Why a shift, not a multiplier. Element size , and multiplying by a power of two is a left shift by that exponent. A shifter costs mux delays; a multiplier costs far more. Why this step? This is the parent's "multiply/divide-by-powers-of-2 every cycle" motivation, concretely.
- Shift amount → stages 0 and 1 ON (jumps 1 and 2, sum 3). Why this step? 's binary form selects the sliders.
- Shift left by 3.
...0000 0101→...0010 1000. Why this step? Bits at positions 0 and 2 move to positions 3 and 5.
Verify: . ...0010 1000 . Element 5 lives at byte 40 — matches . ✅
Example 9 — Cell I: exam twist, count the hardware (not the data)
Forecast: which of these scale as , and which as ?
- Stages . Why this step? One optional stage per bit of the shift amount.
- Control width bits (range –). Why this step? needs 6 bits — not 64. This is the classic trap.
- Total muxes . Why this step? Each of the 6 stages needs muxes, one per output bit.
- Delay mux delays. Why this step? Signal passes through 6 cascaded stages; delay grows logarithmically — the whole point (Logarithmic Delay Structures).
Verify: stages 6, control 6, muxes , delay 6. Consistency: stages = delay. ✅
Example 10 — Cell J: exam twist, arithmetic (sign-extending) right shift
Forecast: a logical right shift fills the top with 0. An arithmetic shift is different — guess what fills in.
- Read the sign bit. Top bit → is negative in two's complement. Its value: . Why this step? Arithmetic shifts preserve sign, so the sign bit governs the fill rule.
- Shift the pattern right by 2, but fill the two vacated top bits with copies of (not 0). Why this step? Rule 3: arithmetic right shift copies the old sign so the result stays negative — dividing correctly.
- Apply:
1101 0000→ shift right 2 with sign-fill →1111 0100. Why this step? Body bits slide down 2; top two positions get11.
Verify: 1111 0100 in two's complement. And (rounding toward ). ✅ A logical shift would have given 0011 0100 — wrong for signed data.
Active Recall
Recall Which matrix cells still use the plain formula
? All of them — only the out-of-range fill changes (0 for logical, for rotate, sign-copy for arithmetic). The mux structure is identical.
Recall Why does shift-by-9 on an 8-bit shifter not shift by 9?
The control bus is only bits; is truncated to (masked) or saturated to all-zeros.
Recall 64-bit shifter: muxes and delay?
muxes; delay mux delays.
Recall Arithmetic right shift of a negative number fills the top with…?
Copies of the sign bit (1 for negatives), so the value stays negative and the shift equals division by a power of two.
Connections
- Multiplexers — every case is still just 2:1 muxes; only changes.
- Binary Number Representation — decomposing and the two's-complement value read in Ex 10.
- Shift and Rotate Operations — logical vs rotate vs arithmetic, all drilled above.
- Combinational Circuits — none of these examples needed a clock.
- Logarithmic Delay Structures — the delay counted in Ex 9.
- Floating Point Normalization — the real consumer of fast variable shifts.