Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

How to build a 4-bit barrel shifter from logic gates

A barrel shifter shifts or rotates a word by any number of places in a single step, with no clock. This 4-bit one rotates D3 to D0 left by 0 to 3 places, chosen by S1 S0, using two layers of 2-to-1 multiplexers: the first rotates by 1 when S0 is 1, the second by 2 when S1 is 1.

Rotating by any amount from 0 to 3 is the same as optionally rotating by 1 and then optionally by 2, because every shift amount is a sum of powers of two. So an n-bit barrel shifter needs only log2(n) layers of n multiplexers, here 2 layers of 4. That is how a processor executes a shift instruction by any amount in one cycle, instead of shifting one place per clock like a shift register. This version rotates: bits that fall off the left end come back in on the right. A logical shift would feed 0 into those mux inputs instead, and an arithmetic right shift would feed copies of the sign bit. All 64 combinations of data and shift amount are checked before the page is built.

Big circuit: pinch, scroll or tap + to zoom in, then drag to move around.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

What you need

Step by step

  1. Place data inputs D3 to D0 and the shift amount S1 S0.
  2. Layer 1, rotate by 1: for each bit i, a 2-to-1 mux picks D(i) when S0 = 0 and D(i - 1), wrapping D3 around to position 0, when S0 = 1.
  3. Layer 2, rotate by 2: for each bit i, a mux picks layer 1's bit i when S1 = 0 and its bit i - 2 (wrapping) when S1 = 1.
  4. Probe Y3 to Y0. Set D = 0001 and step S1 S0 through 00, 01, 10, 11: the 1 moves to Y0, Y1, Y2, Y3.

Rotate-left table

S1 S0Rotate byY3 Y2 Y1 Y0
000D3 D2 D1 D0
011D2 D1 D0 D3
102D1 D0 D3 D2
113D0 D3 D2 D1

Rotating left by 3 places is the same as rotating right by 1, so this circuit covers every rotation of a 4-bit word. Wider shifters add one layer each time the width doubles: 8 bits need 3 layers of 8 muxes, 32 bits need 5 layers of 32.

You can also read the full theory in the lesson on the barrel shifter. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.

Build more parts from gates

Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.

Open the 4-bit barrel shifter in the lab →

Frequently asked

What is a barrel shifter?

A combinational circuit that shifts or rotates a word by a variable number of places in one step. It is built from layers of multiplexers, each layer shifting by a power of two or not.

How many multiplexers does a barrel shifter need?

n times log2(n) 2-to-1 multiplexers for an n-bit word: 8 for 4 bits (2 layers of 4), 24 for 8 bits, 160 for 32 bits.

What is the difference between rotating and shifting?

A rotate moves the bits that fall off one end back in at the other end. A logical shift fills the vacated places with 0, and an arithmetic right shift fills them with copies of the sign bit to keep a two's-complement number's sign.

Where are barrel shifters used?

In processors, to execute shift and rotate instructions by any amount in a single cycle, and in floating-point units, to align mantissas before adding and to normalize results.