Let's Get Digital!
The principles of logic are clearly essential in computer software, as we must verify that conditional statements correctly implement our desired objectives.
More surprisingly, the logic of , , and forms the foundations of how computer hardware is designed and built.
The reason is that it is straightforward to build digital circuits which implement these logical functions.
Observe that the logical conditions , , and can be built out of physical switches.
The earliest computers used electronic relays and then vacuum tubes to construct these switches, which are now made out of transistors.
The black boxes which implement , , and are called logic gates.
Determining the Output Table of a Circuit
The behavior of any circuit which does not include feedback (the output of a gate fed back in as an input to that same gate) can be represented by a truth table or logical formula.
We can build a truth table by observing what happens to the output for each possible input to the circuit.
Note that this circuit computes the exclusive or, .
Determining the Output Function of a Circuit
We can also reconstruct the output function of a circuit symbolically, by tracing from the input signals and appropriately enclosing within parentheses.
The resulting formula can easily be seen to be .
A rule that can be used to construct the output function is to to start at the output gate G and
We stop this reporting process when the inputs are simple signals, instead of gates.
This is an example of a recursive algorithm, because it solves the problem in terms of solutions to smaller versions of the problem.
We will see many more examples of recursive algorithms later in the semester.
Notice that this procedure works correctly because there are no feedback connections. Otherwise we would get into an infinite loop if the circuit had a cycle!
Synthesizing Logical Statements
Given a logical function realized by the rightmost column of an arbitrary truth table, does there always exist a logical statement having this as its truth table?
If so, it means that we can make any circuit we want out of the basic logic gates.
A set of operations is universal if it can represent any possible truth table.
Can you give an example of a truth table which cannot be represented by just and ?
Building Statements from Truth Tables
In fact, , , and are universal.
Why? Each row of a truth table corresponds to a condition under which the formula is true.
This is true if is true, or if is true, or if is true. Thus,
Equivalently, it is false if , so
In fact, it is possible to construct any possible circuit from just one type of gate - either NAND (not and) or NOR (not or) gates suffice.
How can we show that NOR and NAND are universal?
How we can build , , and out of NAND or NOR gates?
Binary Numbers
Internally, computers work with binary (or base-2) numbers.
This is convenient because logical false (or off) can represent the 0 digit while logical true (or on) can represent the 1 digit.
Any number can be represented as a binary number in exactly the same way that any number can be represented as a decimal (base-10) number.
The key is that the value of a symbol depends upon its position. The ith digit from the right in a base b number is multiplied by .
Thus the base-10 number 11101 means
Thus the base-2 number 11101 means
We can convert any base-10 number into a binary number by repeatedly subtracting off the largest possible powers of 2.
To convert decimal 29 to binary, observe there are no 32's or higher powers of two. After subtracting off one 16, we have 13. After subtracting off one 8, we have 5. After subtracting off one 4, we have no 2's and one 1. This gives the binary number 11101.
It is important that you be able to easily convert between binary and decimal numbers!
Anybody celebrating their 16th or 32nd birthday is flipping their bits! Why?
I can count to 1023 on my fingers. Can you?
Binary Addition
Adding numbers in binary is analogous to adding them in decimal. Start at the right, add the last two digits, and move as the carry the part which is too big to fit in one digit.
Adding two 1's leaves a number too big to represent as one binary digit (or bit), so we write 0 as the sum and carry a 1 to represent the value 2.
In general, for each step of the process, we have to add three bits together; one from each number and the carry.
Using our logic design techniques, we can build a logical formula corresponding to this truth table, and then build a circuit which implements it.
The half-adder takes two bits and added them to get sum and carry bits.
The full-adder combines two half-adders to add two bits and a carry.
By cascading together a collection of half and full-adders, we can built a circuit to add up arbitrarily long integers.