3.3.11 · D4Combinational Circuits

Exercises — Barrel shifters

2,615 words12 min readBack to topic

Before we start, one quick reminder of the vocabulary we lean on, so no symbol appears unexplained:

Definition Symbols used on this page (click to expand)
  • ::: the data width — how many bits are in the word we shift.
  • ::: the number of stages, one per power-of-two jump. When is a power of two, exactly.
  • ::: the shift amount — how far we want to move the bits, from to .
  • ::: bit of written in binary; it switches stage ON (=1) or OFF (=0).
  • ::: input / output bit at position of one stage. Position is the least-significant (rightmost) bit.
  • ::: "NOT " — the opposite of . A 2:1 multiplexer picks when the select is and when it is , written .

Before the first exercise, let us earn the one equation the whole page leans on.

Figure — Barrel shifters

The figure above shows that same two-row choice as a physical mux: one select line steering between the straight wire and the shifted wire.


Level 1 — Recognition

Exercise 1.1

An -bit barrel shifter has how many stages, and each stage is built from what kind of gate/block? Answer for .

Recall Solution

WHAT we compute: the stage count . For (a power of two): stages, shifting by , , . Each stage is a row of two-to-one (2:1) multiplexers — one mux per output bit. See Multiplexers. Answer: 3 stages, each a row of 8 2:1 muxes.

Exercise 1.2

For a 32-bit word, how wide (how many bits) is the shift-amount control input ?

Recall Solution

The shift amount ranges from to . The number of bits needed to count up to 31 is . Answer: 5 bits. (Do not confuse this with the data width of 32; see the mistake below.)


Level 2 — Application

Exercise 2.1

Which stages are switched ON to shift left by on a 32-bit word? Give the binary of and list the powers of two.

Recall Solution

Step 1 — write in binary. , so . Step 2 — read off the ON stages (the 1-bits):

  • shift by
  • shift by
  • shift by Step 3 — check the sum: . ✅ Answer: stages for 2, 4, and 16 are ON; stages for 1 and 8 pass through.

Exercise 2.2

Trace shift-left-by-22 applied to the 32-bit input value (only bit 0 set). What value comes out?

Recall Solution

A single set bit at position , shifted left by , lands at position . So it lands at position . The output value is . WHAT IT LOOKS LIKE: figure below — the lone bit slides through three ON stages (2, then 4, then 16), climbing in position.

Figure — Barrel shifters

Exercise 2.3

Using the per-bit stage equation , compute output bit for stage (shift by 4) when , given input bits and .

Recall Solution

With : , so the first term drops and . Answer: ====. The output at position 5 picked up whatever was 4 positions lower (position 1), exactly what "shift left by 4" means.


Level 3 — Analysis

Exercise 3.1

Compare hardware for : total mux count and propagation delay (in mux-delays). Then say what happens to both if you double to 128.

Recall Solution

For : .

  • Total muxes .
  • Delay mux-delays.

Double to ():

  • Muxes .
  • Delay mux-delays.

The insight: doubling the width more than doubles the muxes (, a jump — because both and grew) but the delay rose by only one mux-delay. That gentle delay growth is the whole selling point — see Logarithmic Delay Structures.

Exercise 3.2

A student claims: "Swapping the order of the shift-by-2 stage and the shift-by-8 stage gives a different output." True or false, and prove it on the bit index.

Recall Solution

False. Take a single input bit at position .

  • Order A (by 2, then by 8): .
  • Order B (by 8, then by 2): . Both land at because addition of the displacements commutes (). Every set bit moves by the same total, so the words are identical. Answer: order does not change the final data (for pure shifts/rotates).

Exercise 3.3

On a 4-bit word, rotate right by 3 the input (bit 3 set). Do it stage-by-stage using the rotate rule , then check against a single direct rotation.

Recall Solution

, so : rotate right by , then by .

  • Stage 0 (rotate right by 1): each output bit pulls from one position higher, wrapping mod 4. . (The top bit wrapped down.)
  • Stage 1 (rotate right by 2): pull from 2 higher, mod 4. . Direct check: rotating 1000 right by 3 slides the single 1 from position 3 to position : 0001. ✅ Answer: ====.

Level 4 — Synthesis

Exercise 4.1

Design (on paper) an 8-bit logical left barrel shifter: list the stages, and for each stage write the per-bit equation for output bit (the top bit).

Recall Solution

stages: shift by 1 (), 2 (), 4 (). Per-bit left-shift equation is , with (logical fill). For :

  • Stage 0 ():
  • Stage 1 ():
  • Stage 2 (): Cascade the three (output of one feeds the next). Total muxes ; delay mux-delays. See figure for the three-layer wiring of one bit column.
Figure — Barrel shifters

Exercise 4.2

Floating-point normalization must shift a mantissa left until its leading bit is 1 — an amount known only at runtime. Explain in one paragraph why a barrel shifter, not a 1-bit iterative shifter, is used, and give the worst-case delay for a 24-bit mantissa.

Recall Solution

Normalization happens every floating-point add/subtract, and the required shift can be anywhere from up to positions depending on cancellation. A 1-bit shifter would take up to 23 clock cycles, stalling the pipeline every time. A barrel shifter does any amount in one pass of combinational logic, so normalization costs a fixed, small delay independent of the shift size. The width is not a power of two, so we use stages (shift by ) on a 24-bit-wide datapath — the shifter matches the actual mantissa width, we do not widen the data to 32. The five stages still cover every amount ; the top () stage is simply left off for amounts below 16. Worst-case delay mux-delays. See Floating Point Normalization.


Level 5 — Mastery

Exercise 5.1

You must support both left and right logical shifts with one device, controlled by a direction bit . Propose a minimal design and state its total mux cost for . Then argue whether it beats building two separate shifters.

Recall Solution

Design idea: a right shift by on an -bit word equals a left shift by followed by masking — but the clean textbook trick is: bit-reverse the input, do a left shift, bit-reverse the output. Reversal is free wiring (no gates). So:

  1. Optionally reverse input (controlled by ) — one row of 2:1 muxes selecting straight vs reversed wiring.
  2. One -stage left barrel shifter: muxes.
  3. Optionally reverse output (controlled by ) — another row of muxes.

Cost for ():

  • Core: muxes.
  • Two reversal rows: muxes.
  • Total muxes.

Versus two separate shifters: two independent barrel shifters cost muxes plus an output selector row of = . The reverse-shift-reverse design (96) is cheaper and reuses the single expensive core. ✅

Exercise 5.2

Prove the general hardware bound: an -bit barrel shifter needs exactly muxes and give the count where .

Recall Solution

Proof. The shift amount ranges to , a -bit number, so we need one stage per bit stages. Each stage produces all output bits, and each output bit is one 2:1 mux muxes per stage. Multiply: . ∎ When is a power of two the ceiling vanishes and this is the familiar . For : , so muxes; delay mux-delays.


Active Recall

Recall Fast fire: stages, muxes, delay, control width for

. Stages ; muxes ; delay mux-delays; control width bits.

Recall Fast fire: which stages are ON for shift-by-45 on a 64-bit word?

, so stages for (sum ).

Connections

  • Multiplexers — every stage is a row of 2:1 muxes.
  • Binary Number Representation — decomposing into powers of two.
  • Shift and Rotate Operations — the logical-shift vs rotate distinction driving several exercises.
  • Logarithmic Delay Structures — why delay grows only as .
  • Floating Point Normalization — the runtime-variable shift of Exercise 4.2.
  • Combinational Circuits — no clock: all answers land in one pass.

Concept Map

master rule

L1 count stages and control width

L2 turn on stages and trace bits

L3 analyse cost order and rotate

L4 build stage equations

L5 combine directions and prove bound

muxes equal n times log n