3.3.11Combinational Circuits

Barrel shifters

2,020 words9 min readdifficulty · medium

A barrel shifter is a combinational circuit that shifts (or rotates) a data word by any number of bit positions in one clock cycle, using only multiplexers — no clocking, no iteration.

The Core Idea

WHY do we need it?

Processors execute instructions like SHL R1, 5 or floating-point normalisation, address scaling, and multiply/divide-by-powers-of-2 — every cycle. A multi-cycle shifter would stall the pipeline. We want a single-cycle, purely combinational device.

WHAT is it, precisely?

HOW is it structured?

The key insight: decompose the shift amount into bits.

S=i=0k1si2i,k=log2nS = \sum_{i=0}^{k-1} s_i \, 2^i, \qquad k = \log_2 n

  • Stage ii is controlled by bit sis_i.
  • If si=1s_i = 1, stage ii shifts its input by 2i2^i; if si=0s_i = 0, it passes data through unchanged.
  • Cascade all kk stages → total shift =si2i=S= \sum s_i 2^i = S. ✅
Figure — Barrel shifters

Deriving the multiplexer equation from scratch

Consider stage ii, which shifts left by 2i2^i when si=1s_i=1 (fill with 0s). Let the stage input be A=an1a0A = a_{n-1}\dots a_0 and output B=bn1b0B = b_{n-1}\dots b_0.

What does "left shift by 2i2^i" mean? Output bit jj takes input bit j2ij - 2^i: bj=aj2i(and bj=0 if j2i<0)b_j = a_{j-2^i}\quad(\text{and } b_j = 0 \text{ if } j - 2^i < 0)

When si=0s_i = 0, pass through: bj=ajb_j = a_j.

Combine both cases with a 2:1 mux selected by sis_i:

For a rotate, the bits that fall off the end wrap around instead of being replaced by 0: bj=siaj+sia(j2i)modnb_j = \overline{s_i}\,a_j + s_i\,a_{(j-2^i)\bmod n}

Counting the hardware (the 80/20 that matters)

  • Number of stages ===log2n=== ==\log_2 n==.
  • Each stage has nn muxes (one per output bit).
  • Total muxes ===nlog2n=== ==n\log_2 n==.
  • Propagation delay ===log2n==×(delay of one 2:1 mux)= ==\log_2 n== \times (\text{delay of one 2:1 mux}) — grows only logarithmically, which is why it's fast.

Worked Example 1 — Shift left by 13 on an 8… wait, 16-bit word

Take n=16n = 16, so k=log216=4k = \log_2 16 = 4 stages (shift by 1, 2, 4, 8). Shift amount S=13S = 13.

Step 1 — write SS in binary. 13=1101213 = 1101_2, so s3s2s1s0=1101s_3 s_2 s_1 s_0 = 1101. Why? The control bits ARE the binary digits of the shift amount.

Step 2 — turn on the matching stages.

  • s0=1s_0 = 1 → shift by 20=12^0 = 1
  • s1=0s_1 = 0 → pass through
  • s2=1s_2 = 1 → shift by 22=42^2 = 4
  • s3=1s_3 = 1 → shift by 23=82^3 = 8

Step 3 — total shift =1+4+8=13= 1 + 4 + 8 = 13. ✅ Why this step? Confirms the sum-of-powers decomposition equals the requested shift.

Trace with input A = 0000 0000 0000 0001 (value 1):

  • After stage 0 (shift 1): ...0010
  • Stage 1 (pass): ...0010
  • Stage 2 (shift 4): ...0010 0000
  • Stage 3 (shift 8): 0010 0000 0000 0000 = 213=81922^{13} = 8192. ✅ (1×2131 \times 2^{13}.)

Worked Example 2 — Rotate right by 3 on a 4-bit word

n=4n=4, k=2k=2 stages. Rotate-right amount S=3=112S=3 = 11_2, so s1s0=11s_1 s_0 = 11: rotate by 1 then by 2.

Input A=1000A = 1000 (bit 3 set).

  • Rotate right by 1 → 0100 Why? bj=a(j+1)mod4b_j = a_{(j+1)\bmod 4}; bit that leaves bottom wraps to top.
  • Rotate right by 2 → 0001 Why? Applying the mod-wrap again by 21=22^1=2.

Total rotate-right by 3 of 1000 = 0001. Check directly: rotating 1000 right 3 = 0001. ✅

Common Mistakes

Active Recall

Recall Q: Why only

log2n\log_2 n stages? Because the shift amount (0…n1n-1) is a log2n\log_2 n-bit binary number; each bit selects a stage that shifts by that power of two, and their sum reproduces any amount.

Recall Q: Write the per-bit mux equation for a left-shift stage

ii. bj=siaj+siaj2ib_j = \overline{s_i}\,a_j + s_i\,a_{j-2^i} (with a<0=0a_{<0}=0).

Recall Q: Mux count and delay for

n=32n=32? Muxes =nlog2n=32×5=160= n\log_2 n = 32\times5 = 160; delay =5= 5 mux delays.

Recall Feynman: explain to a 12-year-old

Imagine you want to move a row of books along a shelf by any number of spaces, but you're only allowed to push them in jumps of 1, 2, 4, or 8. To move them 13 spaces, you do jumps of 8, then 4, then 1 (that's 13!). Each jump is a separate "helper" you can switch ON or OFF. With just 4 helpers you can slide the books by any amount from 0 to 15 in one go, instead of nudging them one space at a time. That's a barrel shifter — a stack of ON/OFF sliders, one for each power of two.

Connections

  • Multiplexers — the fundamental building block (2:1 mux per bit).
  • Binary Number Representation — decomposition of shift amount into powers of two.
  • Combinational Circuits — no clock, purely input-driven.
  • Shift and Rotate Operations — the ALU operations this implements.
  • Logarithmic Delay Structures — same logn\log n idea as carry-lookahead / prefix networks.
  • Floating Point Normalization — a major real user of fast shifters.
What is a barrel shifter?
A combinational circuit that shifts/rotates a word by any amount in one cycle using cascaded mux stages, each shifting by a power of two.
How many stages does an n-bit barrel shifter have?
log2n\log_2 n stages.
How many 2:1 muxes total?
nlog2nn\log_2 n.
What is the propagation delay in terms of mux delays?
log2n\log_2 n mux delays (logarithmic).
Per-bit equation of a left-shift stage i?
bj=siaj+siaj2ib_j=\overline{s_i}a_j+s_i a_{j-2^i}.
How wide is the shift-amount control input?
log2n\lceil\log_2 n\rceil bits.
Which stages are active to shift by 13?
Stages for 8, 4, 1 (since 13=1101213=1101_2).
Difference between shift and rotate stage inputs?
Shift fills vacated bits with 0; rotate wraps them around ((j2i)modn(j-2^i)\bmod n).
Why powers of two per stage instead of 1 each?
Any amount is a sum of distinct powers of two, so log2n\log_2 n layers cover all amounts.
Rotate-right-by-3 of 1000 (4-bit)?
0001.

Concept Map

too slow multi-cycle

solved by

is a

decomposes

binary sum of powers of 2

stage i shifts by 2^i

built from

equation

wrap variant

count

delay

Naive 1-bit shifter

Need single-cycle shift

Barrel shifter

Combinational circuit

Shift amount S

log2 n stages

Stage controlled by bit si

Row of 2-to-1 muxes

bj = si' aj + si a j-2^i

Rotate mode

Total n log2 n muxes

Delay grows as log2 n

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, barrel shifter ka basic idea bahut simple hai. Maan lo tumhe ek number ko 13 positions left shift karna hai. Agar tum ek baar mein sirf 1 bit shift kar sakte ho, to 13 clock cycles lag jaayenge — bahut slow! Iska smart solution: shift amount ko binary mein todo. 13=1101213 = 1101_2, matlab 8+4+18 + 4 + 1. Ab agar tumhare paas alag-alag "stages" hain jo fixed jumps de sakte hain — koi stage 1 ka jump, koi 2 ka, koi 4 ka, koi 8 ka — to bas jo stages chahiye unhe ON kar do, baaki OFF. Sab ek hi cycle mein ho jaata hai, no clock, pura combinational.

Har stage sirf 2-to-1 multiplexers ki ek row hoti hai. Har mux decide karta hai: "main apna original bit pass karoon ya shifted bit?" Control bit sis_i jab 1 hoga to shifted version aayega, jab 0 hoga to seedha pass ho jaayega. Equation yaad rakho: bj=siaj+siaj2ib_j = \overline{s_i}a_j + s_i a_{j-2^i} — yahi to ek mux hai! Isliye humein koi naya formula ratna nahi padta, mux se hi derive ho jaata hai.

Sabse important baat jo exam mein poochte hain: nn-bit shifter ke liye stages =log2n= \log_2 n, total muxes =nlog2n= n\log_2 n, aur delay bhi log2n\log_2 n hi. Yeh log wala jaadu isliye kaam karta hai kyunki koi bhi shift amount powers of two ka sum hota hai — jaise binary representation. Isliye 32-bit ke liye sirf 5 stages, 160 muxes.

Ek galti se bachna: shift aur rotate mein farak hai. Logical shift mein jo bits bahar jaate hain unki jagah 0 aata hai; rotate mein woh bits wapas doosre end pe aa jaate hain (wrap around, (j2i)modn(j-2^i)\bmod n). Structure same, bas mux ka doosra input alag. Yeh chhoti si baat exam mein marks dilaati hai!

Go deeper — visual, from zero

Test yourself — Combinational Circuits

Connections