COA/Lesson 2 of 6/7 min read
Digital Logic
Logic gates, Boolean algebra, Karnaugh maps, combinational circuits like adders and multiplexers, and sequential circuits like flip-flops and counters.
On this page
Logic Gate
Logic gates are the building blocks of digital circuits. Each computes a Boolean function of its inputs.
| Gate | Expression | Output is 1 when |
|---|---|---|
| NOT | A' | A is 0 |
| AND | A . B | Both are 1 |
| OR | A + B | At least one is 1 |
| NAND | (A . B)' | Not both 1 |
| NOR | (A + B)' | Both are 0 |
| XOR | A XOR B | Inputs differ |
| XNOR | (A XOR B)' | Inputs are equal |
Truth table for two inputs:
| A | B | AND | OR | NAND | NOR | XOR |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
Universal Gate
NAND and NOR are universal: any Boolean function can be built using only one of them.
NOT A = A NAND A
A AND B = (A NAND B) NAND (A NAND B)
A OR B = (A NAND A) NAND (B NAND B)Chips are often built mostly from NAND gates because they are cheap and fast in CMOS.
Boolean Algebra
| Law | AND form | OR form |
|---|---|---|
| Identity | A . 1 = A | A + 0 = A |
| Null | A . 0 = 0 | A + 1 = 1 |
| Idempotent | A . A = A | A + A = A |
| Complement | A . A' = 0 | A + A' = 1 |
| Absorption | A . (A + B) = A | A + A . B = A |
| Distributive | A . (B + C) = A.B + A.C | A + B.C = (A + B)(A + C) |
De Morgan's Laws
(A . B)' = A' + B'
(A + B)' = A' . B'"Break the bar, change the operator." Also used constantly in code: !(a && b) == !a || !b.
Canonical forms
- SOP (sum of products): OR of AND terms, from rows where the output is 1 (minterms).
F = A'B + AB'. - POS (product of sums): AND of OR terms, from rows where the output is 0 (maxterms).
Karnaugh Map
A grid that simplifies Boolean expressions visually. Cells are ordered in Gray code so adjacent cells differ by one variable.
Rules:
- Group 1s in rectangles of size 1, 2, 4, 8 (powers of two).
- Groups may wrap around edges.
- Make groups as large as possible and use as few as possible.
- Each group becomes a product term; variables that change within a group drop out.
Example: F(A, B, C) = sum of minterms (1, 3, 5, 7) simplifies to F = C, since all four 1s are where C = 1.
Don't care conditions (X) can be counted as 1 or 0, whichever makes bigger groups.
Combinational Circuit
Output depends only on current inputs; no memory. Examples: adders, multiplexers, decoders, encoders, comparators.
Half Adder
Adds two bits.
Sum = A XOR B
Carry = A . BFull Adder
Adds two bits plus a carry-in.
Sum = A XOR B XOR Cin
Cout = A.B + Cin.(A XOR B)Built from two half adders and an OR gate.
Ripple carry vs carry lookahead
- Ripple carry adder: chain
nfull adders; each waits for the previous carry. Delay growsO(n). - Carry lookahead adder: computes carries in parallel using generate
G = A.Band propagateP = A XOR Bsignals. Delay aboutO(log n), at the cost of more gates.
Subtraction uses the same adder: A - B = A + B' + 1 (two's complement), with an XOR on each B input controlled by a subtract signal.
Multiplexer
A MUX selects one of 2^n inputs using n select lines and forwards it to a single output. A 2-to-1 MUX: Y = S'.I0 + S.I1. Any Boolean function of n variables can be implemented with a 2^n-to-1 MUX.
A demultiplexer does the reverse: routes one input to one of many outputs.
Decoder
n inputs activate exactly one of 2^n outputs. Used for memory address decoding and selecting which register to write. An encoder does the reverse; a priority encoder outputs the index of the highest-priority active input.
Sequential Circuit
Output depends on current inputs and stored state. Needs memory elements and usually a clock.
| Combinational | Sequential | |
|---|---|---|
| Memory | No | Yes |
| Depends on | Current inputs | Inputs and state |
| Clock | Not needed | Usually needed |
| Examples | Adder, MUX | Flip-flop, register, counter |
Synchronous circuits change state on clock edges; asynchronous ones respond to input changes immediately.
Flip-Flop
A 1-bit memory element that changes state on a clock edge. A latch is level-triggered (transparent while enabled); a flip-flop is edge-triggered.
| Type | Inputs | Behavior |
|---|---|---|
| SR | Set, Reset | S=1 sets, R=1 resets; S=R=1 is invalid |
| D | Data | Output copies D at the clock edge |
| JK | J, K | Like SR, but J=K=1 toggles |
| T | Toggle | T=1 flips the output each clock |
The D flip-flop is the workhorse of CPU registers and pipelines.
Timing terms: setup time (input stable before the edge), hold time (stable after), clock-to-Q delay. Violating them causes metastability.
Register
A group of flip-flops sharing a clock, storing a multi-bit word. Shift registers move bits left or right each clock: used for serial-to-parallel conversion and multiplication by 2.
Counter
A sequential circuit that cycles through states.
- Asynchronous (ripple) counter: each flip-flop clocks the next; simple but slow and glitchy.
- Synchronous counter: all flip-flops share one clock; faster and cleaner.
- Ring and Johnson counters: shift-register based.
A mod-n counter counts from 0 to n - 1, then resets. An n-bit binary counter has 2^n states.
Finite state machines
Sequential circuits are modeled as FSMs:
- Moore machine: output depends only on the current state.
- Mealy machine: output depends on state and current input (often fewer states, reacts faster).
Examples: traffic light controllers, vending machines, sequence detectors, the CPU control unit.
Quick revision
- NAND and NOR are universal gates.
- De Morgan:
(AB)' = A' + B',(A + B)' = A'B'. - K-maps: group 1s in powers of two, wrap edges, make groups maximal.
- Half adder:
Sum = A XOR B,Carry = AB; full adder adds a carry in. - Carry lookahead beats ripple carry delay using generate and propagate.
- MUX selects one of many inputs; decoder activates one of many outputs.
- Combinational has no memory; sequential stores state with flip-flops and a clock.
- D flip-flop stores a bit at the clock edge; registers are groups of them.