Skip to content
Chethan Yadav_
Computer Architecture

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.

GateExpressionOutput is 1 when
NOTA'A is 0
ANDA . BBoth are 1
ORA + BAt least one is 1
NAND(A . B)'Not both 1
NOR(A + B)'Both are 0
XORA XOR BInputs differ
XNOR(A XOR B)'Inputs are equal

Truth table for two inputs:

ABANDORNANDNORXOR
0000110
0101101
1001101
1111000

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

LawAND formOR form
IdentityA . 1 = AA + 0 = A
NullA . 0 = 0A + 1 = 1
IdempotentA . A = AA + A = A
ComplementA . A' = 0A + A' = 1
AbsorptionA . (A + B) = AA + A . B = A
DistributiveA . (B + C) = A.B + A.CA + 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:

  1. Group 1s in rectangles of size 1, 2, 4, 8 (powers of two).
  2. Groups may wrap around edges.
  3. Make groups as large as possible and use as few as possible.
  4. 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 . B

Full 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 n full adders; each waits for the previous carry. Delay grows O(n).
  • Carry lookahead adder: computes carries in parallel using generate G = A.B and propagate P = A XOR B signals. Delay about O(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.

CombinationalSequential
MemoryNoYes
Depends onCurrent inputsInputs and state
ClockNot neededUsually needed
ExamplesAdder, MUXFlip-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.

TypeInputsBehavior
SRSet, ResetS=1 sets, R=1 resets; S=R=1 is invalid
DDataOutput copies D at the clock edge
JKJ, KLike SR, but J=K=1 toggles
TToggleT=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.