Logic gates and truth tables · Physics Class 11–12 | StemPy Lab
Course 27.3·Lesson 3 of 3
Logic gates and truth tables
A logic gate has inputs and an output that are each 0 or 1. The truth table lists the output for every input. OR, AND and NOT are the basic gates, NAND and NOR are their opposites, and XOR gives 1 when its inputs differ. NAND alone, or NOR alone, can make every other gate, so each is a universal gate.
about 30 min1 simulation2 practice questions1 Python program5 questions to check yourself
This lesson is free to read. Enroll free to keep your place and take the tests.
Lesson 3 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
Yes or no
A smoke alarm asks one question: is there smoke? A lift asks two: is the door closed, and has a floor been chosen? The answers are yes or no, which we write as 1 and 0. A circuit that takes such answers and gives an answer is a logic gate. A computer is a very large collection of gates.
In a digital circuit a voltage has only two meanings. A high voltage, such as 5 V, is 1. A low voltage, near 0 V, is 0. A small noise does not change the meaning, so digital signals are copied and stored without loss. The lesson on the transistor switch showed how one gate, the NOT gate, is made.
The basic gates
A truth table lists the output Y for every combination of the inputs. With n inputs there are 2n rows.
Six logic gates, each with its distinctive symbol, its name above, its truth table below and its expression for the output Y beside the table. NOT has one input A and a triangle with a small circle at the output: Y is the opposite of A, so A equal to 0 gives 1 and A equal to 1 gives 0. OR gives Y equal to A plus B and its table is 0, 1, 1, 1 for the rows 00, 01, 10 and 11. AND gives Y equal to A times B and its table is 0, 0, 0, 1. NAND is the AND shape with a small circle and its table is 1, 1, 1, 0. NOR is the OR shape with a small circle and its table is 1, 0, 0, 0. XOR is the OR shape with an extra curved line at the input and its table is 0, 1, 1, 0.
NOT has one input. Y=A. The output is the opposite of the input.
OR gives 1 if at least one input is 1. Y=A+B.
AND gives 1 only if both inputs are 1. Y=A⋅B.
NAND is AND followed by NOT. Y=A⋅B. It gives 0 only when both inputs are 1.
NOR is OR followed by NOT. Y=A+B. It gives 1 only when both inputs are 0.
XOR (exclusive OR) gives 1 when the two inputs are different. Y=A⊕B. XNOR is the opposite: it gives 1 when they are the same.
Read the four rows in the order (A,B)=(0,0), (0,1), (1,0), (1,1). The outputs are:
OR:0,1,1,1
AND:0,0,0,1
NAND:1,1,1,0
NOR:1,0,0,0
XOR:0,1,1,0
Notation. The signs are + for OR, a dot for AND and a bar over a letter for NOT. Do not read + as addition: 1+1=1 in this algebra. The shapes of the symbols above are the ones in most Indian textbooks. Some courses abroad use boxes instead (the IEC symbols), with ≥1 for OR, & for AND and a small circle for NOT. The syllabus of the latest NCERT book has OR, AND, NOT, NAND and NOR. XOR is common in other courses, so we include it.
Rules for working with gates
A+0=AA⋅1=AA+A=1A⋅A=0A=A
De Morgan's rules let us change AND into OR:
A+B=A⋅BA⋅B=A+B
To find the output of a circuit, work from the inputs towards the output, and write the value on every wire. To find the whole truth table, do it for every row.
Universal gates
A universal gate can make every other gate. NAND is universal.
NOT: tie the two inputs of a NAND gate together. A⋅A=A.
AND: a NAND gate followed by a NOT made from a NAND gate. Two gates.
OR: a NOT on each input, then a NAND gate. A⋅B=A+B by De Morgan's rule. Three gates.
Three circuits made only of NAND gates. On the left, a NOT gate: one NAND gate with its two inputs joined together and joined to A, giving the output A bar, labelled 1 NAND gate. In the middle, an AND gate: a NAND gate for A and B whose output goes to both inputs of a second NAND gate, giving A times B, labelled 2 NAND gates. On the right, an OR gate: A goes to both inputs of one NAND gate and B to both inputs of another, and the outputs of these two feed a third NAND gate, giving A plus B, labelled 3 NAND gates.
NOR is universal too, by the same pattern. This matters in practice: a factory can make one kind of gate in millions and build any circuit from it.
A worked example. A circuit has three gates. Gate 1 is an AND gate for A and B. Gate 2 is a NOT gate for C. Gate 3 is an OR gate for the outputs of gates 1 and 2. Find Y for A=1, B=0, C=1.
Gate 1.A⋅B=1⋅0=0.
Gate 2.C=1=0.
Gate 3.0+0=0. So Y=0.
For all eight rows, Y=A⋅B+C is 0 only when C=1 and A⋅B=0.
Choose gate 5 (XOR). Which rows have a big circle? Then choose gate 6 (XNOR). Do the big circles and the small circles swap places?
Practise with new numbers
Try it in Python
The program builds NOT, AND, OR, NOR and XOR from NAND gates only, and compares each one with its definition for all four rows.
Common mistakes
Reading + as addition. In logic, 1+1=1.
Mixing up NAND and NOR. NAND is 0 only for 1,1. NOR is 1 only for 0,0.
Forgetting the bubble. A small circle at the output of a symbol means NOT.
Calling XOR the same as OR. The two differ for 1,1. OR gives 1, XOR gives 0.
Counting the rows wrongly.n inputs give 2n rows, not 2n.
Mixing up the two forms of De Morgan's rule. The bar over the whole expression changes AND into OR and OR into AND.