Barrel shifter

Shift by a variable amount in a single cycle.

When to use it

When you need to shift or rotate by a runtime amount in one clock, a normalizer, a float aligner, a bit extractor, you can't ripple one place at a time. A barrel shifter does the whole shift combinationally.

How it works

Build it from log2(W) stages, each controlled by one bit of the shift amount: stage i either passes its input through or shifts it by 2^i. Chain them and any shift 0 to W-1 falls out in a single (deep but fast) cone of logic. Writing x >> amt in Verilog synthesizes to exactly this, the recipe just shows the structure.

Gotchas

Try it

Runs on our simulator, edit the code and press Run to see the waveform.

Open & fork in the playground → Ship the verified version: lfpga add lfpga_barrel_shifter

The runnable example above is a teaching version. The libfpga library ships a hardened, parameterised, CI-tested module (lfpga_barrel_shifter) for production use.

Next recipe: LFSR (pseudo-random)