Circuit Bytes · Episode 53 · Electronics · Logic gates
"Flip-flops: one bit of memory"
Ordinary gates forget their answer as soon as their inputs change. Wire two NOT gates in a ring and the loop holds a bit by itself. Add set and reset inputs and you have a latch; make it update only on a clock tick and you have a flip-flop, which stores one bit.
Gates forget
Every gate in this series so far (AND, OR, NOT, NAND, the half adder) works out its output from its inputs right now. Feed an AND gate 1 and 1 and it outputs 1; change one input to 0 and the output drops to 0 at once. Nothing of the old answer is kept. To remember, a circuit needs something more.
The trick: a loop
Take two NOT gates and wire them in a ring: the first gate's output goes into the second gate's input, and the second gate's output goes back into the first gate's input.
Suppose the first gate outputs 1. The second gate flips it, so it outputs 0. That 0 travels back to the first gate's input, which keeps the first gate outputting 1. Each gate holds the other in place, and the bit stays put for as long as the circuit has power. A circuit like this, with two stable states, is called bistable.
Set and reset: the latch
A ring on its own holds a bit, but you can't change it. So the loop gets two extra inputs:
- Set forces the stored bit to
1. - Reset forces the stored bit to
0.
When you let go of both, the loop keeps whatever was last forced into it. That circuit is a latch, often called an SR (set-reset) latch. In practice it is built from two NOR gates (or two NAND gates) wired in the same cross-coupled loop, so the extra inputs have somewhere to go. Pressing set and reset at the same time is not allowed for an SR latch, because it asks for 1 and 0 at once.
Flip-flops and the clock
A flip-flop is a latch that only updates when a clock tells it to. The clock is a signal that ticks steadily between low and high. The flip-flop looks at its input and copies it into storage only at the tick (usually the moment the clock rises), and ignores changes in between. That keeps every part of a chip changing in step. (People use the words differently, but the common modern usage is: a latch follows its inputs whenever it is enabled; a flip-flop changes only on a clock edge.)
Each flip-flop stores one bit. Line up eight and you have a byte; line up 64 and you have one of a processor's registers.
Try it yourself
Trace an SR latch on paper. Start with the stored bit at 0. Then do these steps in order and write down the stored bit after each one: press set; let go; press set again; let go; press reset; let go; let go again (nothing pressed). Which steps actually changed the stored bit, and which ones left it alone?
Good to know
- A circuit with feedback (its output looping back to its input) can hold a value; a circuit without feedback cannot.
- Two NOT gates in a ring have two stable states, so they store one bit.
- An SR latch is usually two cross-coupled NOR gates (or NAND gates); set makes it
1, reset makes it0. - A flip-flop updates only on a clock edge; a latch is level-sensitive.
- This memory is volatile: it is lost when the power goes off, and on power-up the loop settles into either state.
- Processor registers and SRAM cache cells (two cross-coupled CMOS inverters plus two access transistors, six transistors in all) use this loop. Main memory (DRAM) stores bits as charge on tiny capacitors instead.
Answer to the comment question
In the loop, suppose the first NOT gate outputs 0. What does the second one output? 1. The second gate's input is the first gate's output, 0, and a NOT gate flips it, so it outputs 1. That 1 then goes back into the first gate, which flips it to 0, so this state holds too. It is the ring's other stable state: the same loop can sit steadily either way round, which is exactly why it can store one bit.
Test your knowledge
Quick quiz: "Flip-flops: one bit of memory"
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- All About Circuits textbook, Experiments volume, chapter 7, "NOR Gate S-R Latch": two cross-coupled gates with set and reset inputs; the latch holds its state when both are released
- All About Circuits textbook, Vol. IV Digital, chapter 10 "Multivibrators," "Edge-triggered Latches: Flip-Flops": a flip-flop changes state only on a clock edge
- Nand2Tetris (Nisan and Schocken, The Elements of Computing Systems), "Project 3: Memory": clocked flip-flops as one-bit storage, built into registers and RAM
- Wikipedia, "Flip-flop (electronics)"): latch vs flip-flop terminology, level-sensitive vs edge-triggered (cross-check only)
Video transcript
How does a circuit remember anything?
Every gate so far forgets: change its inputs and the old answer is gone.
The trick is a loop. Wire two NOT gates in a ring, each one's output feeding the other's input.
If the first outputs one, the second outputs zero, which keeps the first at one. The bit stays put.
Give the loop two extra inputs, set and reset. Set forces a one, reset forces a zero, and when you let go, it remembers. That's a latch.
A flip-flop is a latch that only updates when a clock tells it to. Each one stores one bit.
In the loop, suppose the first NOT gate outputs zero. What does the second one output? Comment your answer below!
That's your byte.
More from Circuit Bytes
- EP51c"NAND: the universal gate"
- EP52c"Adding numbers with gates"
- EP54c"Clock signals"
- EP55c"What a microcontroller is"