Latches and flip-flops
The memory elements of digital circuits: the SR latch, the gated and D latch, edge-triggered D, JK and T flip-flops, characteristic tables and equations, the race-around problem, setup and hold times.
Why it exists
A combinational circuit forgets: change the inputs and the old output is gone. Anything that remembers, a register, a counter, the program counter, a finite-state controller, needs a circuit that stores a bit. The basic trick is feedback: connect two gates so that each one's output is the other's input. The loop has two stable states, and it stays in whichever one it was put in. From that single idea come the latch, the flip-flop, and every sequential circuit that follows. GATE asks for the behaviour tables, the characteristic equations, the output waveform for a given clock and input, the difference between a latch and a flip-flop, and the cause of the race-around condition.
Prerequisites
You need the gates of logic gates, in particular NAND and NOR.
Precise definitions
Sequential circuit. A circuit whose outputs depend on the present inputs and on stored state. It is synchronous if the state changes only at the ticks of a clock, and asynchronous otherwise. With storage elements it has up to states.
Latch and flip-flop. A latch is level sensitive: while its enable input is active the output follows the data input (it is transparent), and when the enable is inactive it holds. A flip-flop is edge triggered: it looks at its inputs only at a clock edge (rising or falling) and holds the value for the rest of the cycle. In symbols a small triangle on the clock input marks edge triggering, and a bubble before it marks the falling edge.
SR latch (NOR). Two cross-coupled NOR gates: and .
| next | ||
|---|---|---|
| 0 | 0 | holds |
| 0 | 1 | 0 (reset) |
| 1 | 0 | 1 (set) |
| 1 | 1 | forbidden: both outputs 0 |
When both outputs are forced to 0, so and are no longer complements. If both inputs then return to 0 together the latch races: both gates change at the same moment and the final state is unpredictable. That is why is forbidden.
SR latch (NAND). With cross-coupled NAND gates the inputs are active low: sets, resets, holds and is forbidden (both outputs 1).
Gated SR latch. Two extra gates make the inputs effective only while an enable is 1: and .
D latch. A gated SR latch with and , so cannot occur. While the output follows (transparent), while it holds. It can be built from four NAND gates: , , , .
D flip-flop (edge triggered). Two D latches back to back, the first (master) transparent while the clock is low and the second (slave) transparent while the clock is high. At the rising edge the master closes holding the value had just before the edge, and the slave opens and passes it to the output. So takes the value of at the rising edge and holds it for a whole cycle. A direct gate-level version uses six NAND gates (three SR latches). The characteristic equation is (here is the present state and the next state).
JK flip-flop. Behaves like an SR flip-flop with the forbidden input given a useful meaning:
| action | |||
|---|---|---|---|
| 0 | 0 | hold | |
| 0 | 1 | 0 | reset |
| 1 | 0 | 1 | set |
| 1 | 1 | toggle |
Characteristic equation: .
T flip-flop. Toggles when and holds when : . With it divides the clock frequency by 2.
SR flip-flop. with the restriction .
Race-around. A JK latch (level triggered) with toggles over and over for as long as the clock stays high, once per gate delay around the feedback loop, so the output at the end of the pulse depends on the pulse width. Master-slave or edge-triggered construction removes the problem: the output changes only once per clock.
Timing parameters. Setup time : the data must be stable this long before the active clock edge. Hold time : it must stay stable this long after the edge. Clock-to-output delay : the time from the edge until is valid. If setup or hold is violated the flip-flop may enter a metastable state, an intermediate output that takes an unpredictable time to resolve.
Asynchronous inputs. Preset (set) and clear (reset) inputs force the output directly, independent of the clock.
How it works
Three animations show how the same ideas look in time. First the SR latch built from two NOR gates, with every output change one gate delay behind its cause. Watch the forbidden case at the end.
SR latch from two NOR gates: Start
Simulated, not typedDashed lines mark rising clock edges. Green is an output, blue a stored state, black an input, grey the clock. One tick = one gate delay.
Step 1
Two NOR gates feed each other: Q = NOR(R, Q') and Q' = NOR(S, Q). With S = R = 0 the circuit has two stable states; this run starts in the reset state Q = 0, Q' = 1.
Next, a D latch and a D flip-flop driven by the same clock and the same data. The latch follows whenever the clock is high; the flip-flop changes only at the rising edge and takes the value had just before it.
D latch against D flip-flop: Start
Simulated, not typedDashed lines mark rising clock edges. Green is an output, blue a stored state, black an input, grey the clock. One tick = one time unit.
Step 1
Same clock and data feed a D latch (level sensitive) and a D flip-flop (edge triggered, made of two latches). Both start at 0.
With a level-triggered JK latch toggles repeatedly during the high phase (race-around), while an edge-triggered one toggles once per clock.
Race-around with J = K = 1: J = K = 1
Simulated, not typedDashed lines mark rising clock edges. Green is an output, blue a stored state, black an input, grey the clock. One tick = one gate delay.
Step 1
Both JK inputs are 1, so every clock pulse should toggle the output once.
The characteristic equation of the JK flip-flop is just the four rows of its table folded into one expression:
JK flip-flop: the characteristic equation Q+ = JQ' + K'Q
Computed exactly, not typed| J | K | Q | JQ' | K'Q | Q+ by the rule | Q+ = JQ'+K'Q |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | · | · | · | · |
| 0 | 0 | 1 | · | · | · | · |
| 0 | 1 | 0 | · | · | · | · |
| 0 | 1 | 1 | · | · | · | · |
| 1 | 0 | 0 | · | · | · | · |
| 1 | 0 | 1 | · | · | · | · |
| 1 | 1 | 0 | · | · | · | · |
| 1 | 1 | 1 | · | · | · | · |
Step 0
Claim: J = K = 0 holds, J = 0 K = 1 resets, J = 1 K = 0 sets and J = K = 1 toggles. One equation, Q+ = JQ' + K'Q, covers all four cases, where Q is the present state and Q+ the next state.
Worked examples
EX 1Behaviour table of the NOR SR latch, a JK sequence and a T flip-flop
EasyThe program simulates two NOR gates (updating both until nothing changes), then runs a sequence of JK inputs and a T flip-flop with .
#include <stdio.h>
/* NOR-gate SR latch: update both gates until nothing changes */
static void sr_latch(int *q, int *qn, int s, int r) {
for (int i = 0; i < 10; i++) {
int q2 = !(r || *qn);
int qn2 = !(s || *q);
if (q2 == *q && qn2 == *qn) break;
*q = q2;
*qn = qn2;
}
}
static int jk_next(int q, int j, int k) { return (j && !q) || (!k && q); }
int main(void) {
printf("Q S R | Q+ Q'+\n");
for (int q0 = 0; q0 < 2; q0++)
for (int s = 0; s < 2; s++)
for (int r = 0; r < 2; r++) {
int q = q0, qn = !q0;
sr_latch(&q, &qn, s, r);
printf("%d %d %d | %d %d%s\n", q0, s, r, q, qn, s && r ? " forbidden" : "");
}
int q = 0;
int js[] = {1, 1, 0, 0, 1, 1}, ks[] = {0, 1, 0, 1, 0, 1};
printf("JK sequence from Q = 0:");
for (int i = 0; i < 6; i++) {
q = jk_next(q, js[i], ks[i]);
printf(" %d", q);
}
printf("\n");
int t = 0;
printf("T = 1 for eight clocks:");
for (int i = 0; i < 8; i++) {
t = t ^ 1;
printf(" %d", t);
}
printf("\n");
return 0;
}Q S R | Q+ Q'+
0 0 0 | 0 1
0 0 1 | 0 1
0 1 0 | 1 0
0 1 1 | 0 0 forbidden
1 0 0 | 1 0
1 0 1 | 0 1
1 1 0 | 1 0
1 1 1 | 0 0 forbidden
JK sequence from Q = 0: 1 0 0 0 1 0
T = 1 for eight clocks: 1 0 1 0 1 0 1 0Reading the table. With the latch keeps its state (rows 1 and 5). always ends with (rows 3 and 7), ends with (rows 2 and 6). With both outputs are 0, the forbidden state. JK sequence: sets, 11 toggles to 0, 00 holds 0, 01 keeps 0, 10 sets, 11 toggles to 0. T flip-flop: the output toggles on every clock, so it is a square wave of half the clock frequency.
EX 2Latch or flip-flop?
EasyA circuit output follows the data input for the whole time a control line is high. This is a latch (level sensitive). A circuit that samples the data only at the rising edge of a control line and keeps the sampled value for the whole period is a flip-flop (edge triggered). In the animation of the D latch against the D flip-flop, the line "Q latch" copies while the clock is high, including the short pulse of ; "Q flip-flop" ignores that pulse.
EX 3Deriving the JK characteristic equation
MediumFrom the table, for the cases . Group them: and give ; and give . So . The animation checks the equation against the table on all 8 rows.
EX 4Setting and holding with a NAND latch
MediumA NAND SR latch has active-low inputs. With and the output is forced to 1 (set); returning to 1 leaves . Pulsing to 0 resets it to 0. With both inputs 0 both outputs are 1, the forbidden state, and the next state after both rise together is unpredictable. A common mistake is to treat the inputs as active high.
EX 5Output waveform of a D flip-flop
GATE levelGiven a rising-edge D flip-flop with before the first rising edge, falling to 0 in the middle of the high phase, and again just before the second edge, the output is: 1 after the first edge (the value of just before it), still 1 through the high phase (the later change of is ignored), and 1 after the second edge. The output only ever changes at a rising edge, and equals just before that edge. The same inputs applied to a D latch would make the output fall in the middle of the high phase.
EX 6Counting gates and states
GATE level(a) A gated D latch needs 4 NAND gates (no inverter, as the construction above shows). An edge-triggered D flip-flop built directly from NAND gates needs 6; built as a master-slave pair of D latches it needs NAND gates and an inverter for the clock. (b) flip-flops can store different states: 5 flip-flops give 32 states, and a state described by 100 different values needs flip-flops. (c) A T flip-flop with driven by a 1 MHz clock gives a 500 kHz output with a 50% duty cycle.
Cheat box
Cheat box
- Latch = level sensitive (transparent while enabled). Flip-flop = edge triggered.
- NOR SR latch: 00 hold, 01 reset, 10 set, 11 forbidden (both outputs 0). The NAND SR latch is active low: holds, sets, resets, is forbidden.
- D: . JK: . T: . SR: , .
- JK: 00 hold, 01 reset, 10 set, 11 toggle. T = 1 divides the frequency by 2.
- Race-around: level-triggered JK with toggles repeatedly while the clock is high. Cure: master-slave or edge triggering.
- D flip-flop = two D latches (master transparent when clock low, slave when high). Gated D latch = 4 NAND gates; edge-triggered D flip-flop = 6 NAND gates.
- Setup time before the edge, hold time after; = edge to valid output. flip-flops give states.
Traps and GATE patterns
Connections
Builds on
Across subjects
Conversions between the four flip-flop types are the next topic (flip-flop conversion). Rows of D flip-flops are registers and shift registers, T flip-flops chained are counters, and flip-flops plus combinational logic are state machines. In the processor, registers in the datapath and the control state are exactly these elements, and the clock period limit comes from + logic delay + (see the instruction cycle).
PYQs
7 previous year questions from your book. I re-solved each one myself (6 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs
The output of a 2-input multiplexer is connected back to one of its inputs as shown in the figure. Match the functional equivalence of this circuit to one of the following options
- (a)D Flip-flop
- (b)D Latch
- (c)Half-adder
- (d)Demultiplexer
The next state table of a 2-bit saturating up-counter is given below. Q1 Q0 Q1 + Q0 + 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 DIGITAL LOGIC The counter is built as a synchronous sequential circuit using T flip-flops. The expressions for T1 and T0 are
- (a)T1 = Q0Q1 , T0 = Q'0Q'1
- (b)T1 = Q'1Q0 , T0 = Q'1 + Q'0
- (c)T1 = Q1 + Q0 , T0 = Q'1Q'0
- (d)T1 = Q'1Q0 , T0 = Q1 + Q0
Consider a combination of T and D flip-flops connected as shown below. The output of the D flip-flop is connected to the input of the T flip-flop and the output of the T flip-flop is connected to the input of the D flip-flop. Initially, both Q0 and Q1 are set to 1 (before the 1st clock cycle). The outputs
- (a)Q1Q0 after the 3rd cycle are 11 and after the 4th cycle are 00 respectively
- (b)Q1Q0 after the 3rd cycle are 11 and after the 4th cycle are 01 respectively
- (c)Q1Q0 after the 3rd cycle are 00 and after the 4th cycle are 11 respectively
- (d)Q1Q0 after the 3rd cycle are 01 and after the 4th cycle are 01 respectively
A positive edge-triggered D flip-flop is connected to a positive edge-triggered JK flipflop as follows. The Q output of the D flip-flop is connected to both the J and K inputs of the JK flip-flop, while the Q output of the JK flip-flop is connected to the input of the D flip-flop. Initially, the output of the D flip-flop is set to logic one and the output of the JK flip-flop is cleared. Which one of the following is the bit sequence (including the initial state) generated at the Q output of the JK flip-flop when the flip-flops are connected to a free-running common clock? Assume that J = K = 1 is the toggle mode and J = K = 0 is the state-holding mode of the JK flip-flop. Both the flip- flops have non-zero propagation delays.
- (a)0110110..
- (b)0100100...
- (c)01110110...
- (d)011001100...
The above synchronous sequential circuit built using JK flip-flops is initialized with Q2Q1Q0 = 000. The state sequence for this circuit for the next 3 clock cycles is
- (a)001, 010, 011
- (b)111, 110, 101
- (c)100, 110, 111
- (d)100, 011, 001
You are given a free running clock with a duty cycle of 50% and a digital waveform f which changes only at the negative edge of the clock. Which one of the following circuits (using clocked D flip-flops) will delay the phase of f by 180°?
Consider the following circuit involving a positive edge triggered D FF. Consider the following timing diagram. Let represents the logic level on the line in the -th clock period Let represent the complement of . The correct output sequence on over the clock periods through is:
- (a)A0 A1 A1' A3 A4
- (b)A0 A1 A2 A3 A4
- (c)A1 A2 A2' A3 A4
- (d)A1 A2 A3 A4 A5'
Self-check
Question 1
A JK flip-flop is in state and receives , and then , at the next two clock edges. What is after each?
Show answerHide
holds: stays 1. Then toggles: .
Question 2
Write the characteristic equation of a T flip-flop and of an SR flip-flop.
Show answerHide
T: . SR: with the condition .
Question 3
A T flip-flop with is clocked at 40 MHz. What is the output frequency, and how many such flip-flops in a chain give 2.5 MHz?
Show answerHide
Each stage halves the frequency: 20 MHz after one stage. gives , so flip-flops.
Question 4
Why is forbidden in the NOR SR latch?
Show answerHide
Both outputs are forced to 0, so , which contradicts the complement of . When and return to 0 at the same time both gates switch together and the final state depends on tiny delay differences, so it cannot be predicted.
Revision card
One-screen revision
- Latch: level sensitive, transparent. Flip-flop: edge triggered, samples at the edge.
- NOR SR: 00 hold, 01 reset, 10 set, 11 forbidden. NAND SR is active low.
- : D = ; JK = ; T = ; SR = .
- JK: hold, reset, set, toggle. Race-around cured by edge triggering.
- Master-slave D = two latches. Setup before, hold after the edge. flip-flops, states.
References
- ·Mano and Ciletti, Digital Design, 5th ed., Chapter 5 Synchronous Sequential Logic (sequential circuits, latches, flip-flops)
- ·Kohavi and Jha, Switching and Finite Automata Theory, Chapter on memory elements and flip-flops
Verification
The latch and flip-flop behaviours in the animations come from discrete-time simulations (scripts/gen-dl-seq-traces.py) that tests/dl-seq-traces.test.ts re-simulates independently. The NOR SR latch, the NAND latch, the four-NAND gated D latch and the six-NAND edge-triggered D flip-flop are simulated gate by gate in scripts/enum-checks/dl_seq.py against the intended behaviour, and the characteristic and excitation tables are enumerated there. The C program is compiled and run by npm run verify:c. Setup, hold and clock-to-Q behaviour is described qualitatively, since those are physical effects not captured by the ideal models. Textbook chapter titles are from memory; the independent second pass has not been run, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (conversions, signed ranges and flags, IEEE 754 fields and custom formats, Boolean identities and counts, K-map groups, adder carries, BCD correction, flip-flop conversions, counter sequences and T equations, state reduction, Quine-McCluskey rounds and covers, floating-point alignment and rounding); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)