Skip to content

2.3 Minimization goals and gate cost

Why this matters

Two circuits can compute the same function and still differ a lot in size, speed and cost. Before you can minimize anything you have to decide what you are minimizing, and the answer depends on what the circuit is made of. This section fixes the measures, gives a cost model you can compute with, and shows that a single kind of gate, NAND or NOR, is enough to build everything.

  • Name the four things a minimization can try to reduce: gates, gate inputs, levels and gate kinds, and say how the gate-input count differs from an expression's literal count.
  • Compute the transistor cost of a circuit of two-input gates from a cost table.
  • Explain why circuits with the same number of gates can differ in cost.
  • Rebuild NOT, AND and OR from NAND gates alone and from NOR gates alone.
  • Count the gates of a NAND-only or NOR-only circuit for a given function.

Think of a function as a recipe and a circuit as a kitchen that cooks it. Two kitchens can serve the same dish, one with five stations and one with two. The smaller kitchen needs less space and fewer cooks, and a dish passing through fewer stations arrives sooner. A third kitchen may use fewer stations but expensive ones. Minimizing a circuit means choosing among such kitchens, and you cannot choose until you know whether you care about space, speed, or the price of the equipment.

In circuits the recipe is a Boolean function and the equipment is gates. The laws of the previous two sections give you many circuits for one function, and this section gives you the yardsticks to compare them.

Definition 2.3.1(measures of a circuit).

A circuit can be measured in four ways. Its gate count is the number of gates. Its gate-input count is the total number of inputs on the gates of that particular circuit, which is also the number of wire ends that land on a gate. Its levels, or depth, is the largest number of gates on a path from an input to the output. Its gate kinds are the types of gate it uses, which matter because some kinds are cheaper than others.

The gate-input count is not the literal count of Section 2.1, which is a property of an expression: the number of variable occurrences in it, plain or complemented. The two are related but different. The expression AB+CDAB + CD has four literals, while its direct circuit of two AND gates and one OR gate has six gate inputs, two on each gate. When you quote a gate-input count, say whether the inputs of inverters are counted.

Each measure matters for a reason. Every extra gate costs chip area and, in a packaged part, money. In some technologies a gate with more inputs is slower, and the number of inputs a gate can have is limited, so fewer inputs per gate is better. A signal must pass through every gate on its path, so fewer levels means a shorter delay. And since the gate kinds differ in how they are built, a circuit with the same number of gates can be cheaper or dearer than another. None of the four measures decides everything, and a circuit that is best in one can be worse in another.

The course measures a two-input gate by the number of CMOS transistors it needs: NOT 2, AND 6, OR 6, NAND 4, NOR 4, XOR 8 and XNOR 8. (fact ch02/gate-cost-model) We use that model throughout, with one condition that is easy to forget: the numbers are for gates with two inputs. A three-input gate usually costs more, and the model gives no number for it. A NOT has one input, and its cost of 2 is the smallest in the list.

The table has a pattern. A NAND is the cheapest two-input gate, and an AND is a NAND followed by an inverter, which gives 4 plus 2 equals 6. The same holds for OR and NOR. So NAND and NOR are the natural gates of this technology, and a plain AND or OR pays for an extra inverter.

ReadingThe numbers depend on the circuit style

A static CMOS exclusive-or gate is commonly drawn with 12 transistors when it must make the inverted inputs itself, and with 8 when they are already available; transmission-gate designs use 8 or fewer. (fact ch02/xor-cmos-12) So the 8 for XOR in the table is a number for one particular design, not a law. The model is a way to compare circuits on paper. A real design flow counts transistors, or more usually other resources, with its own tools.

Example 2.3.1(costing the circuits from Section 2.1).

The five-gate circuit of Section 2.1 has three AND gates and two OR gates, all with two inputs. Each costs 6, so the circuit costs 30. The simplified circuit has one OR gate and one AND gate and costs 12. Both circuits are below, drawn from the same checked drawings as before.

Schematic ch02/s01-ex1-circuitABCQ

A logic schematic with inputs A, B and C and output Q.

AND gate g1: inputs A and B; output n1.

OR gate g2: inputs B and C; output n2.

AND gate g3: inputs C and B; output n3.

AND gate g4: inputs n2 and n3; output n4.

OR gate g5: inputs n1 and n4; output Q.

It computes Q = A and B or B and C and (B or C).

Description: A circuit with inputs A, B and C and output Q built from three AND gates and two OR gates.

Figure 2.3.1. The original circuit: three AND gates and two OR gates.
Schematic ch02/s01-ex1-minACBQ

A logic schematic with inputs A, C and B and output Q.

OR gate g1: inputs A and C; output n1.

AND gate g2: inputs n1 and B; output Q.

It computes Q = B and (A or C).

Description: A circuit with inputs A, B and C and output Q: an OR gate on A and C feeds an AND gate whose other input is B.

Figure 2.3.2. The simplified circuit: one OR gate and one AND gate.

Simplifying also cut the levels. The longest path in the first circuit passes through three gates and the longest in the second through two. Both numbers come from the drawings, and the quiz below asks you to check them. All three measures improved at once here. They do not always.

Example 2.3.2(same shape, different kinds).

Take AB+CDAB + CD. An AND-OR circuit uses two AND gates and one OR gate, which costs 6 + 6 + 6 = 18. Replace all three gates by NAND gates and the function does not change, and the cost is 4 + 4 + 4 = 12. The circuits have the same number of gates, the same number of literals and the same number of levels. Only the gate kind differs. Section 2.4 explains why replacing every gate by a NAND is allowed here.

Schematic ch02/andorABCDY

A logic schematic with inputs A, B, C and D and output Y.

AND gate g1: inputs A and B; output n1.

AND gate g2: inputs C and D; output n2.

OR gate g3: inputs n1 and n2; output Y.

It computes Y = A and B or C and D.

Description: A circuit with inputs A, B, C and D and output Y: two AND gates, one on A and B and one on C and D, feed an OR gate.

Figure 2.3.3. An AND-OR circuit for AB + CD.
Schematic ch02/andor-nandABCDY

A logic schematic with inputs A, B, C and D and output Y.

NAND gate g1: inputs A and B; output n1.

NAND gate g2: inputs C and D; output n2.

NAND gate g3: inputs n1 and n2; output Y.

It computes Y = A and B or C and D.

Description: A circuit with inputs A, B, C and D and output Y: two NAND gates, one on A and B and one on C and D, feed a third NAND gate.

Figure 2.3.4. The same function from three NAND gates.
Definition 2.3.2(universal gate).

A gate kind is universal if every Boolean function can be built from gates of that kind alone. The lecture calls NAND and NOR the universal gates. (fact ch02/universal-gates)

To show that NAND is universal it is enough to build NOT, AND and OR from it, since every expression is made from those three. A NAND whose two inputs are tied together is a NOT, because X‾=XX‾\overline{X} = \overline{XX}. An AND is a NAND followed by that NOT, because XY=XY‾‾XY = \overline{\overline{XY}}. An OR is a NAND of two inverted inputs, because X+Y=X‾ Y‾‾X + Y = \overline{\overline{X}\,\overline{Y}}. The last identity is one of DeMorgan's laws, which Section 2.4 covers.

Schematic ch02/universal-nandnot_anot_yand_aand_band_yor_aor_bor_y

Only NAND gates (6 NAND).

A logic schematic with inputs not_a, and_a, and_b, or_a and or_b and outputs not_y, and_y and or_y.

NAND gate g_not: inputs not_a and not_a; output not_y.

NAND gate g_a: inputs and_a and and_b; output n1.

NAND gate n1: inputs or_a and or_a; output n2.

NAND gate n2: inputs or_b and or_b; output n3.

NAND gate g_i: inputs n1 and n1; output and_y.

NAND gate g3: inputs n2 and n3; output or_y.

It computes not_y = not not_a.

It computes and_y = and_a and and_b.

It computes or_y = or_a or or_b.

Description: Three small circuits made only of NAND gates. The first is one NAND gate with both inputs connected to the same input. The second is a NAND gate followed by a NAND gate with tied inputs. The third is two NAND gates with tied inputs feeding a third NAND gate.

Figure 2.3.5. NOT, AND and OR built from NAND gates only, from top to bottom: one gate, two gates and three gates.

The same idea works for NOR, with the roles of AND and OR swapped. A NOR with tied inputs is a NOT, because X‾=X+X‾\overline{X} = \overline{X + X}. An OR is a NOR followed by that NOT, because X+Y=X+Y‾‾X + Y = \overline{\overline{X + Y}}. An AND is a NOR of two inverted inputs, because XY=X‾+Y‾‾XY = \overline{\overline{X} + \overline{Y}}. Figure 2.3.6 shows the three constructions.

Schematic ch02/universal-nornot_anot_yor_aor_bor_yand_aand_band_y

Only NOR gates (6 NOR).

A logic schematic with inputs not_a, or_a, or_b, and_a and and_b and outputs not_y, or_y and and_y.

NOR gate g_not: inputs not_a and not_a; output not_y.

NOR gate g_a: inputs or_a and or_b; output n1.

NOR gate n1: inputs and_a and and_a; output n2.

NOR gate n2: inputs and_b and and_b; output n3.

NOR gate g_i: inputs n1 and n1; output or_y.

NOR gate g3: inputs n2 and n3; output and_y.

It computes not_y = not not_a.

It computes and_y = and_a and and_b.

It computes or_y = or_a or or_b.

Description: Three small circuits made only of NOR gates. The first is one NOR gate with both inputs connected to the same input. The second is a NOR gate followed by a NOR gate with tied inputs. The third is two NOR gates with tied inputs feeding a third NOR gate.

Figure 2.3.6. NOT, OR and AND built from NOR gates only, from top to bottom: one gate, two gates and three gates.

Universal does not mean cheaper. Under the cost model, a NOT made from a NAND costs 4 against 2 for a real NOT, an AND from two NANDs costs 8 against 6, and an OR from three NANDs costs 12 against 6. Rebuilding gate by gate is the safe way to convert a circuit, not the efficient way. A smarter conversion rewrites the expression first, as the next example shows.

Example 2.3.3(B(A + C) in one kind of gate).

We want to implement B⋅(A+C)B \cdot (A + C) using as few gates as possible, all NAND or all NOR.

Start with the direct translation: the OR gate becomes three NANDs (an inverter on each of A and C, then a NAND), the AND gate becomes two (a NAND and an inverter). That gives five gates and a cost of 20.

Schematic ch02/ex3-nand-directACBF

Only NAND gates (5 NAND). 5 gates in all.

A logic schematic with inputs A, C and B and output F.

NAND gate n_a: inputs A and A; output n1.

NAND gate n_c: inputs C and C; output n2.

NAND gate g_or: inputs n1 and n2; output n3.

NAND gate g_and: inputs n3 and B; output n4.

NAND gate g_inv: inputs n4 and n4; output F.

It computes F = B and (A or C).

Description: A circuit with inputs A, C and B and output F made of five NAND gates: two with tied inputs on A and on C, a NAND on their outputs, a NAND of that output and B, and a final NAND with tied inputs.

Figure 2.3.7. B(A + C) translated gate by gate into NAND gates: five gates.

Rewriting first does much better. Multiply out to get AB+BCAB + BC. That is an AND-OR circuit, and an AND-OR circuit becomes a NAND-NAND circuit directly, so three NAND gates suffice, with a cost of 12.

Schematic ch02/ex3-nandABCF

Only NAND gates (3 NAND). 3 gates in all.

A logic schematic with inputs A, B and C and output F.

NAND gate g1: inputs A and B; output n1.

NAND gate g2: inputs B and C; output n2.

NAND gate g3: inputs n1 and n2; output F.

It computes F = B and (A or C).

Description: A circuit with inputs A, B and C and output F made of three NAND gates: one on A and B, one on B and C, and a third NAND gate whose inputs are their outputs.

Figure 2.3.8. B(A + C) as AB + BC in three NAND gates.

For NOR, the original form is already close. The OR of A and C is one NOR followed by an inverter. But the NOR of two inverted signals is an AND, so we can drop that inverter and invert B instead: B⋅(A+C)=B‾+A+C‾‾B \cdot (A + C) = \overline{\overline{B} + \overline{A + C}}. This needs a NOR for A and C, a NOR with tied inputs for the inverted B, and a final NOR, which is three gates and a cost of 12.

Schematic ch02/ex3-norACBF

Only NOR gates (3 NOR). 3 gates in all.

A logic schematic with inputs A, C and B and output F.

NOR gate g1: inputs A and C; output n1.

NOR gate nb: inputs B and B; output n2.

NOR gate g2: inputs n1 and n2; output F.

It computes F = B and (A or C).

Description: A circuit with inputs A, C and B and output F made of three NOR gates: one on A and C, one with both inputs connected to B, and a third NOR gate whose inputs are their outputs.

Figure 2.3.9. B(A + C) in three NOR gates.

Three gates is the smallest count for this function in either kind: an exhaustive search over every circuit of two or fewer two-input NAND gates, and over every one of NOR gates, with A, B and C as the only inputs, finds none that computes it. The plain OR-and-AND circuit also costs 12 under the model, so converting to a universal gate did not make this one cheaper, only uniform.

Gate counts also change with the function. Exclusive-or needs four NAND gates, shown below, which cost 16 against 8 for an XOR gate in the model.

Schematic ch02/xor-four-nandABF

Only NAND gates (4 NAND). 4 gates in all.

A logic schematic with inputs A and B and output F.

NAND gate g1: inputs A and B; output n1.

NAND gate g2: inputs A and n1; output n2.

NAND gate g3: inputs n1 and B; output n3.

NAND gate g4: inputs n2 and n3; output F.

It computes F = A xor B.

Description: A circuit with inputs A and B and output F made of four NAND gates: the first NAND takes A and B, the second takes A and the first gate's output, the third takes the first gate's output and B, and the fourth takes the outputs of the second and third.

Figure 2.3.10. Exclusive-or from four NAND gates. The first gate's output is shared by the other two.

How CSE 369 writes this

Gates in drawings have distinctive shapes, and a bubble means NOT, on an output or on an input. A NAND is an AND with a bubble on the output, and a NOR is an OR with a bubble on the output. The cost table above is for two-input gates; this book never applies it to a gate with more inputs.

Common pitfall

  • Treating the cost table as a law of nature. It counts transistors of two-input gates in one circuit style, and real counts depend on the style.
  • Forgetting the inverters. In a NAND-only circuit every inversion is a NAND with tied inputs, and it counts as a gate and costs 4.
  • Judging cost by gate count alone. The AND-OR and NAND-NAND circuits for AB + CD have the same gate count but cost 18 and 12.
  • Leaving a NOT or an input bubble in a drawing that is supposed to use only NAND or only NOR gates. Every inversion must be made by one of the allowed gates.
  • Calling a circuit the smallest without trying other forms of the function. The expression you start from decides which circuits you find: B(A + C) took five NAND gates directly and three after rewriting.
Take the quiz (4 questions)

How many NAND gates do the three constructions in the NAND figure use in total?

How many gates does the direct NAND translation of B(A + C) use?

How many levels does the original five-gate circuit of Section 2.1 have?

How many NAND gates does the NAND-NAND circuit for AB + CD use? Multiply by the cost of a NAND to get its transistor count.

Exercise 2.3.1

Use the cost table to find the transistor count of the four-NAND exclusive-or circuit and compare it with one XOR gate.

Hint
Count the gates in the figure and multiply by the cost of one NAND.
Solution

The circuit has four NAND gates, and each costs 4, so the total is 16. A single XOR gate costs 8 in the model, so here the gate that the model offers directly is half the cost of the rebuilt one. The model's 8 is for one circuit style, though: a different style gives a different number, and so the comparison is only as good as the model.

Tags: Warm-upGates and schematics

Exercise 2.3.2

Draw NOT, OR and AND using only NOR gates, and say how many gates each takes.

Hint
A NOR with its inputs tied is a NOT. For OR, undo a NOR. For AND, invert both inputs of a NOR.
Solution

NOT is one NOR with both inputs connected to the input. OR is a NOR followed by a NOT, so two gates. AND is a NOR of two inverted inputs, so one NOR plus two NOR inverters, three gates. The figure shows all three, drawn and checked.

Schematic ch02/universal-nornot_anot_yor_aor_bor_yand_aand_band_y

Only NOR gates (6 NOR).

A logic schematic with inputs not_a, or_a, or_b, and_a and and_b and outputs not_y, or_y and and_y.

NOR gate g_not: inputs not_a and not_a; output not_y.

NOR gate g_a: inputs or_a and or_b; output n1.

NOR gate n1: inputs and_a and and_a; output n2.

NOR gate n2: inputs and_b and and_b; output n3.

NOR gate g_i: inputs n1 and n1; output or_y.

NOR gate g3: inputs n2 and n3; output and_y.

It computes not_y = not not_a.

It computes and_y = and_a and and_b.

It computes or_y = or_a or or_b.

Description: Three small circuits made only of NOR gates. The first is one NOR gate with both inputs connected to the same input. The second is a NOR gate followed by a NOR gate with tied inputs. The third is two NOR gates with tied inputs feeding a third NOR gate.

Figure 2.3.11. The NOR constructions, from top to bottom: NOT with one gate, OR with two gates and AND with three gates.

Tags: StandardGates and schematics

Exercise 2.3.3

Implement A+BCA + BC with NAND gates only, and then with NOR gates only. How many gates does each use?

Hint
For NAND, write it as a NAND of two things. For NOR, use the OR-over-AND distributive law to turn it into a product of sums.
Solution

For NAND, the OR of A and B&C is a NAND of the inverted pieces: A+BC=A‾⋅BC‾‾A + BC = \overline{\overline{A} \cdot \overline{BC}}. The inverted A is a NAND with tied inputs, the inverted product is a NAND of B and C, and the outer gate is one more NAND. That is three gates.

Schematic ch02/s03-nand-solABCF

Only NAND gates (3 NAND). 3 gates in all.

A logic schematic with inputs A, B and C and output F.

NAND gate n_a: inputs A and A; output n1.

NAND gate g1: inputs B and C; output n2.

NAND gate g2: inputs n1 and n2; output F.

It agrees with the function logic:ch02/s03-ex-or-and#F.

Description: A circuit with inputs A, B and C and output F made of three NAND gates: one with both inputs connected to A, one on B and C, and a third whose inputs are their outputs.

Figure 2.3.12. A + BC in three NAND gates.

For NOR, the distributive law gives the product of sums (A+B)⋅(A+C)(A + B) \cdot (A + C). A product of sums is the NOR of the NORs of its sums: (A+B)⋅(A+C)(A + B) \cdot (A + C) =A+B‾+A+C‾‾= \overline{\overline{A + B} + \overline{A + C}}. That is also three gates.

Schematic ch02/s03-nor-solABCF

Only NOR gates (3 NOR). 3 gates in all.

A logic schematic with inputs A, B and C and output F.

NOR gate g1: inputs A and B; output n1.

NOR gate g2: inputs A and C; output n2.

NOR gate g3: inputs n1 and n2; output F.

It agrees with the function logic:ch02/s03-ex-or-and#F.

Description: A circuit with inputs A, B and C and output F made of three NOR gates: one on A and B, one on A and C, and a third whose inputs are their outputs.

Figure 2.3.13. A + BC in three NOR gates.

Tags: StandardGates and schematicsBoolean algebra

Exercise 2.3.4

Show with a derivation that X+YX + Y can be written using only NAND operations, and find which part of your result is a NOT.

Hint
Put two bars on the whole expression, then push the inner bar through the sum with DeMorgan's law.
Solution

Each line follows from the one above by the law beside it.

Step-by-step simplification; each line names the law used to get from the line above
ExpressionLaw used
X+YX + Y
=X+Y‾‾= \overline{\overline{X + Y}}Double negation
=X‾ Y‾‾= \overline{\overline{X}\,\overline{Y}}DeMorgan

The last line is the NAND of X‾\overline{X} and Y‾\overline{Y}. Each X‾\overline{X} is a NAND of X with itself, so the circuit uses three NAND gates: two inverters and the final NAND.

Tags: StandardBoolean algebra

Exercise 2.3.5

Build the exclusive-nor of A and B, which is 1 when A and B are equal, from NAND gates only. How many gates does your circuit use, and what does it cost under the table?

Hint
The exclusive-nor is the complement of the exclusive-or, and you already have a NAND circuit for the exclusive-or.
Solution

Take the four-gate exclusive-or and add a NAND whose two inputs are both connected to the exclusive-or's output, which inverts it. That gives five NAND gates and a cost of 20, against 8 for an XNOR gate in the model.

Schematic ch02/xnor-five-nandABF

Only NAND gates (5 NAND). 5 gates in all.

A logic schematic with inputs A and B and output F.

NAND gate g1: inputs A and B; output n1.

NAND gate g2: inputs A and n1; output n2.

NAND gate g3: inputs n1 and B; output n3.

NAND gate g4: inputs n2 and n3; output n4.

NAND gate g5: inputs n4 and n4; output F.

It computes F = not (A xor B).

Description: A circuit with inputs A and B and output F: the four NAND gates of the exclusive-or circuit followed by a fifth NAND gate with both inputs connected to the fourth gate's output.

Figure 2.3.14. Exclusive-nor from five NAND gates.

Tags: ChallengeGates and schematics