# Boolean Logic

08/08/10 08:57 PM Filed in: Computing | Natural Theology

I have a BS in applied math and I’m appalled at what I wasn’t taught. I learned about truth tables, the logical operators AND, OR, NOT, EXCLUSIVE-OR, IMPLIES, and EQUIVALENT. I know De Morgan’s rules and in 1977 I wrote a Pascal program to read an arbitrary logical expression and print out the truth table for it. I was dimly aware of NAND and NOR. I think I knew that any logical operation could be written using NAND (or NOR) exclusively, but I didn’t know why. Perhaps that’s the life of a software engineer.

Consider Boolean expressions of two variables; call them x and y. Each variable can take on two values, 0 and 1, so there are 4 possible inputs and 4 possible outputs. Four possible outputs gives a total of 16 different outcomes, as the following tables, labeled t(0) to t(15), show. The tables are ordered so that each table in a row is the complement of the other table. This will be useful in exploiting symmetry when we start writing logical expressions for each table. Note that for each t(n), the value in the first row corresponds to bit 0 of n, the second row is bit 1, and so on.

We can make some initial observations.

t(8) = (AND

t(9) = (EQUIVALENT

t(10) =

t(11) =(IMPLIES

t(12) =

t(13) is a function I’m not familiar with. The Turing Omnibus says that it’s the “reverse implication” function, which is patently obvious since it’s (IMPLIES

t(14) = (OR

t(15) = 1

What I never noticed before is that all of the common operations: AND, OR, NOT, IMPLIES, and EQUIVALENCE are grouped together. EXCLUSIVE-OR is the only “common” operation on the other side. Is this an artifact of the way our minds are wired to think: that we tend to define things in terms of

In any case, the other table entries follow by negation:

t(0) = 0

t(1) = (NOT (OR

t(2) = (NOT (IMPLIES

t(3) = (NOT

t(4) = (NOT (IMPLIES

t(5) = (NOT

t(6) = (EXCLUSIVE-OR

t(7) = (NOT (AND

All of these functions can be expressed in terms of NOT, AND, and OR as will be shown in a subsequent table. t(0) = 0 can be written as (AND

For each row with a zero result in a particular table, create a function

What about the rows that evaluate to one? Suppose one such row is denoted by

Converting to NOT/OR form uses the same idea. For each row with a one result in a particular table, create a function

The application of this algorithm yields the following formulas. Note that the algorithm gives a non-optimal result for t(0), which is more simply written as (AND X (NOT X)). Perhaps this is not a fair comparison, since the algorithm is generating a function of two variables, when one will do. More appropriately, t(1) is equivalent to (AND (NOT X) (NOT Y)). So there is a need for simplifying expressions, which will mostly be ignored for now.

(AND

These two transformations allow t(0) through t(15) to be expressed solely in terms of NAND.

Putting everything together, we have the following tables of identities. There is some organization to the ordering: first, the commonly defined function. Next, the AND/NOT form. Then the negation of the complementary form in those cases where it makes sense. Then a NAND form and, lastly, an alternate OR form. No effort was made to determine if any formula was in its simplest form. All of these equations have been machine checked. That’s one reason why they are in LISP notation.

Let’s make an overly long post even longer. Since we can do any logical operation using NAND, and since I’ve never had any classes in digital hardware design, let’s go ahead and build a 4-bit adder. The basic high-level building block will be a device that has three inputs:

Substituting (X, Y, Z) for (a, b, c) the NOT/AND forms are

The NAND forms for t(sum) and t(carry) are monstrous. The conversions contain a great deal of redundancy since (AND X Y) becomes (NAND (NAND x y) (NAND x y)).

However, symmetry will help a little bit. t(sum) = t(#x96) = (not t(not #x96)) =

The complexity can be tamed with mechanical substitution and the use of “variables”:

let G0 = (NAND X X)

let G1 = (NAND Y Y)

let G2 = (NAND G1 Z)

let G3 = (NAND G2 G2)

let G4 = (NAND G0 G3)

let G5 = (NAND Z Z)

let G6 = (NAND Y G5)

let G7 = (NAND G6 G6)

let G8 = (NAND G0 G7)

let G9 = (NAND G1 G5)

let G10 = (NAND G9 G9)

let G11 = (NAND X G10)

let G12 = (NAND Y Z)

let G13 = (NAND G12 G12)

let G14 = (NAND X G13)

let G15 = (NAND G11 G14)

let G16 = (NAND G15 G15)

let G17 = (NAND G8 G16)

let G18 = (NAND G17 G17)

t(sum) = (NAND G4 G18)

The same kind of analysis can be done with the NAND form of the carry. The carry has a number of gates in common with the summation. Putting everything together, the circuitry for the adder would look something like this. Ignoring, of course, the real world where I’m sure there are issues involved with circuit layout. The output of the addition is the red (rightmost bottom) gate while the output of the carry is the last green (rightmost top) gate. The other green gates are those which are unique to the carry. The diagram offends my aesthetic sense with the crossovers, multiple inputs, and choice of colors. My apologies to those of you who may be color blind.

What took me a few hours to do with a computer must have taken thousands of man-hours to do without a computer. I may share the code I developed while writing this blog entry in a later post. The missing piece is simplification of logical expressions and I haven’t yet decided if I want to take the time to add that.

Consider Boolean expressions of two variables; call them x and y. Each variable can take on two values, 0 and 1, so there are 4 possible inputs and 4 possible outputs. Four possible outputs gives a total of 16 different outcomes, as the following tables, labeled t(0) to t(15), show. The tables are ordered so that each table in a row is the complement of the other table. This will be useful in exploiting symmetry when we start writing logical expressions for each table. Note that for each t(n), the value in the first row corresponds to bit 0 of n, the second row is bit 1, and so on.

x y | t(0)x y | t(15)0 0 | 0 0 0 | 1

0 1 | 0 0 1 | 1

1 0 | 0 1 0 | 1

1 1 | 0 1 1 | 1x y | t(1)x y | t(14)0 0 | 1 0 0 | 0

0 1 | 0 0 1 | 1

1 0 | 0 1 0 | 1

1 1 | 0 1 1 | 1x y | t(2)x y | t(13)0 0 | 0 0 0 | 1

0 1 | 1 0 1 | 0

1 0 | 0 1 0 | 1

1 1 | 0 1 1 | 1x y | t(3)x y | t(12)0 0 | 1 0 0 | 0

0 1 | 1 0 1 | 0

1 0 | 0 1 0 | 1

1 1 | 0 1 1 | 1x y | t(4)x y | t(11)0 0 | 0 0 0 | 1

0 1 | 0 0 1 | 1

1 0 | 1 1 0 | 0

1 1 | 0 1 1 | 1x y | t(5)x y | t(10)0 0 | 1 0 0 | 0

0 1 | 0 0 1 | 1

1 0 | 1 1 0 | 0

1 1 | 0 1 1 | 1x y | t(6)x y | t(9)0 0 | 0 0 0 | 1

0 1 | 1 0 1 | 0

1 0 | 1 1 0 | 0

1 1 | 0 1 1 | 1x y | t(7)x y | t(8)0 0 | 1 0 0 | 0

0 1 | 1 0 1 | 0

1 0 | 1 1 0 | 0

1 1 | 0 1 1 | 1

We can make some initial observations.

t(8) = (AND

*x y*)t(9) = (EQUIVALENT

*x y*).t(10) =

*y*t(11) =(IMPLIES

*x y*), which is equivalent to (OR (NOT*x*)*y*)t(12) =

*x*.t(13) is a function I’m not familiar with. The Turing Omnibus says that it’s the “reverse implication” function, which is patently obvious since it’s (IMPLIES

*y x*).t(14) = (OR

*x y*)t(15) = 1

What I never noticed before is that all of the common operations: AND, OR, NOT, IMPLIES, and EQUIVALENCE are grouped together. EXCLUSIVE-OR is the only “common” operation on the other side. Is this an artifact of the way our minds are wired to think: that we tend to define things in terms of

*x*instead of (NOT*x)? Are we wired to favor some type of computational simplicity? Nature is “lazy," that is, she conserves energy and our mental computations require energy.*In any case, the other table entries follow by negation:

t(0) = 0

t(1) = (NOT (OR

*x y*)), which is equivalent to (NOR*x y*).t(2) = (NOT (IMPLIES

*y x*))t(3) = (NOT

*x*)t(4) = (NOT (IMPLIES

*x y*))t(5) = (NOT

*y*).t(6) = (EXCLUSIVE-OR

*x y*), or (NOT (EQUIVALENT*x y*))t(7) = (NOT (AND

*x y*)), also known as (NAND*x y*)All of these functions can be expressed in terms of NOT, AND, and OR as will be shown in a subsequent table. t(0) = 0 can be written as (AND

*x*(NOT x)). t(15) = 1 can be written as (OR*x*(NOT*x*)). The Turing Omnibus gives a method for expressing each table in terms of NOT and AND:For each row with a zero result in a particular table, create a function

*(*AND*(f x) (g y))*where*f*and*g*__evaluate to one__for the values of*x*and*y*in that row, then negate it, i.e., (NOT (AND (f*x*)*(g**y*))). This guarantees that the particular row evaluates to zero. Then AND all of these terms together.What about the rows that evaluate to one? Suppose one such row is denoted by

*xx*and*yy*. Then either xx is not equal to*x*,*yy*is not equal to*y*, or both. Suppose*xx*is differs from*x*. Then (f*xx*) will evaluate to zero, so (AND (f*xx*) (g*yy*)) evaluates to zero, therefore (NOT (AND (f*xx*)*(g**yy*))) will evaluate to one. In this way, all rows that evaluate to one will evaluate to one and all rows that evaluate to zero will evaluate to zero. Thus the resulting expression generates the table.Converting to NOT/OR form uses the same idea. For each row with a one result in a particular table, create a function

*(OR (f x) (g y))*where*f*and*g*__evaluate to zero__for the values of*x*and*y*in that row, then negate it, i.e. (NOT (OR (f x) (g y))). Then OR all of these terms together.The application of this algorithm yields the following formulas. Note that the algorithm gives a non-optimal result for t(0), which is more simply written as (AND X (NOT X)). Perhaps this is not a fair comparison, since the algorithm is generating a function of two variables, when one will do. More appropriately, t(1) is equivalent to (AND (NOT X) (NOT Y)). So there is a need for simplifying expressions, which will mostly be ignored for now.

Define (NAND

t(0) = (AND (NOT (AND (NOT X) (NOT Y)))

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

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

t(1) = (AND (NOT (AND (NOT X) Y))

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

t(2) = (AND (NOT (AND (NOT X) (NOT Y)))

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

t(3) = (AND (NOT (AND X (NOT Y))) (NOT (AND X Y)))

t(4) = (AND (NOT (AND (NOT X) (NOT Y)))

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

t(5) = (AND (NOT (AND (NOT X) Y)) (NOT (AND X Y)))

t(6) = (AND (NOT (AND (NOT X) (NOT Y))) (NOT (AND X Y)))

t(7) = (NOT (AND X Y))

t(8) = (AND (NOT (AND (NOT X) (NOT Y)))

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

t(9) = (AND (NOT (AND (NOT X) Y)) (NOT (AND X (NOT Y))))

t(10) = (AND (NOT (AND (NOT X) (NOT Y))) (NOT (AND X (NOT Y))))

t(11) = (NOT (AND X (NOT Y)))

t(12) = (AND (NOT (AND (NOT X) (NOT Y))) (NOT (AND (NOT X) Y)))

t(13) = (NOT (AND (NOT X) Y))

t(14) = (NOT (AND (NOT X) (NOT Y)))

t(15) = (NOT (AND X (NOT X)))

*x y*) to be (NOT (AND*x y*)). Then (NAND*x x*) = (NOT (AND*x x*)) = (NOT*x*).(AND

*x y*) = (NOT (NOT (AND*x y*)) = (NOT (NAND*x y*)) = (NAND (NAND*x y*) (NAND*x y*)).These two transformations allow t(0) through t(15) to be expressed solely in terms of NAND.

Putting everything together, we have the following tables of identities. There is some organization to the ordering: first, the commonly defined function. Next, the AND/NOT form. Then the negation of the complementary form in those cases where it makes sense. Then a NAND form and, lastly, an alternate OR form. No effort was made to determine if any formula was in its simplest form. All of these equations have been machine checked. That’s one reason why they are in LISP notation.

x y | t(0)x y | t(15)0 0 | 0 0 0 | 1

0 1 | 0 0 1 | 1

1 0 | 0 1 0 | 1

1 1 | 0 1 1 | 1

0 1

(NOT 1) (NOT 0)

(AND X (NOT X)) (NOT (AND X (NOT X)))

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

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

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

(NOT (AND X Y)))))

(NOT (NAND X (NAND X X))) (NAND X (NAND X X))

(NAND (NAND X (NAND X X))

(NAND X (NAND X X)))

(OR X (NOT X))x y | t(1)x y | t(14)0 0 | 1 0 0 | 0

0 1 | 0 0 1 | 1

1 0 | 0 1 0 | 1

1 1 | 0 1 1 | 1

(NOT (OR X Y)) (OR X Y)

(NOR X Y)

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

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

(NOT (AND X Y))))

(NOT (NAND (NAND X X) (NAND Y Y))) (NAND (NAND X X) (NAND Y Y))

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

(NAND (NAND X X) (NAND Y Y)))x y | t(2)x y | t(13)0 0 | 0 0 0 | 1

0 1 | 1 0 1 | 0

1 0 | 0 1 0 | 1

1 1 | 0 1 1 | 1

(NOT (IMPLIES Y X)) (IMPLIES Y X)

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

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

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

(NOT (AND X Y))))

(AND (NAND X X) Y)

(NOT (NAND (NAND X X) Y)) (NAND (NAND X X) Y)

(NAND (NAND (NAND X X) Y)

(NAND (NAND X X) Y))

(NOT (OR X (NOT Y))) (OR X (NOT Y))x y | t(3)x y | t(12)0 0 | 1 0 0 | 0

0 1 | 1 0 1 | 0

1 0 | 0 1 0 | 1

1 1 | 0 1 1 | 1

(NOT X) X

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

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

(NAND X X) (NAND (NAND X X) (NAND X X))x y | t(4)x y | t(11)0 0 | 0 0 0 | 1

0 1 | 0 0 1 | 1

1 0 | 1 1 0 | 0

1 1 | 0 1 1 | 1

(NOT (IMPLIES X Y)) (IMPLIES X Y)

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

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

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

(NOT (AND X Y))))

(NOT (NAND X (NAND Y Y))) (NAND X (NAND Y Y))

(NAND (NAND X (NAND Y Y))

(NAND X (NAND Y Y)))

(OR (NOT X) Y)x y | t(5)x y | t(10)0 0 | 1 0 0 | 0

0 1 | 0 0 1 | 1

1 0 | 1 1 0 | 0

1 1 | 0 1 1 | 1

(NOT Y) Y

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

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

(AND (NAND (NAND X X) Y) (AND (NAND (NAND X X) (NAND Y Y))

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

(NAND Y Y) (NOT (NAND Y Y))

(NAND (NAND Y Y) (NAND Y Y))x y | t(6)x y | t(9)0 0 | 0 0 0 | 1

0 1 | 1 0 1 | 0

1 0 | 1 1 0 | 0

1 1 | 0 1 1 | 1

(NOT (EQUIVALENT X Y)) (EQUIVALENT X Y)

(EXCLUSIVE-OR X Y) (NOT (EXCLUSIVE-OR X Y))

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

(NOT (AND X Y)))

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

(NAND X (NAND Y Y)))

x y | t(7)x y | t(8)0 0 | 1 0 0 | 0

0 1 | 1 0 1 | 0

1 0 | 1 1 0 | 0

1 1 | 0 1 1 | 1

(AND X Y)

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

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

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

(NAND X Y) (NOT (NAND X Y))

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

(OR (NOT X) (NOT Y))

Let’s make an overly long post even longer. Since we can do any logical operation using NAND, and since I’ve never had any classes in digital hardware design, let’s go ahead and build a 4-bit adder. The basic high-level building block will be a device that has three inputs:

*addend*,*augend*, and*carry*and produces two outputs:*sum*and*carry*. The bits of the addend will be denoted by a0 to a3, the augend as b0 to b3, the sum as s0 to s3, and the carry bits as c0 to c3. The carry from one operation is fed into the next summation in the chain.

The “add” operation is defined by t(sum), while the carry is defined by t(carry):

a b c | t(sum)a b c | t(carry)0 0 0 | 0 0 0 0 | 0

0 0 1 | 1 0 0 1 | 0

0 1 0 | 1 0 1 0 | 0

0 1 1 | 0 0 1 1 | 1

1 0 0 | 1 1 0 0 | 0

1 0 1 | 0 1 0 1 | 1

1 1 0 | 0 1 1 0 | 1

1 1 1 | 1 1 1 1 | 1

Substituting (X, Y, Z) for (a, b, c) the NOT/AND forms are

t(sum) = (AND (NOT (AND (NOT X) (AND (NOT Y) (NOT Z))))

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

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

t(carry) = (AND (NOT (AND (NOT X) (AND (NOT Y) (NOT Z))))

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

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

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

The NAND forms for t(sum) and t(carry) are monstrous. The conversions contain a great deal of redundancy since (AND X Y) becomes (NAND (NAND x y) (NAND x y)).

However, symmetry will help a little bit. t(sum) = t(#x96) = (not t(not #x96)) =

(NAND

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

(NAND

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

(NAND

(NAND

(NAND X

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

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

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

(NAND

(NAND X

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

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

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

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

(NAND

(NAND

(NAND X

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

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

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

(NAND

(NAND X

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

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

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

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

(NAND

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

(NAND

(NAND

(NAND X

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

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

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

(NAND

(NAND X

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

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

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

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

(NAND

(NAND

(NAND X

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

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

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

(NAND

(NAND X

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

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

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

The complexity can be tamed with mechanical substitution and the use of “variables”:

let G0 = (NAND X X)

let G1 = (NAND Y Y)

let G2 = (NAND G1 Z)

let G3 = (NAND G2 G2)

let G4 = (NAND G0 G3)

let G5 = (NAND Z Z)

let G6 = (NAND Y G5)

let G7 = (NAND G6 G6)

let G8 = (NAND G0 G7)

let G9 = (NAND G1 G5)

let G10 = (NAND G9 G9)

let G11 = (NAND X G10)

let G12 = (NAND Y Z)

let G13 = (NAND G12 G12)

let G14 = (NAND X G13)

let G15 = (NAND G11 G14)

let G16 = (NAND G15 G15)

let G17 = (NAND G8 G16)

let G18 = (NAND G17 G17)

t(sum) = (NAND G4 G18)

The same kind of analysis can be done with the NAND form of the carry. The carry has a number of gates in common with the summation. Putting everything together, the circuitry for the adder would look something like this. Ignoring, of course, the real world where I’m sure there are issues involved with circuit layout. The output of the addition is the red (rightmost bottom) gate while the output of the carry is the last green (rightmost top) gate. The other green gates are those which are unique to the carry. The diagram offends my aesthetic sense with the crossovers, multiple inputs, and choice of colors. My apologies to those of you who may be color blind.

What took me a few hours to do with a computer must have taken thousands of man-hours to do without a computer. I may share the code I developed while writing this blog entry in a later post. The missing piece is simplification of logical expressions and I haven’t yet decided if I want to take the time to add that.

blog comments powered by Disqus