Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

Master-slave JK flip-flop from NAND gates, live

A master-slave JK flip-flop is two clocked NAND latches in a row: the master follows J and K while the clock is 1, and the slave copies the master when the clock falls to 0. Because the output only changes after the master has stopped listening, J = K = 1 toggles exactly once per clock instead of racing.

A single clocked JK latch made of four NAND gates has a flaw. Its inputs are gated with its own outputs (J with NOT Q, K with Q), so with J = K = 1 and the clock held high, the output toggles, the new output re-enables the opposite input, and it toggles again, over and over for as long as the clock stays high. That is the race-around condition. The master-slave form breaks the loop: the master's input gates still use the slave's Q and NOT Q, but the slave only changes when the clock goes low, after the master has stopped listening, so each clock gives one decision. This build is the textbook eight NAND gates, with each 3-input NAND made of an AND feeding a NAND, plus a clock inverter and a clear (CLR). While CLR is on, Q is held at 0 whatever J does; the master keeps sampling J and K, so the first falling edge after CLR is released applies them as usual. Compare the JK flip-flop built on a D flip-flop and the clocked SR flip-flop from NAND gates, which is one half of this circuit.

Big circuit: pinch, scroll or tap + to zoom in, then drag to move around.

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 →

What you need

Step by step

  1. Place a clock CLK, inputs J, K and CLR, and probes Q and Qbar.
  2. Master input gates: NAND J with CLK and Qbar, and NAND K with CLK and Q. Feeding back Q and Qbar is what makes J = K = 1 toggle.
  3. Master latch: cross-couple two NANDs, one fed by the J gate and one by the K gate.
  4. Slave: two more NANDs pass the master's outputs only when NOT CLK is 1, into a second cross-coupled pair that drives Q and Qbar.
  5. Clear: with CLR on, Q = 0. Turn CLR off, then set J and K and watch Q change each time the clock falls.

JK truth table (on the falling clock edge)

JKQ nextAction
00Qhold
010reset
101set
11NOT Qtoggle

Before the page is built, Q and Qbar are checked at every one of 13 clocks: a clear held for two clocks, with J = 1 during the first (Q stays 0), then hold, set, hold, reset, hold, three toggles in a row (one change per clock, no race-around), hold and reset. The live circuit starts with CLR on.

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

Build more parts from gates

half adderfull adderhalf subtractorfull subtractor2:1 multiplexer4:1 multiplexerXOR gate from NAND gates2-to-4 decoderSR latchD flip-flopJK flip-flopT flip-flop4-bit shift register4-bit ripple carry adderD latch1-to-4 demultiplexer4-bit magnitude comparator4-bit ring counter4-bit Johnson counter4-to-2 priority encoder4-bit synchronous counter3-to-8 decoder8-to-1 multiplexer4-bit carry lookahead adderhalf adder from NAND gates2-bit comparatorBCD to 7-segment decoder4-bit up/down counterdecade counter2-bit multiplier4-bit ALU4-bit array multiplierNOT, AND, OR and NOR from NANDNOT, OR, AND and XOR from NORfull adder from NAND gatesSR latch from NAND gatesparity generator and checker4-bit adder/subtractorBCD adder4-bit universal shift registermod-6 counter4-bit ripple counterbinary to Gray code converterhalf adder from NOR gatesfull adder from NOR gates4-bit register with parallel load1011 sequence detectorbinary to BCD converterD flip-flop from NAND gates1-bit ALUCMOS inverterCMOS NAND gateCMOS NOR gatePISO shift registerSR flip-flop from NAND gatesBCD to excess-3 code converterHamming (7,4) code encoder and correctortraffic light controller4-bit barrel shifterCMOS AND gateCMOS OR gateCMOS XOR gate1-bit magnitude comparatorhalf subtractor from NAND gatesfull subtractor from NAND gates8-to-3 encoderGray to binary converter4-to-16 decoderfull adder using 4:1 multiplexersedge detectorserial adder4-bit carry-select adderMealy machine (1011 detector)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code convertervending machine FSM4-bit carry-save adder3-bit synchronous counter (JK)CMOS transmission gatetri-state buffer (CMOS)2-to-1 mux from transmission gates

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 master-slave JK flip-flop (NAND) in the lab →

Frequently asked

What is a master-slave JK flip-flop?

Two latches in series on opposite clock phases: the master captures J and K while the clock is high, and the slave passes the master's state to the output when the clock goes low. The output therefore changes once per clock, on the falling edge.

What is the race-around condition?

In a level-triggered JK latch with J = K = 1, the output toggles, the toggled output feeds back to the inputs, and it toggles again, repeatedly, for as long as the clock is high. The final value then depends on how long the clock pulse lasts. A master-slave or an edge-triggered design avoids it.

How many NAND gates are in a master-slave JK flip-flop?

Eight in the textbook circuit: two 3-input NANDs and two 2-input NANDs for the master, four 2-input NANDs for the slave, plus an inverter for the clock. With only 2-input gates, each 3-input NAND becomes an AND followed by a NAND, as here.

Is a master-slave flip-flop edge-triggered?

It behaves like one from the outside, since the output changes only when the clock falls. Inside, the master is level-sensitive, so a pure master-slave JK can catch a glitch on J or K that happens while the clock is high (ones catching). Edge-triggered designs do not have that weakness.