Latches and Flip-Flops

A computer must remember things — a loop counter, a return address, a single bit telling it whether a door is locked. Logic Gates alone cannot do this. A network of AND, OR, and NOT gates with no feedback is purely combinational. This means its output is a fixed function of its current inputs. It vanishes the instant the inputs change. Memory requires a circuit whose output can persist. It must persist even after the inputs that produced it are gone. That circuit is built by feeding a gate's output back into its own input. The resulting family of devices — SR latches, D latches, and edge-triggered D flip-flops — is the entire foundation of digital storage. This matters because every register, every counter, every pipeline stage, and every byte of cache in a real processor is, underneath, thousands of copies of the same one-bit circuit described here. The register file in a RISC-V or x86 core is a grid of flip-flops. The program counter is a flip-flop. The pipeline latches that separate fetch from decode from execute are literally latches in the technical sense of this article. Understanding how a single bit of state is held stable is essential. So is knowing precisely when it is allowed to change. This is the prerequisite for understanding Registers and Register Files, Finite State Machines, and ultimately the entire notion of a clocked, synchronous machine. The central engineering problem this article solves is this: how do you build a circuit that (1) can be set to 0 or 1, (2) holds that value indefinitely without external input, and (3) changes state only at a moment you control, not the many chaotic moments Voltage happens to wiggle? The answer unfolds in three stages of refinement. The SR latch shows that cross-coupled gates can hold a bit. But it exposes a forbidden input combination. It is also level-sensitive (transparent) at unpredictable times. The D latch fixes the forbidden-state problem. It adds a single "enable" input. But it remains transparent — dangerous when many latches are chained together. The edge-triggered D flip-flop solves transparency by sampling the input only at the instant a clock signal transitions. This gives the clean, composable building block. All of Clocks and Timing and synchronous digital design depends on it. By the end of this article you will be able to derive the truth tables and characteristic equations of all three devices. You will trace a bit as it propagates through master-slave construction. You will explain precisely why the SR latch's forbidden state is dangerous. You will also see how these circuits scale up into the registers that hold every value a CPU manipulates.

Key takeaways
  • A latch or flip-flop stores one bit by feeding a gate's output back into its own input, creating stable feedback states.
  • The SR latch (Set/Reset) has a forbidden input combination (S=R=1) and is level-sensitive — it responds to inputs whenever enabled.
  • The D latch removes the forbidden state by deriving S and R from a single data line D, but is still transparent while its enable is high.
  • The edge-triggered D flip-flop, typically built as two D latches in master-slave configuration, samples input only at a clock edge — no transparency window.
  • Complexity is O(1) time and space per bit; correctness hinges on feedback-loop stability, not iteration, and on satisfying setup/hold timing constraints.
  • Flip-flops are the atomic unit behind Registers and Register Files, pipeline stage boundaries, and every Finite State Machine's state memory.
The core insight

Feeding a gate's output back into its own input turns a memoryless circuit into a stable one-bit memory; the flip-flop refines this by restricting when that memory can change to a single clock edge.

The idea

A latch is a bistable circuit: a circuit with exactly two stable output configurations, each of which persists forever in the absence of a new input, because the output is wired back as an input to itself. This feedback is the entire trick. Two NOR gates, or two NAND gates, cross-coupled so that each gate's output feeds the other gate's input, have two self-consistent equilibrium states (Q=0,Q̄=1) and (Q=1,Q̄=0). Which one the circuit sits in depends on its history — that history-dependence is memory. A latch is called level-sensitive (or transparent) because whenever its enable/clock input is asserted (at the "1" level, say), its output tracks its data input immediately, gate-delay only; it is not waiting for any special moment. A flip-flop is a memory element whose output can change only at a controlled instant, usually the rising or falling edge of a clock signal, rather than throughout an entire clock level. This is called edge-triggering. The distinction between level-sensitive latches and edge-triggered flip-flops is the single most important vocabulary distinction in sequential logic: a latch is "open" (transparent) for a whole half-cycle, during which glitches or races can propagate straight through it, while a flip-flop is open for only an instant, decoupling its output change from any lingering transients on its input. The three circuits in this article's scope form a strict progression. The SR latch (Set-Reset) is the primitive cross-coupled circuit, simplest to build and reason about but flawed: one input combination is "forbidden" because it makes both outputs equal and leaves the next state ambiguous. The D latch (Data/Delay latch) wraps an SR latch (or is built directly from a multiplexer-like gate structure) so that only one data line D and one enable line C are exposed; the forbidden state is architecturally impossible because S and R can never both be asserted. The edge-triggered D flip-flop, most commonly realized as a master-slave pair of D latches driven by complementary clock phases, converts the D latch's whole-half-cycle transparency into a single-instant sample-and-hold, producing the element every synchronous digital system is built from.

How it works

The SR latch. Build it from two cross-coupled NOR gates. Gate 1 computes Q = NOR(R, Q̄); gate 2 computes Q̄ = NOR(S, Q). The truth table, checked case by case:

SRBehaviorQ(next)
00HoldQ (unchanged)
01Reset0
10Set1
11Forbidden0 and 1 both attempt to hold (Q = Q̄ = 0), invalid/racy on release

When S=1, R=0, gate 2 forces Q̄=0 regardless of its other input, and gate 1 then sees R=0, Q̄=0, giving Q = NOR(0,0) = 1 — consistent. Symmetric reasoning gives the Reset row. When S=R=0, whichever value Q currently holds is self-sustaining: if Q=1, Q̄=NOR(0,1)=0, and Q=NOR(0,0)=1, consistent; if Q=0, Q̄=NOR(0,0)=1, and Q=NOR(0,1)=0, also consistent — both states are stable, which is exactly the bistability that gives us memory. When S=R=1, both outputs are forced to 0, violating the invariant Q=NOT(Q̄) that defines a well-formed latch state; the moment S and R are released, whichever gate reacts faster wins unpredictably — a race condition. This is why S=R=1 is forbidden in normal operation.

The D latch. Take an SR latch and add a transparent-gating layer with a single enable input C (clock/control): feed S = D AND C, and R = (NOT D) AND C. When C=0, S=R=0 regardless of D, so the latch holds its last value. When C=1, S and R are complements of each other, so S=R=1 is structurally impossible — the forbidden state is eliminated by construction. While C=1, Q follows D immediately (transparency).

The edge-triggered D flip-flop (master-slave). Chain two D latches: a master latch gated by clock CLK, and a slave latch gated by the inverted clock NOT CLK. While CLK=0, the master is transparent (tracks D) and the slave is opaque (holds its old value, isolating the output). While CLK=1, the master becomes opaque (freezing whatever value D held at the instant CLK rose) and the slave becomes transparent, copying that frozen value to the output Q. Because the master closes at the exact instant the slave opens, the only moment the external input D affects the external output Q is the rising edge of CLK — everywhere else, one of the two latches is opaque and blocks propagation. This is what "edge-triggered" means operationally: sensitivity to D is compressed into an instant, even though internally the circuit is built from two ordinary level-sensitive latches. ```pseudocode // SR latch — state held in Q, Q_bar; feedback defines next state function SR_LATCH_STEP(S, R, Q, Q_bar): if S == 1 and R == 1: error("forbidden input combination") // both outputs would go low Q_next = NOR(R, Q_bar) Q_bar_next = NOR(S, Q) return (Q_next, Q_bar_next) // D latch — level sensitive, transparent while C == 1 function D_LATCH_STEP(D, C, Q): if C == 1: Q = D // transparent: output follows input instantly // else: C == 0, Q holds its previous value (no assignment) return Q // Edge-triggered D flip-flop, master-slave construction // Internally two D latches; externally samples D only at CLK rising edge function DFF_STEP(D, CLK, master_Q, slave_Q): master_C = NOT(CLK) slave_C = CLK master_Q = D_LATCH_STEP(D, master_C, master_Q) // opaque while CLK=1 slave_Q = D_LATCH_STEP(master_Q, slave_C, slave_Q) // opaque while CLK=0 Q = slave_Q return (Q, master_Q, slave_Q) ```

Figure
Master–slave D flip-flop: two D latches gated by complementary clock phases
D MASTER D latch C = NOT(CLK) Qm SLAVE D latch C = CLK Q CLK NOT CLK CLK While CLK = 0: master transparent, slave opaque (holds Q). While CLK rises to 1: master freezes D, slave opens and copies it to Q — the edge.

Complexity & correctness

Time complexity. Every operation on a latch or flip-flop — set, reset, hold, or clocked update — takes O(1) time: a fixed, small number of gate propagation delays independent of any input size, because there is no notion of "problem size" for a single bit. The relevant real-world timing quantities are not asymptotic but physical: propagation delay (t_pd, gate-to-output delay), setup time (t_su, how long D must be stable before the clock edge), and hold time (t_h, how long D must remain stable after the edge). A flip-flop is used correctly only if D is stable throughout the window [edge − t_su, edge + t_h]; this is a timing constraint, not a complexity bound, but it plays the same role in correctness that loop invariants play in iterative algorithms.

Space complexity. Storing one bit costs O(1) space — a constant number of gates (2 cross-coupled gates for an SR latch, roughly 4–6 gates for a D latch, and about double that, ~8–12 gates, for a master-slave D flip-flop). An n-bit register built from n flip-flops costs Θ(n) gates and Θ(n) area, which is the expected linear scaling and is why Registers and Register Files are described as arrays of this same one-bit primitive.

Correctness argument — SR latch. The claim is that the cross-coupled NOR pair has exactly two stable fixed points under S=R=0, and that S=1,R=0 (respectively S=0,R=1) drives the circuit to the Q=1 (respectively Q=0) fixed point regardless of starting state. Proof sketch: treat (Q, Q̄) as a state and check self-consistency of the defining equations Q = NOR(R,Q̄), Q̄ = NOR(S,Q) under each input. For S=0,R=0: Q=1,Q̄=0 satisfies Q=NOR(0,0)=1 ✓ and Q̄=NOR(0,1)=0 ✓; likewise Q=0,Q̄=1 is also self-consistent — two fixed points confirmed, i.e., bistability, the defining property of memory. For S=1,R=0: Q̄ = NOR(0,Q) is forced toward 0 once we substitute the target Q=1 (Q̄=NOR(0,1)=0), and then Q=NOR(0,0)=1, a self-reinforcing loop that converges to Q=1 from either starting state — this is the "Set" behavior working correctly. The forbidden case S=R=1 breaks the invariant Q=NOT(Q̄) (both go to 0 simultaneously), so the two-fixed-point argument does not apply, and the state reached when S and R are simultaneously released depends on gate-delay mismatches — an unavoidable race, which is exactly why the input combination is disallowed by contract rather than fixed by circuitry.

Correctness argument — D latch. Because S = D∧C and R = D̄∧C are complementary whenever C=1 (one of D, D̄ is always 0, so S and R cannot both be 1), the forbidden SR state is provably unreachable — not merely avoided by careful use, but structurally excluded. This is a stronger correctness guarantee: a proof by construction rather than a usage discipline. When C=0, S=R=0 regardless of D, which by the SR latch's own correctness argument means Q holds its last value — giving the desired "opaque when disabled" behavior.

Correctness argument — edge-triggered flip-flop. The master-slave design's correctness rests on an invariant: at every instant, at most one of the two latches is transparent. Master is transparent exactly when CLK=0; slave is transparent exactly when CLK=1; these conditions are mutually exclusive (ignoring the zero-width instant of an idealized transition), so there is never a moment when D has a direct combinational path all the way to Q. Consequently Q can change only during the CLK transition itself, when control passes from master to slave — this is the formal justification for calling the device "edge-triggered" despite being built from two purely level-sensitive latches. The tradeoff is a doubling of gate count and an added full latch-delay of propagation time (data must pass through both master and slave), compared to a single (unsafe, transparent) D latch — the classic time/robustness tradeoff of adding a second stage to gain edge sensitivity.

Worked example

Trace a master-slave D flip-flop through a sequence of (D, CLK) inputs, assuming instantaneous gate response for clarity and starting from Q=0, master_Q=0.

StepDCLKMaster latch (C=NOT CLK)Master_QSlave latch (C=CLK)Q (output)Note
000transparent (C=1)tracks D=0 → 0opaque (C=0)0initial state
110transparenttracks D=1 → 1opaque0D changed; master follows, Q unaffected
210→1 (rising edge)closes at edgelocked at 1opens at edge, copies master1the only moment Q changes
301opaque (C=0)stays 1 (ignores new D=0)transparent, mirrors master=11D dropped mid-cycle, correctly ignored
401→0 (falling edge)openstracks D=0 → 0closes, freezes at last value 11slave now opaque; Q holds
510transparenttracks D=1 → 1opaque1master free to move, Q still frozen
610→1 (rising edge)closeslocked at 1opens, copies master1Q re-confirmed 1

The critical row is step 3: D changes from 1 to 0 while CLK is already high (mid-level, not at an edge). Because the slave is transparent during CLK=1 and mirrors the master, one might worry Q would glitch — but the master is opaque during CLK=1 (it closed at the edge in step 2), so the new D=0 has no path to the master's output, hence none to Q. Q remains a stable 1 throughout the entire CLK=1 phase, changing only at the next rising edge (step 6), which correctly demonstrates edge-triggered behavior: input changes between edges are invisible to the output, exactly the property a plain, single-stage D latch would fail to provide (a D latch would have let Q glitch down to 0 in step 3, because D latches stay transparent for the whole enable level).

Common pitfalls & traps

PitfallWhy it happensConsequence
Asserting S=R=1 on an SR latchTreating S and R as independent controls without the mutual-exclusion disciplineOutput enters an undefined/racing state when both are released
Confusing "latch" and "flip-flop"Both store one bit and both are called "memory elements" informallyUsing a transparent latch where edge-triggering is required causes race conditions across chained stages
Ignoring setup/hold timeTreating the circuit as instantaneous/logical rather than physicalD changing too close to the clock edge causes metastability — an unresolved, unpredictable output voltage
Assuming D latches are "safe" versions of SR latchesThe forbidden-state fix is real, but transparency is still a hazardCascading two transparent D latches directly (no clock-phase separation) lets data race straight through in one cycle
Forgetting the flip-flop's inherent extra delayMaster-slave design adds a second latch stageUnderestimating a synchronous circuit's clock period, causing timing violations
Treating "rising-edge" and "falling-edge" triggered flip-flops as interchangeableBoth exist as standard parts, differing only in which clock phase gates the masterMixing them in one clock domain without accounting for phase causes off-by-half-cycle bugs

Pioneers & history

The cross-coupled bistable circuit predates the transistor: it was first realized with vacuum tubes as the Eccles-Jordan trigger circuit, the ancestor of every flip-flop built since, including the ones inside every modern CPU register.

Origins · 1 minWilliam Eccles and how this was discovered
WE
William Eccles
1875–1966 · British physicist and radio engineer

Eccles, working with F. W. Jordan, patented the trigger circuit in 1918 — a pair of cross-coupled vacuum-tube amplifiers that could latch into either of two stable states. Their motivation was radio and telegraphy pulse-counting circuits, not computing as we now understand it, but the topology they described — feedback between two amplifying stages producing bistability — is exactly the topology of the modern SR latch, translated later into relays, then transistors, then CMOS gates.

History
1918
— Eccles and Jordan patent the trigger (flip-flop) circuit using vacuum tubes.
1937
— Claude Shannon's master's thesis formalizes switching circuits with Boolean algebra, giving latches a rigorous logical foundation.
1946
— The ENIAC and successor machines use flip-flop circuits (vacuum-tube based) as working registers and counters.
1960
s — Transistor-transistor logic (TTL) integrated circuits package D latches and JK/D flip-flops as standard off-the-shelf parts.
1970
s–present — CMOS master-slave and edge-triggered flip-flop designs become the universal storage primitive inside VLSI processors and memory controllers.

Where this matters

Every synchronous digital system — from a simple 8-bit microcontroller to a many-core server processor — is built as combinational logic interspersed with banks of edge-triggered D flip-flops. The program counter, register file entries, and pipeline latches separating fetch/decode/execute/memory/writeback stages in a pipelined CPU are all literal instances of the D flip-flop described here, chosen specifically for its edge-triggered (not level-sensitive) behavior so that pipeline stages do not race each other within a single clock cycle. SRAM cells used in CPU caches and register files are themselves small cross-coupled bistable circuits, direct descendants of the SR latch topology, optimized for transistor count rather than input-count flexibility. FPGA fabric provides a configurable D flip-flop at every logic cell, letting designers build arbitrary Finite State Machines whose entire notion of "current state" is stored across that layer of flip-flops. Serial interfaces and clock-domain-crossing logic use chains of flip-flops as synchronizers to manage the metastability risk discussed above. In short, this article's three circuits are the atomic storage unit beneath Registers and Register Files, the state memory beneath every hardware Finite State Machine, and the reason Clocks and Timing exists as a discipline: someone must guarantee that every flip-flop's setup and hold windows are respected on every single edge, every cycle, across an entire chip.

Ask the tutor: How does a master-slave D flip-flop's two-latch structure change if the design needs both a synchronous reset and an asynchronous clear input?

What is the difference between a latch and a flip-flop, in one sentence?

A latch is level-sensitive — transparent, tracking its input, for as long as its enable signal is asserted — while a flip-flop is edge-triggered, sampling its input only at the instant a clock signal transitions, and holding that sampled value otherwise. This distinction matters because level-sensitive devices allow input changes to propagate through combinational chains within a single enabled period, which is dangerous when latches are cascaded, whereas edge-triggered flip-flops compress that window to zero (ideally), making them safely composable into pipelines, registers, and finite state machines without unintended race conditions between stages.

Why do master-slave flip-flops need two latches instead of one?

A single D latch is inherently transparent for the entire duration its enable is asserted, meaning any change in D during that window immediately shows up at Q — useful for some designs but dangerous when chaining stages, since data can race all the way through multiple stages within one clock level. Using two latches, gated by opposite clock phases, guarantees that whenever one is open (transparent) the other is closed (opaque), so there is never a direct combinational path from the external D input to the external Q output except at the instant the clock transitions. This converts level-sensitivity into edge-sensitivity using only ordinary latches as building blocks.

Can you build an SR latch from NAND gates instead of NOR gates?

Yes — cross-coupled NAND gates form a valid SR latch, but the roles of the inputs and the forbidden state invert. In the NAND version (often called an SR-bar or active-low latch), inputs are typically labeled S̄ and R̄, the hold state occurs when both inputs are 1, and the forbidden combination becomes S̄=R̄=0 (both asserted low) rather than both asserted high. The underlying bistability argument is structurally identical; only the polarity convention changes, since NAND and NOR are dual operations under De Morgan's laws.

Why does setup and hold time exist at all — why can't a flip-flop just sample instantly?

Real gates have nonzero propagation delay, and the internal feedback loop of a latch needs a finite amount of time to settle into one of its two stable states after an input change. If the data input changes too close to the clock edge, the feedback loop can be caught mid-transition, without enough time to resolve fully to either 0 or 1 before the output is read — this is metastability. Setup time (before the edge) and hold time (after the edge) define the window during which the input must be stable, giving the internal circuit enough time to settle deterministically; violating this window doesn't cause a wrong logical answer so much as an undefined, sometimes oscillating, physical one.

How does this relate to SRAM and cache memory in real chips?

An SRAM (static RAM) cell is, at its core, a cross-coupled inverter pair — the same bistable feedback topology as the SR latch — augmented with access transistors that connect it to bit lines for reading and writing, rather than dedicated S and R inputs. This is why SRAM is called "static": like a latch, it holds its value indefinitely as long as power is supplied, with no need for periodic refresh (unlike DRAM, which uses capacitive charge storage and does require refresh). CPU caches and register files are essentially large, densely packed, address-decoded arrays of this same bistable memory primitive, optimized for area and speed rather than for having flexible logical inputs like a general-purpose flip-flop.

Why do digital designers almost always use flip-flops rather than latches for registers in synchronous circuits?

Edge-triggering makes reasoning about timing tractable: because each flip-flop changes state only at a clock edge, an entire pipeline's behavior over one clock cycle can be analyzed as "read old state, compute in combinational logic, write new state at the next edge," with no risk of a signal racing through several stages within one cycle. Level-sensitive latches remain transparent for a whole half-cycle, so a chain of latches sharing the same enable can let data ripple through multiple stages unintentionally — a hazard called a race-through. Some high-performance and low-power designs deliberately use latches (in "latch-based" or "pulse-latch" pipelines) for their smaller size and lower power, but this requires much more careful timing verification, which is why flip-flops remain the default choice for general synchronous design.

What does the characteristic equation Q(next) = D mean for a D flip-flop?

The characteristic equation summarizes the flip-flop's next-state behavior as a Boolean function of its current inputs: for an ideal D flip-flop, Q(next) simply equals whatever value D held at the most recent active clock edge, independent of the flip-flop's previous state. This is different from the SR latch's characteristic equation, Q(next) = S + R̄·Q (valid when S·R = 0), which shows an explicit dependence on the previous state Q when neither S nor R is asserted — the "hold" behavior. The D flip-flop's equation looks deceptively simple precisely because the internal S/R generation and master-slave staging have already absorbed all the complexity of avoiding forbidden states and transparency hazards.

Before this

Where this leads