3.3.11 · D3Combinational Circuits

Worked examples — Barrel shifters

2,711 words12 min readBack to topic

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 .

Figure — Barrel shifters

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?

  1. Write in binary. control bits. , so . Why this step? The control bits are the binary digits of the shift amount; three bits cover .
  2. 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.
  3. 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 ?

  1. 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.
  2. 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.
  3. 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.

  1. Binary of . . , so . Why this step? Stages ON = 8, 4, 1; stage 2 (value 4… wait, that is ) — recount: bits set are . Sum . ✅
  2. Stage 0 (shift 1): ...0001...0010. Why this step? pulls ; the lone bit moves up one.
  3. Stage 1 (pass, ): ...0010 unchanged. Why this step? Slider off = wire straight through.
  4. Stage 2 (shift 4): ...0010...0010 0000 (bit at pos 1 → pos 5). Why this step? pulls .
  5. Stage 3 (shift 8): bit at pos 5 → pos 13: 0010 0000 0000 0000. Why this step? pulls ; .
Figure — Barrel shifters

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?

  1. 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.
  2. Cascade: bit at pos 0 → after +1 → pos1 → after +2 → pos3 → after +4 → pos7. Why this step? Displacements add: .
  3. 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?

  1. 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.
  2. 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 .
  3. 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?

  1. Binary of . . , so : rotate by 1, then by 2. Why this step? Same decomposition — but now the edge rule is wrap, not zero-fill.
  2. Rotate right by 1: . 10000100. Why this step? Right rotate pulls from one position higher; the bit leaving the bottom re-enters the top via .
  3. Rotate right by 2: apply again. 01000001. Why this step? ; total wrap displacement .
Figure — Barrel shifters

Verify: rotate-right-by-3 of 1000 = rotate-left-by-1 = 0001. Direct check: 1000010000100001. ✅ 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?

  1. Left by 2 (): . 0001 10000110 0000. Why this step? Left pulls from a lower index → bits move up → multiply.
  2. Value check left: . 0110 0000 . ✅
  3. Right by 2 (, direction reversed): , fill top with 0. 0001 10000000 0110. Why this step? Right pulls from a higher index → bits move down → divide.
  4. 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: .)

  1. 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.
  2. Shift amount → stages 0 and 1 ON (jumps 1 and 2, sum 3). Why this step? 's binary form selects the sliders.
  3. 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 ?

  1. Stages . Why this step? One optional stage per bit of the shift amount.
  2. Control width bits (range ). Why this step? needs 6 bits — not 64. This is the classic trap.
  3. Total muxes . Why this step? Each of the 6 stages needs muxes, one per output bit.
  4. 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.

  1. 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.
  2. 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.
  3. Apply: 1101 0000 → shift right 2 with sign-fill → 1111 0100. Why this step? Body bits slide down 2; top two positions get 11.

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.

Concept Map

write in binary

switch sliders

zero

wrap mod n

copy sign bit

Shift amount S

Control bits

Power-of-two stages

Bits slide

Edge fill rule

Logical shift

Rotate

Arithmetic shift