How to build logic gates from other logic gates

Introduction

Here are formulas that tell you how to build various logic gates (e.g. three-input XOR) from two-input NAND gates only or from two-input NOR gates only.

Operands used on this page:

OperandMeaning
A, B, C, Dinput bits of functions
T, U, Vtemporary bits (if the value is needed more than once)

Operators used on this page:

OperatorMeaning
¬bitwise NOT (1 input; e.g. ¬A)
bitwise AND (2 inputs)
bitwise NAND (2 inputs)
bitwise OR (2 inputs)
bitwise NOR (2 inputs)
bitwise XOR (2 inputs)
=assignment of temporary bit (e.g. T = ¬A)

¬ has the highest precedence and = the lowest. That is, T=¬A∧B means T=((¬A)∧B).

Diagram symbols for the NOT, NAND and NOR gates:

US-style diagram symbols for gates: NOT, NAND, NOR

Truth table for the one-input gate:

A ¬A
01
10

Truth tables for the two-input gates; ¬(A⊻B) means A XNOR B:

A B A∧B A⊼B A∨B A⊽B A⊻B ¬(A⊻B)
00010101
01011010
10011010
11101001

Notes:

1-input gates

diagram: a NOT gate built using only 1 NAND gate and using only 1 NOR gate

The truth table shows the output of the function for inputs A=0 and A=1.

Formula Truth table NAND gates only NOR gates only
GatesDelayFormula GatesDelayFormula
¬A 1,0 11A⊼1 11A⊽0
A⊼A A⊽A

2-input gates

The truth tables show the output of the function for all combinations of the inputs: A=0 and B=0; A=0 and B=1; A=1 and B=0; A=1 and B=1.

From now on, the “¬” operator is used in addition to “⊼” and “⊽” to reduce clutter.

The formulas have been grouped in pairs to highlight their similarities: e.g. changing all “⊼” operators to “⊽” in “(A⊼B) ⊼ (¬A⊼¬B)” turns the function from XNOR to XOR.

Order of inputs does not matter

diagram: 2-input gates built using only NOT gates and 2-input NAND gates: NAND, AND, OR, NOR, XOR, XNOR
diagram: 2-input gates built using only NOT gates and 2-input NOR gates: NOR, OR, AND, NAND, XNOR, XOR

Swapping the values of A and B does not affect the result of these functions.

Formula A+B must equal Truth table NAND and NOT gates only NOR and NOT gates only
GatesDelayFormula GatesDelayFormula
A⊼B 0 or 1 1,1,1,0 11A⊼B 43¬(¬A⊽¬B)
A⊽B 0 1,0,0,0 43¬(¬A⊼¬B) 11A⊽B
A∧B 2 0,0,0,1 22¬(A⊼B) 32¬A⊽¬B
A∨B 1 or 2 0,1,1,1 32¬A⊼¬B 22¬(A⊽B)
A⊻B 1 0,1,1,0 43T = A⊼B; (A⊼T)⊼(B⊼T) 53(A⊽B) ⊽ (¬A⊽¬B)
¬(A⊻B) 0 or 2 1,0,0,1 53(A⊼B) ⊼ (¬A⊼¬B) 43T = A⊽B; (A⊽T)⊽(B⊽T)

Order of inputs matters

Swapping the values of A and B may affect the result of these functions.

Formula Truth table NAND and NOT gates only NOR and NOT gates only
GatesDelayFormula GatesDelayFormula
A∨¬B 1,0,1,1 22¬A⊼B 33¬(A⊽¬B)
A∧¬B 0,0,1,0 33¬(A⊼¬B) 22¬A⊽B

3-input gates

The truth tables show the output of the function for all combinations of the inputs: A=0,0,0,0,1,1,1,1; B=0,0,1,1,0,0,1,1; C=0,1,0,1,0,1,0,1.

Order of inputs does not matter

diagram: 3-input gates built using only NOT gates and 2-input NAND gates:
NAND, AND, OR, NOR, XOR, XNOR, 2 or 3 ones, 0 or 1 ones, 1 or 2 ones, 0 or 3 ones, 2 ones, 0 or 1 or 3 ones, 1 one, 0 or 2 or 3 ones
diagram: 3-input gates built using only NOT gates and 2-input NOR gates:
NOR, OR, AND, NAND, XNOR, XOR, 2 or 3 ones, 0 or 1 ones, 0 or 3 ones, 1 or 2 ones, 0 or 2 or 3 ones, 1 one, 0 or 1 or 3 ones, 2 ones

(TODO: redraw diagrams)

Formula A+B+C must equal Truth table NAND and NOT gates only NOR and NOT gates only
GatesDelayFormula GatesDelayFormula
¬(A∧B∧C) not 3 1,1,1,1,1,1,1,0 33¬(A⊼B)⊼C 75¬(¬(¬A⊽¬B) ⊽ ¬C)
¬(A∨B∨C) 0 1,0,0,0,0,0,0,0 75¬(¬(¬A⊼¬B) ⊼ ¬C) 33¬(A⊽B)⊽C
A∧B∧C 3 0,0,0,0,0,0,0,1 44¬(¬(A⊼B) ⊼ C) 64¬(¬A⊽¬B) ⊽ ¬C
A∨B∨C not 0 0,1,1,1,1,1,1,1 64¬(¬A⊼¬B) ⊼ ¬C 44¬(¬(A⊽B) ⊽ C)
(A∧B)∨(A∧C)∨(B∧C) 2 or 3 0,0,0,1,0,1,1,1 64(A⊼B) ⊼ ((¬A⊼¬B) ⊼ C) 64(A⊽B) ⊽ ((¬A⊽¬B) ⊽ C)
¬((A∧B)∨(A∧C)∨(B∧C)) 0 or 1 1,1,1,0,1,0,0,0 73(¬A⊼¬B) ⊼ ((A⊼B)⊼¬C) 73(¬A⊽¬B) ⊽ ((A⊽B)⊽¬C)
¬(¬(A∨B∨C) ∨ (A∧B∧C)) 1 or 2 0,1,1,1,1,1,1,0 74T = ¬B; ((¬A⊼T) ⊼ (A⊼C)) ⊼ (C⊼T) 85T = ¬B; ¬[((¬A⊽T) ⊽ (A⊽C)) ⊽ (C⊽T)]
¬(A∨B∨C) ∨ (A∧B∧C) 0 or 3 1,0,0,0,0,0,0,1 85T = ¬B; ¬[((¬A⊼T) ⊼ (A⊼C)) ⊼ (C⊼T)] 74T = ¬B; ((¬A⊽T) ⊽ (A⊽C)) ⊽ (C⊽T)
¬((A⊻B)⊻C) ∧ (A∨B∨C) 2 0,0,0,1,0,1,1,0 84T = A⊼C; U = B⊼C; [((A⊼B)⊼T) ⊼ U] ⊼ (T⊼¬U) 95T = ¬A⊽¬B; [((A⊽B)⊽T) ⊽ ¬C] ⊽ (C⊽T)
¬((A⊻B)⊻C) ∨ (A∧B∧C) not 1 1,0,0,1,0,1,1,1 95T = ¬A⊼¬B; [((A⊼B)⊼T) ⊼ ¬C] ⊼ (C⊼T) 84T = A⊽C; U = B⊽C; [((A⊽B)⊽T) ⊽ U] ⊽ (T⊽¬U)
(A⊻B)⊻C 1 or 3 0,1,1,0,1,0,0,1 86T = A⊼B; U = (A⊼T)⊼(B⊼T); V = C⊼U; (C⊼V)⊼(U⊼V) 86T = A⊽B; U = (A⊽T)⊽(B⊽T); V = C⊽U; (C⊽V)⊽(U⊽V)
¬((A⊻B)⊻C) 0 or 2 1,0,0,1,0,1,1,0 96T = A⊼B; U = (A⊼T)⊼(B⊼T); (C⊼U)⊼(¬C⊼¬U) 96T = A⊽B; U = (A⊽T)⊽(B⊽T); (C⊽U)⊽(¬C⊽¬U)
((A⊻B)⊻C) ∨ ¬(A∨B∨C) not 2 1,1,1,0,1,0,0,1 84T = A⊼B; [(B⊼T) ⊼ (A⊼(B⊼C))] ⊼ (¬C⊼T) 105T = ¬A; U = ¬B; V = C⊽(T⊽U); [((A⊽U)⊽(B⊽T)) ⊽ V] ⊽ (C⊽V)
((A⊻B)⊻C) ∧ ¬(A∧B∧C) 1 0,1,1,0,1,0,0,0 105T = ¬A; U = ¬B; V = C⊼(T⊼U); [((A⊼U)⊼(B⊼T)) ⊼ V] ⊼ (C⊼V) 84T = A⊽B; [(B⊽T) ⊽ (A⊽(B⊽C))] ⊽ (¬C⊽T)

Note: I may have missed optimal solutions for the most complex formulas because these are the largest exhaustive searches I have made:

That is, there may be a solution with 8 NAND gates for ¬((A⊻B)⊻C).

Order of inputs matters

Formula Truth table NAND and NOT gates only NOR and NOT gates only
GatesDelayFormula GatesDelayFormula
(A∧B)∨¬C 1,0,1,0,1,0,1,1 22(A⊼B)⊼C 43T = ¬C; (A⊽T)⊽(B⊽T)
(A∨B)∧¬C 0,0,1,0,1,0,1,0 43T = ¬C; (A⊼T)⊼(B⊼T) 22(A⊽B)⊽C
(A∧B)∨C 0,1,0,1,0,1,1,1 32(A⊼B)⊼¬C 32(A⊽C)⊽(B⊽C)
(A∨B)∧C 0,0,0,1,0,1,0,1 32(A⊼C)⊼(B⊼C) 32(A⊽B)⊽¬C
(A∧¬B)∨¬C 1,0,1,0,1,1,1,0 33(A⊼¬B)⊼C 53T = ¬C; (A⊽T)⊽(¬B⊽T)
(A∨¬B)∧¬C 1,0,0,0,1,0,1,0 53T = ¬C; (A⊼T)⊼(¬B⊼T) 33(A⊽¬B)⊽C
(A∧¬B)∨C 0,1,0,1,1,1,0,1 43(A⊼¬B)⊼¬C 43(A⊽C)⊽(¬B⊽C)
(A∨¬B)∧C 0,1,0,0,0,1,0,1 43(A⊼C)⊼(¬B⊼C) 43(A⊽¬B)⊽¬C
A∨¬B∨¬C 1,1,1,0,1,1,1,1 43¬A⊼¬(B⊼C) 65¬(¬(A⊽¬B)⊽¬C)
A∧¬B∧¬C 0,0,0,0,1,0,0,0 65¬(¬(A⊼¬B)⊼¬C) 43¬A⊽¬(B⊽C)
A∧B∧¬C 0,0,0,0,0,0,1,0 54¬(¬(A⊼B)⊼¬C) 54¬(¬A⊽¬B)⊽C
A∨B∨¬C 1,0,1,1,1,1,1,1 54¬(¬A⊼¬B)⊼C 54¬(¬(A⊽B)⊽¬C)

4-input gates

The truth tables show the output of the function for all combinations of the inputs: A=0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1; B=0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1; C=0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1; D=0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1. These formulas may not be optimal.

Order of inputs does not matter

diagram: 4-input gates built using only NOT gates and 2-input NAND gates: NAND, AND, OR, NOR
diagram: 4-input gates built using only NOT gates and 2-input NOR gates: NOR, OR, AND, NAND

(TODO: redraw diagrams)

Formula A+B+C+D must equal Truth table NAND and NOT gates only NOR and NOT gates only
GatesDelayFormula GatesDelayFormula
¬(A∧B∧C∧D) not 4 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 53¬(A⊼B)⊼¬(C⊼D) 105¬(¬(¬A⊽¬B)⊽¬(¬C⊽¬D))
¬(A∨B∨C∨D) 0 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 105¬(¬(¬A⊼¬B)⊼¬(¬C⊼¬D)) 53¬(A⊽B)⊽¬(C⊽D)
A∧B∧C∧D 4 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 64¬(¬(A⊼B)⊼¬(C⊼D)) 94¬(¬A⊽¬B)⊽¬(¬C⊽¬D)
A∨B∨C∨D not 0 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 94¬(¬A⊼¬B)⊼¬(¬C⊼¬D) 64¬(¬(A⊽B)⊽¬(C⊽D))

Order of inputs matters

Formula Truth table NAND and NOT gates only NOR and NOT gates only
GatesDelayFormula GatesDelayFormula
¬(A∧B∧C∧¬D) 1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1 64¬(A⊼B) ⊼ ¬(C⊼¬D) 95¬(¬(¬A⊽¬B) ⊽ ¬(¬C⊽D))
¬(A∨B∨C∨¬D) 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 95¬(¬(¬A⊼¬B) ⊼ ¬(¬C⊼D)) 64¬(A⊽B) ⊽ ¬(C⊽¬D)
A∨B∨¬C∨¬D 1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1 74¬(¬A⊼¬B) ⊼ ¬(C⊼D) 85¬(¬(A⊽B) ⊽ ¬(¬C⊽¬D))
A∧B∧¬C∧¬D 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0 85¬(¬(A⊼B) ⊼ ¬(¬C⊼¬D)) 74¬(¬A⊽¬B) ⊽ ¬(C⊽D)
A∧B∧C∧¬D 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 75¬(¬(A⊼B) ⊼ ¬(C⊼¬D)) 84¬(¬A⊽¬B) ⊽ ¬(¬C⊽D)
A∨B∨C∨¬D 1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1 84¬(¬A⊼¬B) ⊼ ¬(¬C⊼D) 75¬(¬(A⊽B) ⊽ ¬(C⊽¬D))

RPN calculator

This command-line Python program accepts a formula in Reverse Polish notation (RPN) and prints the corresponding truth table. In RPN, operands precede their operator; the advantage is that parentheses are not needed. For example, A¬B¬⊼¬ is equivalent to ¬((¬A)⊼(¬B)) in ordinary notation. The truth table consists of output bits of the formula when it is evaluated for every possible combination of the input bits.

Command line arguments:

  1. number of input bits (1–8)
  2. formula

The program has an internal stack. For each evaluation of the formula, the stack starts empty and is affected by operands and operators. The stack must contain exactly one bit at the end of the formula. You can't pop from an empty stack.

The formula may include these symbols:

E.g. python3 rpncalc.py 2 "A¬B¬⊼¬" will print 1,0,0,0.