# Simplifying Boolean Expressions

08/10/10 10:25 Filed in: Computing

The previous post on Boolean logic presented an algorithm for generating an expression consisting of AND and NOT for any given truth table. However, this method clearly did not always give the simplest form for the expression. As just one example, the algorithm gives this result:

Consider the problem of simplifying Boolean expressions. A truth table with

Finding the simplest expression is conceptually simple. For expressions of three variables we can see that the expression that results in f

Create a table E

While conceptually easy, this is computationally hard. Expressions of 4 variables require an expression table with 2

Still, simplifying expressions of up to 4 variables is useful and considering the general solution is an interesting mental exercise.

To determine the total number of equations for expressions with three variables, start by analyzing simpler cases.

With zero variables there are two unique expressions: “0” and “1”.

With one variable there are four expressions: “0”, “1”, “X”, and “(NOT X)”.

Two variables is more interesting. There are the two constant expressions “0” and “1”. Then there are the single variable expressions, with X and Y: “X”, “(NOT X)”, “Y”, “(NOT Y)”. There are 8 expressions of two variables: “(AND X Y)”, “(AND X (NOT Y))”, “(AND (NOT X) Y)” and “(AND (NOT X) (NOT Y))”, and their negations. But that gives only 14 out of the 16 possible expressions. We also have to consider combinations of the expressions of two variables. At most this would be 8

It might be possible to further reduce the number of expressions to be examined. Out of the 2*8

Absent a smarter compiler, is it possible to reduce the gate count even more? The code that searches for the simplest forms of expressions using AND and NOT can also find the simplest form of NAND expressions. One of three versions of E

E

G11 is the sum of the three inputs while G16 is the carry.

Via inspection, the simpler form is (NOT X).x y | t(3)0 0 | 1

0 1 | 1 => (AND (NOT (AND X (NOT Y))) (NOT (AND X Y)))

1 0 | 0

1 1 | 0

Consider the problem of simplifying Boolean expressions. A truth table with

*n*variables results in 2^{2n}expressions as shown in the following figure.Finding the simplest expression is conceptually simple. For expressions of three variables we can see that the expression that results in f

_{7}=f_{6}=f_{5}=f_{4}=f_{3}=f_{2}=f_{1}=f_{0}= 0 is the constant 0. The expression that results in f_{7}..f_{0}= 1 is the constant 1. Then we progress to the functions of a single variable. f_{7}..f_{0}= 10001000 is X. f_{7}..f_{0}= 01110111 is (NOT X). f_{7}..f_{0}= 11001100 is Y and 00110011 is (NOT Y). f_{7}..f_{0}= 10101010 is Z and 01010101 is (NOT Z).Create a table E

_{xyz}of 2^{23}= 256 entries. E_{xyz}(0) = “0”. E_{xyz}(255) = “1”. E_{xyz}(10001000 {i.e. 136}) = “X”. E_{xyz}(01110111 {119}) = “(NOT X)”. E_{xyz}(204) = “Y”, E_{xyz}(51) = “(NOT Y)”, E_{xyz}(170) = “Z” and E_{xyz}(85) is “(NOT Z)”. Assume that this process can continue for the entire table. Then to simplify (or generate) an expression, evaluate f_{7}..f_{0 }and look up the corresponding formula in E_{xyz}.While conceptually easy, this is computationally hard. Expressions of 4 variables require an expression table with 2

^{16}entries and 5 variables requires 2^{32}entries -- not something I’d want to try to compute on any single machine at my disposal. An expression with 8 variables is close to the number of particles in the universe, estimated to be 10^{87}.Still, simplifying expressions of up to 4 variables is useful and considering the general solution is an interesting mental exercise.

To determine the total number of equations for expressions with three variables, start by analyzing simpler cases.

With zero variables there are two unique expressions: “0” and “1”.

With one variable there are four expressions: “0”, “1”, “X”, and “(NOT X)”.

Two variables is more interesting. There are the two constant expressions “0” and “1”. Then there are the single variable expressions, with X and Y: “X”, “(NOT X)”, “Y”, “(NOT Y)”. There are 8 expressions of two variables: “(AND X Y)”, “(AND X (NOT Y))”, “(AND (NOT X) Y)” and “(AND (NOT X) (NOT Y))”, and their negations. But that gives only 14 out of the 16 possible expressions. We also have to consider combinations of the expressions of two variables. At most this would be 8

^{2}combinations times 2 for the negated forms. Since (AND X X) is equivalent to X, this can be reduced to 7+6+5+4+3+2+1 = 28 combinations, times 2 for the negated forms. This gives 56 possibilities for the remaining two formulas, which turn out to be “(AND (NOT (AND (NOT X) (NOT Y))) (NOT (AND X Y)))” and “(AND (NOT (AND X (NOT Y))) (NOT (AND (NOT X) Y)))”.It might be possible to further reduce the number of expressions to be examined. Out of the 2*8

^{2}possible combinations of the two variable forms, there can only be 16 unique values. However, I haven’t given much thought how to do this. In any case, the computer can repeat the process of negating and combining expressions to generate the forms with the fewest number of AND/NOT (or NAND) operators.*E*

_{xyz}(150) is interesting, since this is the expression for the sum part of the adder. The “naive” formula has 21 operators. There are three formulas with 20 operators that give the same result:`(AND (AND (NOT (AND (AND X (NOT Y)) Z)) (NOT (AND X (AND Y (NOT Z)))))`

(NOT (AND (NOT (AND (NOT Y) Z)) (AND (NOT (AND Y (NOT Z))) (NOT X)))))

(AND (AND (NOT (AND (NOT X) (AND Z Y))) (NOT (AND X (AND Y (NOT Z)))))

(NOT (AND (AND (NOT (AND X (NOT Z))) (NOT Y)) (NOT (AND (NOT X) Z)))))

(AND (NOT (AND (AND (NOT Z) (NOT (AND X (NOT Y)))) (NOT (AND (NOT X) Y))))

(AND (NOT (AND (AND X (NOT Y)) Z)) (NOT (AND (NOT X) (AND Z Y)))))

Which one should be used? It depends. A simple nand gate compiler that uses the rules(NOT X) -> (NAND X X)compiles the first and last into 17 gates while the second compiles to 16 gates. Another form of E

(AND X Y) -> temp := (NAND X Y) (NAND temp temp)

_{xyz}(150) compiles to 15 gates. This suggests that the nand gate compiler could benefit from additional optimizations. One possible approach might be to make use of the equality between (NAND X (NAND Y Y)) and (NAND X (NAND X Y)).Absent a smarter compiler, is it possible to reduce the gate count even more? The code that searches for the simplest forms of expressions using AND and NOT can also find the simplest form of NAND expressions. One of three versions of E

_{xyz}(150) is:`(NAND (NAND (NAND (NAND Z Y) (NAND (NAND Y Y) (NAND Z Z))) X)`

(NAND (NAND (NAND (NAND X X) Y) (NAND (NAND X X) Z)) (NAND Z Y)))

This compiles to 12 gates.E

_{xyz}(232), which is the carry equation for the adder, can be written as:`(NAND (NAND (NAND (NAND Y X) (NAND Z X)) X) (NAND Z Y))`

With these simplifications the adder takes ten fewer gates than the first iteration:G11 is the sum of the three inputs while G16 is the carry.

blog comments powered by Disqus