Draft preview. Lessons are being written and reviewed.

Digital Design / Module 1: Numbers and logic

2. Logic gates and truth tables

Connect each gate to the question it answers.

Lecture coming soon

See it move

The idea

A gate answers a simple yes-or-no question about its inputs. Think of each gate as a question, and truth tables stop being something you memorise:

GateQuestion it asks
ANDAre all inputs 1?
ORIs any input 1?
XORDo the inputs differ? (for two inputs)
NOTWhat is the opposite?

NAND, NOR and XNOR are the same questions with the answer inverted.

Truth tables

A truth table lists the output for every input combination, so N inputs need 2^N rows. It is a complete description of any combinational block, and it grows quickly: a 32-input function would have over four billion rows. That is why we build large functions from small ones instead.

Universal gates

NAND alone can build every other function, and so can NOR. A NAND with its inputs tied together is an inverter. A NAND followed by an inverter is an AND. Real cell libraries still offer many gate types, because a dedicated cell is usually smaller and faster than a combination of NANDs.

Worked example: XOR from four NANDs

Let n1 = NAND(A,B), n2 = NAND(A,n1) and n3 = NAND(B,n1). The output is NAND(n2,n3). The animation above lets you check all four input combinations and watch each internal signal.

Common mistake

Assuming XOR with many inputs still means "exactly one input is high". A multi-input XOR tells you whether an odd number of inputs are high. That is why XOR is used for parity, and it is a common trick question in interviews.

Key ideas

  • AND asks all, OR asks any, XOR asks different.
  • NAND and NOR are each universal.
  • A truth table with N inputs has 2^N rows.

Your exercise

Build XOR using only NAND gates.

Interview questions

coreHow many 2-input NANDs make an XOR?

coreWhy are NAND gates preferred over NOR in CMOS?

basicHow many different Boolean functions of two inputs exist?

basicA 3-input XOR has all three inputs at 1. What is the output?

coreBuild a 2-input OR using only 2-input NAND gates. How many do you need?

coreShow how NOR alone makes an inverter and a 2-input AND.

coreA 5-input XOR's truth table has how many rows, and in how many of them is the output 1?

coreWhat does this line compute for two 4-bit buses, and why does it work?

wire [3:0] a, b;
wire eq = &(a ~^ b);

advancedAn 8-bit byte is 1101_0110. What even-parity bit is sent with it, and what does the receiver check?

wire [7:0] d = 8'b1101_0110;
wire p_even = ^d;    // parity bit sent with d

advancedCan you make a 3-input NAND by feeding one 2-input NAND's output into a second 2-input NAND with the third input?

Practise these 10 questions with animations