HDLbits
Start Practicing

Circuits/Combinational Logic

4-bit Barrel Shifter (Rotate Left)

medium
barrel-shifterrotatecombinational

Build a 4-bit barrel shifter that rotates in left by amt bits (0-3). Bits shifted out of the top wrap around into the bottom — no bits are lost, unlike a plain logical shift.

Interface

Signal Direction Width Description
in input 4 Value to rotate
amt input 2 Rotate-left amount, 0-3
out output 4 Rotated result

Examples

in amt out
0001 0 0001
0001 1 0010
1000 1 0001
1100 2 0011
1011 3 1101

A common trick: concatenate in with itself to form an 8-bit value, then take the appropriate 4-bit window out of it.