Flip-Flops and Registers -- How Computers "Remember" Data
From combinational to sequential logic: understanding how D flip-flops and registers work
Real-life analogy: a camera shutter
Imagine holding a camera. Before you press the shutter, the outside world keeps changing — pedestrians walking, leaves swaying, light shifting between bright and dim.
But at the instant you press the shutter (the clock rising edge), the current scene is "latched" into the photo.
After that, no matter how the outside world changes, the content of the photo stays frozen at that instant. This is the core behavior of a D flip-flop — capturing and holding data on the rising edge of the clock signal.
Combinational logic vs sequential logic
The AND, OR, and NOT gates we studied in earlier lessons are all combinational logic. Their output depends only on the current input — when the input changes, the output changes immediately (ignoring propagation delay).
But for a computer to "remember" data, we need a circuit that keeps its output even after the input disappears. That is sequential logic.
Core difference
Combinational logic is like a lamp — it lights when you press the switch and goes out when you release; the output depends entirely on the current input.
Sequential logic is like a safe — you put something in and lock it (the clock rising edge arrives), and even after you walk away (the input disappears), the contents remain.
| Comparison Dimensions | Combinational logic | Sequential logic |
|---|---|---|
| Output depends on | Only the current input | The current input and the previous state |
| Memory capability | None (stateless) | Yes (can remember history) |
| Typical circuits | AND, OR, NOT gates, adders | D flip-flops, registers, counters |
| Clock dependence | No clock | Depends on a clock signal to synchronize state updates |
| Feedback loop | No feedback (acyclic) | Has a feedback loop to hold state |
Structure and behavior of the D flip-flop
The D flip-flop (D Flip-Flop) is the most basic sequential logic unit. It has two inputs and one output:
D (Data): data input, the value to be stored (0 or 1).
CLK (Clock): clock input, controls when data is latched.
Q: data output, outputs the currently stored value.
Key behavior: the D flip-flop only, on the clock signal'srising edge(the instant of transition from 0 to 1) latches the value of D into Q. At all other times, no matter how D changes, Q stays the same.
This is like a camera recording the scene only at the moment the shutter is pressed; at other times, changes outside do not affect the photo.
Why do we need a clock?
Without a clock signal, the D flip-flop's output would keep changing with the input, becoming just another combinational circuit. There would be no "memory" to speak of.
The clock signal provides a synchronous beat so all flip-flops update state at a unified point in time. The whole computer system works in coordination under this beat.
Registers: multiple flip-flops in parallel
A single D flip-flop can store only 1 bit of data. To store a byte (8 bits) or a word (32 bits), you need several flip-flops in parallel.
Putting N D flip-flops in parallel, sharing the same clock signal, forms an N-bit register (Register).
When the clock rising edge arrives, all flip-flops latch their own D input at the same time, saving the full N-bit data in one shot.
Multi-stage registers and pipelines
Inside a CPU, a register's output often connects to the next stage of combinational logic, then to the next register — forming a register–combinational-logic–register pipeline structure.
Each clock cycle, data flows from one register through combinational logic and is stored into the next register on the next rising edge. This is the basis of modern CPU pipelines.
Code demo: simulating D flip-flops and registers in Python
The Python code below simulates a D flip-flop and a 4-bit register. Focus on the rising-edge detection logic: data is captured only when clk transitions from 0 to 1.
Example
class
DFlipFlop:
"""
D flip-flop: latch input data on the clock rising edge
Behavior rules:
- When the clock transitions from 0 to 1 (rising edge), latch the D input to the Q output
- At other times (clk=0, or clk=1 but not a transition), Q keeps its previous value
"""
def
init
(
self
,
name
=
"DFF"
)
:
self
.
q
=
0
Output Q, initially 0
self
.
prev_clk
=
0
The clock value at the previous instant, used to detect the rising edge
self
.
name
=
name
def
update
(
self
,
d
,
clk
)
:
"""
Update the flip-flop state
Parameters:
d: data input (0 or 1)
clk: clock signal (0 or 1)
Returns:
The current Q output value
Rising-edge detection: prev_clk == 0 and clk == 1
Only when this condition is met is the value of d latched into q
"""
=== Rising-edge detection ===
if
self
.
prev_clk
==
0
and
clk
==
1
:
self
.
q
=
d
Latch the value of D into Q on the rising edge
self
.
prev_clk
=
clk
Record the current clock state for use in the next cycle
return
self
.
q
def
reset
(
self
)
:
"""Reset the flip-flop state"""
self
.
q
=
0
self
.
prev_clk
=
0
class
Register4Bit:
"""
4-bit register: 4 D flip-flops in parallel, sharing the same clock signal
Structure diagram:
D3 ──▶ [DFF3] ──▶ Q3 (high bit)
D2 ──▶ [DFF2] ──▶ Q2
D1 ──▶ [DFF1] ──▶ Q1
D0 ──▶ [DFF0] ──▶ Q0 (low bit)
↑↑↑↑ share the same CLK
When the clock rising edge arrives, all 4 flip-flops latch their own D input simultaneously
"""
def
init
(
self
)
:
Create 4 flip-flops, bit[3] is the highest bit, bit[0] the lowest
self
.
flip_flops
=
[
DFlipFlop
(
"bit3"
)
,
DFlipFlop
(
"bit2"
)
,
DFlipFlop
(
"bit1"
)
,
DFlipFlop
(
"bit0"
)
,
]
def
update
(
self
,
data
,
clk
)
:
"""
Update the register
Parameters:
data: the data to store (an integer 0-15; out-of-range values are truncated)
clk: clock signal (0 or 1)
Returns:
The value currently stored in the register (0-15)
"""
data
=
data &
0b1111
Ensure only 4 bits are valid
Feed each bit into its corresponding flip-flop, high bit first
for
i
in
range
(
4
)
:
Extract each bit from high to low: bit3, bit2, bit1, bit0
bit
=
(
data
(
3
- i
)
)
&
1
self
.
flip_flops
[
i
]
.
update
(
bit
,
clk
)
return
self
.
read
(
)
def
read
(
self
)
:
"""Read the value currently stored in the register (without changing state)"""
value
=
0
for
ff
in
self
.
flip_flops
:
value
=
(
value
<<
1
)
| ff.
q
return
value
def
reset
(
self
)
:
"""Reset all flip-flops"""
for
ff
in
self
.
flip_flops
:
ff.
reset
(
)
============================================================
Simulation demo: register behavior over multiple clock cycles
============================================================
if
name
==
"main"
:
reg
=
Register4Bit
(
)
Test data sequence: simulate writing different data at different clock cycles
test_data
=
[
5
,
10
,
15
,
0
,
7
,
13
,
3
,
8
,
1
,
14
]
5=0101, 10=1010, 15=1111, 0=0000, 7=0111
13=1101, 3=0011, 8=1000, 1=0001, 14=1110
(
"="
55
)
(
" runoob - 4-bit register simulation demo"
)
(
" Observe: data is latched into the register only on the clock rising edge"
)
(
"="
55
)
(
f
"{'Cycle':<6} {'Clock':<10} {'Input data D':<12} {'Register value Q':<12} {'Note'}"
)
(
"-"
55
)
for
cycle
in
range
(
10
)
:
data
=
test_data
[
cycle
]
Phase 1: clock low (clk=0), the register value should not change
reg.
update
(
data
,
0
)
Phase 2: clock rising edge (clk from 0→1), latch the data
result
=
reg.
update
(
data
,
1
)
data_bin
=
f
"{data:04b}"
result_bin
=
f
"{result:04b}"
note
=
"← rising-edge latch"
if
result
==
data
else
"State unchanged"
(
f
"{cycle:<6} {'0→1':<10} {data}({data_bin}){'':<4} {result}({result_bin}){'':<4} {note}"
)
(
"-"
55
)
(
)
============================================================
Extra demo: data is latched only on the rising edge and held at other times
============================================================
(
"="
55
)
(
" Demo 2: on non-rising edges, data changes are not latched"
)
(
"="
55
)
reg2
=
Register4Bit
(
)
First write the initial value 5
reg2.
update
(
5
,
0
)
reg2.
update
(
5
,
1
)
(
f
"Write 5 on rising edge → register value: {reg2.read()}"
)
Keep clk high and change the data to 10 — it should not be latched
reg2.
update
(
10
,
1
)
(
f
"Change data to 10 while clk=1 → register value: {reg2.read()} (should still be 5)"
)
Bring clk back low, then give a rising edge to write 10
reg2.
update
(
10
,
0
)
result
=
reg2.
update
(
10
,
1
)
(
f
"Write 10 on clk 0→1 → register value: {reg2.read()} (now 10)"
)
(
)
(
"="
55
)
(
" Demo complete! runoob - understanding flip-flops and registers"
)
(
"="
55
)
Interactive demo: D flip-flop timing diagram
Below, using Chart.js's stepped:true option, we draw timing diagrams of the clock signal (square wave) and data signal. Red vertical lines mark each clock rising edge (the moment CLK transitions from 0 to 1) — exactly the key instants when the D flip-flop latches data.
D flip-flop timing diagram — rising-edge latch demo
Clock CLK (square wave)
Data input D
Output Q (latched value)
Red dashed line = rising-edge instant (D flip-flop latches data).
The Q output updates only on the rising edge and stays unchanged the rest of the time.