Computer Architecture
How a shift register works
A shift register is a row of flip-flops sharing one clock, each one's output wired to the next one's input. On every clock tick, every bit moves one place along the row. Feed bits in one at a time on a single wire and, after enough ticks, they sit side by side as a whole number: serial in, parallel out.
A chain of flip-flops
Take four D flip-flops. The input, IN, goes to the first one's D. Its Q goes to the second one's D, that Q to the third, and so on. One clock goes to all four.
On each rising edge every flip-flop copies its D at the same instant, so each takes the bit its neighbour held a moment before. Put a 1 in for one tick and it walks along: first stage, second, third, fourth. Read the outputs as a number with the first stage as the 1s and the last as the 8s, and it shows 1, 2, 4, 8.
Live experiment
Walk a bit along the chain
Four flip-flops in a chain: IN feeds the first, each Q feeds the next D, and one clock ticks them all. Put a 1 in and walk it along, 1, 2, 4, 8, then send 5 in a bit at a time and see why the first bit in lands in the 8s.
“Shift it along” is lesson 1 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 before it first.
Shifting is doubling
Moving every bit one place toward the bigger end multiplies a binary number by 2, the way adding a 0 on the end multiplies a decimal number by 10. 0011 (3) becomes 0110 (6). Shifting the other way halves it, dropping the remainder. Processors have shift instructions for exactly this, and in C it's n << 1 and n >> 1.
Serial in, parallel out
To send 5, 0101, down one wire into a four-stage register, send the bits biggest first. The first bit in walks furthest, so after four ticks it's in the 8s place. After four ticks the whole number sits in the register, ready to read at once. That's how serial links work: one wire, many ticks, then the receiver has the whole byte.
The reverse chip, the 74HC165, loads eight inputs at once and shifts them out one at a time: eight buttons read through three pins.
Off the end, or round again
In a plain shift register the last bit simply falls off the end. Wire the last Q back to the first D and the pattern goes round and round, a ring counter. Feed it back inverted and it becomes a Johnson counter, which steps through twice as many states as it has flip-flops. Shift registers also make delays: a bit that goes in comes out the far end exactly n ticks later.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Shift it along” is lesson 1 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Multiplying it by 2. Each stage is the next power of two. Moving every bit up one is doubling.
- Why not “Adding 1”? Adding 1 changes only the bottom of the number. A shift moves every bit to a place worth twice as much.
- Why not “Dividing by 2”? Dividing by 2 is a shift the other way, toward the 1s.
- Why not “Nothing”? Every bit moves to a new place, so the number changes, almost always doubling.
Show the answer
Answer: 15: all four stages load it at once. All four take IN on the same tick, and all four are 1. Nothing shifts.
- Why not “8”? 8 is where a single 1 ends up after walking the chain for four ticks. With every D on IN, nothing walks.
- Why not “1”? 1 would mean only the first stage took the bit. With every D wired to IN, all four take it on the same tick.
- Why not “0”? IN is 1 at the tick, and every stage copies IN, so all four become 1, not 0.
Show the answer
Answer: when the clock rises. Like a camera, it only takes its picture at one moment, the clock's rising edge. Between ticks the input can change as much as it likes, and the output holds the last picture.
- Why not “all the time”? A flip-flop that copied all the time would just be a wire, with no memory.
- Why not “whenever the input changes”? Copying whenever the input changes is what a transparent latch does. A flip-flop waits for the clock's edge.
“Shift it along” is lesson 1 of 6 in Fetch Fjord, Computer Architecture World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 before it first.
Questions people ask
What is a shift register used for?
Turning a stream of bits on one wire into a parallel number, or the reverse; adding outputs or inputs to a microcontroller; delaying a signal by a set number of clock ticks; and doubling or halving binary numbers.
How do I control 8 LEDs with 3 Arduino pins?
Use a 74HC595 shift register. Send the 8 bits on a data pin with a clock pin, using shiftOut(), then pulse a latch pin to copy them to the eight outputs at once. Chain more 595s to add eight outputs each.
What is the difference between SIPO and PISO?
Serial in, parallel out (SIPO), like the 74HC595, takes bits one at a time and presents them side by side. Parallel in, serial out (PISO), like the 74HC165, loads several inputs at once and sends them out one at a time, so a few pins can read many buttons.
Related topics
- How a D flip-flop worksCopy D to Q at the clock's rising edge, then hold it: one bit of memory.
- How a binary counter divides a clock by 2, 4, 8 and 16Flip-flops that toggle, chained: each one halves the clock, together they count.
- Binary numbers explainedPlaces worth 1, 2, 4 and 8: add up the bits that are on, and four wires count to 15.
- Synchronous vs asynchronous countersBits that wait for each other, or bits that change on the same tick.