OCR H446, section 1.4.3 (e)
Logic gates can do more than evaluate conditions. Joined together, they can store data and do arithmetic. This page covers a circuit that remembers a bit (the D-type flip flop), and circuits that add (half and full adders).
The D-type flip flop
A flip flop is a circuit that can store (or delay) one bit and hold it until it is told to change. It is the basic building block of registers and memory.
The D-type flip flop has:
- a D input (the data to store);
- a clock input;
- a Q output (the stored value);
- a (NOT Q) output, which is always the inverse of Q.
The small triangle on the clock input shows that the flip flop is edge-triggered.
How it works
The flip flop is edge-triggered: it only looks at at a particular moment, when the clock signal changes (for example on the rising edge, from 0 to 1).
- On the clock edge, the value at D is copied to Q.
- Between clock edges, Q stays the same, whatever happens to D.
| Clock | D | Q after |
|---|---|---|
| Rising edge | 0 | 0 |
| Rising edge | 1 | 1 |
| No edge | any | unchanged |
So the flip flop remembers the value that D had at the last clock edge. That is 1 bit of memory.
Uses
- A row of D-type flip flops (one for each bit) forms a register: for example an 8-bit register uses 8 flip flops sharing a clock.
- Registers in the processor (the program counter, accumulator and others), and cache and static memory (SRAM), are made of such circuits.
- They synchronise a circuit with the clock, so that everything changes in step.
The half adder
An adder adds binary digits. A half adder adds two single bits, and , and produces:
- a sum (the result bit), and
- a carry (to the next column).
| A | B | Carry | Sum |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 0 |
(This is just in binary.)
Look at the columns:
- Sum is A XOR B: it is 1 when the inputs are different.
- Carry is A AND B: it is 1 only when both are 1.
So a half adder is built from one XOR gate and one AND gate, both fed with and .
The limit of a half adder
It can't add a carry coming in from the previous column. It can only be used for the rightmost column of an addition.
The full adder
A full adder adds three bits: , , and a carry in () from the previous column. It produces a sum and a carry out ().
| A | B | Sum | ||
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 |
Building it from two half adders
- The first half adder adds and , giving a partial sum and a carry .
- The second half adder adds and , giving the final sum and another carry .
- An OR gate combines the two carries: .
Adding whole numbers: the ripple carry adder
To add multi-bit numbers, join several full adders in a row. The carry out of each one is the carry in of the next, so the carry "ripples" along from the right to the left. The rightmost column can use a half adder (or a full adder with a carry in of 0).
Example: add (5 + 3) with a 4-bit adder.
| Column (right to left) | A | B | Carry in | Sum | Carry out |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 0 | 0 | 1 |
| 2 | 0 | 1 | 1 | 0 | 1 |
| 3 | 1 | 0 | 1 | 0 | 1 |
| 4 | 0 | 0 | 1 | 1 | 0 |
Reading the sum bits from column 4 to column 1: . ✓
A final carry out of the leftmost column would signal an overflow.
Disadvantage: each adder must wait for the carry from the one before, so a long ripple carry adder is slow. Faster designs (such as carry look-ahead) exist.
Worked example
Task: use the full adder truth table to find the sum and carry out for , , , and explain it.
- From the table: and .
- This is : write 1 and carry 1. ✓
- By the gates: , so Sum . . ✓
Exam tip: remember that the half adder is XOR for the sum and AND for the carry, and that a full adder is two half adders and an OR gate. For the D-type flip flop, say that it stores one bit, name its inputs (D, clock) and outputs (Q, NOT Q), and say that Q takes the value of D only on the clock edge. To draw Q on a timing diagram, look at D only at each rising edge.
Check yourself
What does a D-type flip flop do?
It stores one bit. When the clock edge occurs, the value at D is copied to Q, and Q then stays the same until the next clock edge.
Which two gates make a half adder?
An XOR gate gives the sum, and an AND gate gives the carry.
Why is a full adder needed, and not just a half adder?
A half adder can't accept a carry from the previous column, so it can only be used for the rightmost bit. A full adder takes a carry in, so it can be used in every column.
What are the sum and carry out of in a full adder?
Sum 0 and carry out 1, since .