Computers work by manipulating patterns of zeroes and ones, which are represented by electrical signals varying in intensity. It is fascinating to ponder the fact that starting with simple manipulations of these electrical signals, we can build Mac OS.
The most basic logic units in a computer are called (fittingly) logic gates. These are devices with a number of inputs, and a number of outputs. The only allowed input and output are zeroes and ones. Thus logic gates represent functions . Such functions are called Boolean functions (after George Boole). We often think of zero as representing “false”, and one as representing “true”. Thus the input of a Boolean function is an array of “truth values”, and the output is another array of truth values.
The whole function can be represented by a truth table:
Here we have the truth table for the logical function “and”, which takes two inputs, and produces one input. It is “true” if and only if both its inputs are true.
Similarly, we have the truth table for the “or” function:
The “or” function is true if and only if one of its input variables are true.
We also have the “negation function” which is true if and only its input is not true.
Given these three functions, we can construct all other Boolean functions.
We first introduce a handy algebraic notation for Boolean functions. We can define them by a formula with variables . We write . A formula has no value until it is evaluated at some truth values. Say we put in , and , meaning that both and are false. Then we have , since the “And” function must return false here. Similarly, we have .
For the “or” function we write . With this notation, we can manipulate Boolean functions must like in high school algebra. For example, it is true that , and also that , since in “and” and “or”, order doesn’t matter. Not so obvious, but still true, we have that , just as in high school algebra. We also have , , and (where is the boolean function that is always false).
But here stops the similarities with high school algebra. In this algebraic system, it is always true that (shorthanded ). This is true because if a statement is true, then clearly the statement is true (and conversely). Similarly, we have that . Also: .
We also write for the negation function.
Every boolean function of two variables can be described using this three functions. We list all boolean functions of two variables:
|x And Not y||0||0||1||0|
|Not x And y||0||1||0||0|
|If y then x||1||0||1||1|
|If x then y||1||1||0||1|
The boring way to prove this is to check that all the expressions evaluate to the values in the right columns.
However, a more surprising fact, is that the only boolean function we really need is the “Nand” function: all other boolean functions can be built from combinations of Nand’s. This can be proved using the algebraic laws we discussed above.
- The not-function is the same as . If we had physical nand gates and wires, we could then form the not-function as follows:
- Given this, it is easy to build the and-function: . This is done by wireing as follows:The gates are nand gates.
- The construction of “or” is slightly more complicated. We first show a solution diagram: All gates are nand gates. That this is actually an “or” gate follows from de Morgan’s laws: the wireing can be translated to the following formula
. Inserting the definitions, this is , which is . By de Morgan’s law, this expression is equal to .
Since we can build “not”, “and”, and “or”, it follows that we can build all other logic gates. Thus everything a computer does comes from a simple “nand”.
So far we have talked about the basic logic gates, and introduced an algebraic notation for computing with boolean functions. The next steps in explaining how a computer works would be to talk about arithmetic units, for example. That would be in a future blog post!
(an interesting mathematical question is this: what kind of algebra does the boolean functions constitute? They do not form a ring, since there is no additive inverse…)