Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksBuild a CPUToolsOpen the lab →

How to build a D flip-flop from logic gates

A D flip-flop captures its data input on the clock's rising edge and is built from two gated D latches in series (a master and a slave) with the clock inverted between them.

A plain latch is transparent: while it is enabled the output tracks the input. A flip-flop is stricter: it only samples on the clock edge, which is what makes synchronous design predictable. The trick is two latches, each itself made of gates, run on opposite clock phases so their open windows never overlap.

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 →

Embed or cite this circuit

Free to embed on a course page, blog or worksheet: paste this code. The circuit stays live, and the small digiwleea badge links back here.

To cite it: digiwleea, “D flip-flop”, https://digiwleea.wleeaf.dev/tools/build/d-flip-flop/

What you need

Step by step

  1. Place a clock CLK and a data input D.
  2. Invert the clock with a NOT gate. The inverted clock enables the first (master) latch; the raw clock enables the second (slave) latch.
  3. Feed D into the master D latch. While CLK = 0 the master is open and follows D; the slave is closed and holding.
  4. Wire the master's output into the slave D latch. When CLK rises to 1 the master freezes whatever D was at that instant, and the slave opens and passes that frozen value to the output Q.
  5. Because the two latches are never open at the same moment, D reaches Q only at the rising edge. Probe Q and pulse the clock to watch it capture exactly once per cycle.

D flip-flop behaviour

CLKDQ (next)
rise00
rise11
0 or 1 (steady)xhold
fallxhold

The only moment Q can change is the rising edge, when it takes the value of D. Any change to D between edges is ignored. That one-update-per-cycle predictability is why every register in a processor is built from flip-flops, not bare latches. Compare the two side by side in D latch vs D flip-flop, or see two of them, with preset and clear, in the 7474 chip and the CMOS CD4013. A common mistake is to draw Q following D while the clock is high: that is a D latch. A flip-flop changes only at the edge.

You can also read the full theory in the lesson on the D flip-flop. 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.

More builds: Memory: latches and flip-flops

All 247 build guides, by topic

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 D flip-flop in the lab →

Frequently asked

What gates make a D flip-flop?

A positive-edge D flip-flop is two gated D latches in series (a master and a slave) plus one NOT gate on the clock. Each D latch is itself built from AND and NOT gates around an SR latch, so the whole thing reduces to logic gates.

What is the difference between a D latch and a D flip-flop?

A D latch is level-sensitive: while enabled its output follows D continuously. A D flip-flop is edge-triggered: it samples D only at the clock edge and holds the rest of the cycle. The flip-flop is two latches on opposite clock phases.

Why does a D flip-flop need two latches?

One latch would be transparent while enabled, letting data ripple straight through. Putting a master and a slave on opposite clock phases means only one is ever open, so data advances by exactly one stage per clock edge instead of racing through.

What is the characteristic equation of a D flip-flop?

Q(next) = D: on the active clock edge the flip-flop stores whatever D is.

What is the excitation table of a D flip-flop?

To move Q from its present value to a wanted next value, set D to that next value: 0 to 0 needs D = 0, 0 to 1 needs D = 1, 1 to 0 needs D = 0 and 1 to 1 needs D = 1.

What is the truth table of a positive edge-triggered D flip-flop?

On a rising clock edge, Q takes the value of D: D = 0 gives Q = 0 and D = 1 gives Q = 1. At any other time, whatever D does, Q keeps its value. That is why a D flip-flop's table has a clock column marked with an up arrow.

Can a D flip-flop be used as a counter?

Yes. Feed Q' back into D and the flip-flop toggles on every clock edge, dividing the clock by two. Chaining such stages gives a ripple counter, as in the live frequency divider and ripple counter builds.