Binary basics

Flip-flops and latches: SR, D, JK and T explained

How the circuits that store a single bit work, with a simulator, truth tables and timing diagrams for each type.

Written by
Updated · 18 min read

A flip-flop is a circuit that stores one bit. It holds a 0 or a 1 on its output, called Q, and keeps holding it until its inputs tell it to change. A latch does the same job with one difference: a latch reacts to its inputs for as long as it is enabled, while a flip-flop only reacts at the moment the clock changes, usually when it rises from 0 to 1. Registers, counters and the state inside every processor are built from latches and flip-flops.

Key takeaways

A latch follows its input while enabled; a flip-flop changes only on the clock edge.
The SR latch is two cross-coupled NOR gates, and S = R = 1 is not allowed.
A D flip-flop is two D latches in a row and copies D on each rising edge: Q next = D.
A JK flip-flop toggles when J = K = 1: Q next = JQ' + K'Q.
A T flip-flop held at 1 halves the clock frequency, so chains of them make counters.
binarytranslator.ai

There are four common types. The SR latch sets or resets the stored bit. The D flip-flop copies its D input on each clock edge. The JK flip-flop works like SR but toggles when both inputs are 1. The T flip-flop toggles on each clock edge while T is 1. Pick one below, change the inputs, pulse the clock and watch Q change on the timing diagram.

Types of flip-flops

This table puts the four types side by side, with the D latch added because a D flip-flop is built from two of them. "Q next" is the value Q will have after the clock edge, or right away for a latch, and Q' means "the opposite of Q".

TypeInputsWhat it doesQ next
SR latchS (set), R (reset)S makes Q = 1, R makes Q = 0, S = R = 1 is not allowedS + R'Q
D latchD (data), EN (enable)Q follows D while EN is 1 and holds while EN is 0D while EN = 1
D flip-flopD, clockQ copies D on each rising clock edgeD
JK flip-flopJ, K, clocklike SR, but J = K = 1 makes Q toggleJQ' + K'Q
T flip-flopT (toggle), clockQ flips on each clock edge while T is 1T ⊕ Q

In practice the D flip-flop is the one you will use most. Registers, pipelines and the state of almost every digital design are rows of D flip-flops sharing one clock. JK and T flip-flops show up in counters and in textbooks, and in textbook gate diagrams the SR latch is the circuit all the others are built on.

Latch vs flip-flop

The difference is when the output is allowed to change. Feed the same D signal into a D latch and a D flip-flop, connect the same clock to the latch's enable and the flip-flop's clock, and compare the two outputs:

Timing diagram comparing a D latch and a D flip-flop fed the same clock and the same D input. The latch output copies every change of D while the clock is high, including short pulses. The flip-flop output changes only at the rising clock edges, taking the value D has at that moment.

While the clock is high, the latch is transparent: every change on D passes straight through to Q, glitches included. The flip-flop looks at D once, at the instant the clock rises, and ignores everything D does in between. That is why the term "level-triggered" goes with latches and "edge-triggered" with flip-flops.

LatchFlip-flop
Reacts tothe level of the enable input (EN = 1)the edge of the clock (0 to 1, or 1 to 0)
Q can changeany time EN is 1only at the clock edge
Sizesmaller: about half the gatestwo latches in a row
Where you meet itswitch debouncing, some low-power and high-speed designsregisters, counters, state machines, almost all synchronous logic

Older books use "flip-flop" for both. Most current authors keep "flip-flop" for edge-triggered circuits only and call the level-sensitive versions latches, which is how this guide uses the words.

The SR latch

The simplest memory circuit is two NOR gates, each feeding its output into one input of the other. The two free inputs are S (set) and R (reset), and the two outputs are Q and Q'.

SR latch circuit made of two cross-coupled NOR gates. R and Q' go into the top gate, whose output is Q. S and Q go into the bottom gate, whose output is Q'. With S and R at 0 the latch holds Q, R = 1 resets Q to 0, S = 1 sets Q to 1, and S = R = 1 forces both outputs to 0, which is not allowed.

Follow it through with S = 1 and R = 0. A NOR gate outputs 1 only when both its inputs are 0, so the gate that gets S outputs 0. That 0 goes to the other gate, which now has R = 0 and 0 on its inputs, and outputs 1. So Q = 1. Now drop S back to 0. The gate with S still sees Q = 1 on its other input, so it keeps outputting 0, and Q stays 1. The loop remembers. The NOR gate truth table is all you need to check every row below.

SR latch truth table

SRQ nextQ' nextState
00QQ'hold, no change
0101reset
1010set
1100not allowed

Why is S = R = 1 not allowed? Both NOR gates get a 1, so both output 0, and Q and Q' are equal. The circuit no longer means what its labels say. The real trouble comes next: if S and R both drop to 0 at the same moment, each gate waits for the other. The output can hover between levels or oscillate for a moment before it settles to 0 or 1, depending on which gate is a little faster, and you cannot predict which. Try it in the simulator: set S and R to 1, then press "Release S and R together", and Q turns into a question mark.

NAND SR latch

The same latch built from two NAND gates has active-low inputs, usually written S̅ and R̅. A 0 on S̅ sets the latch and a 0 on R̅ resets it, while 1 on both holds. The forbidden input is now both at 0, which forces both outputs to 1. NAND is the more common choice on chips, because in CMOS a NAND gate switches faster than a NOR gate of the same size. De Morgan's law shows how the two versions relate: a NAND gate is an OR gate with inverted inputs, so for hold, set and reset the NAND latch behaves like the NOR latch with active-low inputs. Only the forbidden state differs.

S̅R̅Q nextState
11Qhold
100reset
011set
001 (and Q' = 1)not allowed

A common use is cleaning up a mechanical switch. When a switch closes, its contacts bounce for a few milliseconds and a plain input sees several pulses. Wire a two-way switch so it pulls S̅ or R̅ to 0, with a pull-up resistor holding each input at 1 the rest of the time. The first contact sets the latch. During each bounce the input goes back to 1, which only means hold, so Q changes once.

The D latch

The D latch removes the forbidden input. It puts two AND gates in front of an SR latch and an inverter between them, so S gets D and R gets the opposite of D. S and R can never both be 1. A second input, EN (enable, sometimes called gate or clock), goes into both AND gates. While EN is 0, both gates output 0 and the latch holds. While EN is 1, the latch is transparent and Q follows D. A latch with this enable input is often called a gated D latch. Leave out the inverter, feed S and R in separately, and you have a gated SR latch.

D latch truth table

ENDQ nextState
0anyQhold
100Q follows D
111Q follows D

Open the D latch in the simulator and turn EN on. Every time you toggle D, Q follows it. Turn EN off and D can change as much as it likes without Q moving.

The D flip-flop

Put two D latches in a row and drive their enables with opposite clock levels, and you get a D flip-flop. The first latch, the master, is transparent while the clock is 0 and keeps tracking D. The second latch, the slave, is transparent while the clock is 1. At the rising edge, the master closes and freezes the last value of D, and the slave opens and passes that frozen value to Q. While the clock stays high, D can change but the closed master blocks it. The result is a circuit that samples D once per clock cycle, at the rising edge.

D flip-flop truth table

ClockDQ nextState
↑ (rising edge)00reset
↑ (rising edge)11set
0, 1 or falling edgeanyQhold

Its rule is short: Q next = D. The flip-flop delays D by one clock cycle and holds it steady in between, which is why it is called a data or delay flip-flop.

D flip-flop timing diagram

Read a timing diagram left to right, as time passing. Each rising edge of the clock is a moment where Q may change. In the simulator's D flip-flop example, D goes high, then the first rising edge copies it to Q. Later, D dips to 0 and back to 1 between two edges. Q never sees that dip, because nothing happened at an edge. Then D falls to 0 before an edge, and Q drops to 0 at that edge. To check any diagram yourself, put your finger on each rising edge, read D right before it, and that is Q until the next edge.

Real D flip-flops add two more inputs, preset and clear, that force Q to 1 or 0 straight away without waiting for the clock. The 74HC74 is a common example: one 14-pin chip holds two positive-edge-triggered D flip-flops, each with its own preset and clear. Both are active low on that chip, so a 0 on preset or clear sets or resets the output whatever the clock and D are doing. Pulling both low at once drives Q and Q' to 1 together, a state the datasheet notes will not last once they are released.

The JK flip-flop

The JK flip-flop keeps the set and reset of the SR latch and gives the forbidden input a job. J works like S and K works like R, both checked at the clock edge. When J and K are both 1, Q toggles: 0 becomes 1 and 1 becomes 0.

JK flip-flop truth table

JKQ nextState
00Qno change
010reset
101set
11Q'toggle

Each row only applies at a rising clock edge. Between edges, Q holds whatever J and K are doing. In the simulator's JK example, J and K are both 1 for the last three clock pulses, so Q flips on every one of them.

JK flip-flop characteristic equation

The truth table turns into one Boolean expression. Q is 1 next when J sets a Q that is 0, or when K fails to reset a Q that is already 1:

Q next = J·Q' + K'·Q

You can get this from the truth table yourself. Number the rows by J, K and Q as a 3-bit value, and Q next is 1 in rows 1, 4, 5 and 6. Enter those minterms in the Karnaugh map solver with three variables, reading A as J, B as K and C as Q. It returns B'C + AC', which is K'Q + JQ' with the letters put back. The other types reduce the same way: Q next = D for the D flip-flop, Q next = T ⊕ Q for the T flip-flop, and Q next = S + R'Q for the SR latch, with the condition that S and R are never both 1.

Master-slave JK flip-flop and the race-around condition

Early JK flip-flops were built from a gated latch with its outputs fed back to its inputs. With J = K = 1 and the clock held high, Q toggles, the new Q feeds back, and Q toggles again, over and over for as long as the clock stays high. By the time the clock falls, Q could be either value. This is called the race-around condition, and it happens whenever the clock pulse is longer than the time a signal takes to pass through the gates.

The master-slave JK flip-flop fixes it the same way the D flip-flop does. The master latch takes J and K while the clock is high, and the slave copies the master when the clock falls, so the output changes once per clock pulse. It still has a weak spot, called ones catching: a short glitch on J or K while the clock is high gets stored in the master and passed on. Modern chips use edge-triggered JK flip-flops instead, which sample J and K at the clock edge only, so neither problem can happen.

The T flip-flop

The T flip-flop has one input, T for toggle. On each rising clock edge, Q flips if T is 1 and stays put if T is 0. It is a JK flip-flop with J and K tied together.

T flip-flop truth table

TQQ nextState
000no change
011no change
101toggle
110toggle

The rule is Q next = T XOR Q, which you can try on any pair of bits in the XOR calculator. Hold T at 1 and Q changes once per clock cycle, so it makes one full cycle for every two clock cycles. Its frequency is half the clock's: a T flip-flop is a divide-by-two circuit. Chain several and each one halves the frequency again. To make the chain count up, clock each stage from the Q' output of the stage before it, or use flip-flops that trigger on the falling edge. Three stages count from 000 to 111 and start over, the same sequence you get when counting in binary from 0 to 7:

Timing diagram of a 3-bit counter built from T flip-flops. Q0 changes on every rising clock edge, Q1 at half that rate and Q2 at a quarter. Read together as Q2 Q1 Q0 they count 001, 010, 011, 100, 101, 110, 111, then back to 000.

With n stages the last output runs at the clock frequency divided by 2n. A 32,768 Hz watch crystal through 15 stages gives exactly 1 pulse per second, which is why that crystal frequency is so common.

Characteristic and excitation tables

A characteristic table answers "given these inputs, what does Q become?". That is what the truth tables above show. An excitation table answers the reverse: "Q is now 0 and I want it to be 1, which inputs do I need?". You need it when you design a counter or a state machine, because you start from the states you want and work back to the inputs.

QQ nextS RJ KDT
000 x0 x00
011 01 x11
100 1x 101
11x 0x 010

An x means "either value works". For the JK flip-flop, going from 0 to 1 needs J = 1, and K does not matter: with K = 0 the flip-flop sets, and with K = 1 it toggles, and both give 1. Those don't-care entries are why JK flip-flops often lead to the simplest counter circuits. The D column needs no working out: D is always the value you want next.

Converting one flip-flop into another

Any type can stand in for another with a little logic in front of its inputs. To find the logic, take the excitation table of the flip-flop you have, list the inputs it needs for each Q and Q next of the one you want, and simplify. The results:

You wantYou haveConnect
TDD = T ⊕ Q (one XOR gate)
TJKJ = K = T
DJKJ = D, K = D' (one inverter)
JKDD = JQ' + K'Q
DTT = D ⊕ Q

The first row is the one people get wrong. Wiring T straight into D gives a flip-flop that copies T, not one that toggles. The XOR with Q is what makes the output flip: when T is 1, D gets the opposite of Q.

Setup time, hold time and metastability

A flip-flop needs D to stay steady for a short time around the clock edge. Setup time is how long D must be stable before the edge. Hold time is how long it must stay stable after the edge. Both are listed in every flip-flop datasheet, often in nanoseconds or less.

If D changes inside that window, the flip-flop can become metastable: its output hangs between 0 and 1, or takes much longer than usual to settle. It will end up at 0 or 1, but you cannot know which or exactly when. This is the same problem as releasing S and R together in an SR latch.

Inside one clocked design, timing tools check that setup and hold times are met on every path. The risk is a signal that arrives from outside, such as a button, a sensor, or another chip running on a different clock. The standard fix is a synchronizer: two flip-flops in a row on the receiving clock. If the first one goes metastable, it gets a full clock cycle to settle before the second one samples it. That does not make metastability impossible, but it makes it rare enough for most designs, and more stages make it rarer still.

Where flip-flops are used

  • Registers: 8, 32 or 64 D flip-flops sharing one clock hold a byte or a word. A processor's pipeline stages are built this way, while large register files and caches use denser SRAM cells.
  • Shift registers: D flip-flops in a chain, each feeding the next, move a bit one place along on every clock edge. They turn serial data into parallel and back.
  • Counters and frequency dividers: chains of T or JK flip-flops, as in the counter diagram above.
  • State machines: a few flip-flops store the current state, and logic gates work out the next state from the inputs. Traffic lights, vending machines and USB controllers are built this way.
  • Synchronizers and debouncers: two flip-flops for signals from another clock, and SR latches for mechanical switches.

Flip-flops are the sequential half of digital logic. A full adder is combinational: its output depends only on its inputs right now. Add flip-flops and a circuit gets memory, so its output can depend on what happened before.

Where the flip-flop came from

The first electronic latch was built by British physicists William Eccles and F. W. Jordan from two vacuum tubes in 1918. Their patent, GB 148582, was filed that year and published in 1920. They called it a trigger circuit, and it became known as the Eccles-Jordan circuit. Colossus, the British code-breaking computer of 1943, used these circuits. According to engineer P. L. Lindley, Montgomery Phister's computer design course at UCLA was teaching the SR, D, T and JK types by 1954, and the JK name comes from Eldred Nelson at Hughes Aircraft, who used the notations "j-input" and "k-input" in a patent application filed in 1953. The letters were labels, not abbreviations.

Questions people ask

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

A latch is level-triggered: its output follows its inputs for as long as the enable input is 1. A flip-flop is edge-triggered: its output can only change at the moment the clock rises (or falls, for a negative-edge flip-flop).

Why is S = R = 1 not allowed in an SR latch?

In a NOR SR latch it forces both Q and Q' to 0, so they are no longer opposites. When both inputs then return to 0 together, the latch settles to 0 or 1 unpredictably, depending on which gate is faster.

What does JK stand for in a JK flip-flop?

Nothing. J and K were labels for the two inputs in a 1953 patent application by Eldred Nelson, and the name stuck.

How many bits does a flip-flop store?

One. Q is either 0 or 1, and Q' is always its opposite, so the pair still carries a single bit. A byte needs 8 flip-flops.

What is the race-around condition?

In a level-triggered JK flip-flop with J = K = 1, Q keeps toggling for as long as the clock stays high, so its final value is unknown. Master-slave and edge-triggered JK flip-flops prevent it by letting Q change only once per clock cycle.

Is a D flip-flop edge-triggered?

Yes. A D flip-flop changes Q only on a clock edge, usually the rising one. The level-triggered version with an enable input is called a D latch.

What is a T flip-flop used for?

Counters and frequency dividers. With T held at 1 it toggles on every clock edge, which divides the clock frequency by two, and n of them in a chain divide it by 2 to the power n.

What is a characteristic equation?

It is the truth table of a flip-flop written as one Boolean expression for Q next. For the JK flip-flop it is Q next = JQ' + K'Q, and for the T flip-flop it is Q next = T XOR Q.

About the authors

Written byZachary PainterTechnical writer at GitLab

Zachary Painter is a technical writer at GitLab, where he writes developer documentation and UI text. He has written API documentation for REST and GraphQL APIs and reference docs for Kubernetes, Docker and command-line tools, earlier as a technical writer at Pomerium and a senior technical content writer at Stream. He holds a BA in English and German studies from the University of North Carolina at Greensboro. On binarytranslator.ai he writes guides and the how-to sections on tool pages.

All guides by ZacharyLinkedIn

Reviewed byMehran Mozaffari KermaniProfessor of computer engineering, University of South Florida

Mehran Mozaffari Kermani is a professor at the Bellini College of Artificial Intelligence, Cybersecurity and Computing at the University of South Florida. His research covers computer arithmetic, cryptographic hardware and fault detection in digital circuits, and he worked as an ASIC design engineer at AMD before he joined academia. He earned his PhD in electrical and computer engineering at the University of Western Ontario, was a postdoctoral fellow at Princeton and is a senior member of IEEE. On binarytranslator.ai he reviews the logic gate, binary arithmetic and floating-point tools.

ProfileLinkedInHow we review

Keep reading

All posts
The bytes of Hi! add up to 210, which is the checksum D2.Binary basics

What is a checksum? How checksums, parity bits and CRC catch errors

A checksum is a short value calculated from data so you can tell if it changed. See a byte sum, parity bits, the internet checksum, CRC, Hamming codes and SHA-256, each worked through.11 min read
In an 8-bit signed integer, 127 + 1 equals -128.Binary basics

MSB and LSB: most and least significant bits, signed integers and overflow

The MSB is the leftmost, highest-value bit and the LSB the rightmost. See what each tells you, how signed integers use the MSB as a sign bit, and how integer overflow wraps values around.9 min read
In floating point, 0.1 + 0.2 equals 0.30000000000000004.Binary basics

Floating point numbers explained: why 0.1 + 0.2 is not 0.3

A floating point number is scientific notation in binary. See why 0.1 + 0.2 is 0.30000000000000004, how precise floats are, float vs double, and how to compare floats and handle money.8 min read
Scroll to Top