< Day 3 Day 5 >
This post is part of a series I hope to publish regularly called "1 Times Daily." The idea is simple: that almost anyone can learn about computer science. I'm going to try to boil CS down to its core and present that in a way that nearly everyone can understand. I'll do my best to write from a "general audience" perspective, and we'll see if anyone bothers to read!
Day 4: Computers manipulate yes/no values with logic.
As usual, we need to introduce some terms in order to have a good chat about logic. At the hardware level, computers have many electrical circuits. This tutorial on circuits provides an awesome introduction to them, if you're interested. Electricity flows around a circuit, and we can add things to the circuit to manipulate the flow of electricity. As mentioned yesterday, computers use electrical current to represent a value, and we can systematically manipulate the current to symbolize manipulating the value it represents. One basic way we manipulate current is to introduce logic gates. A basic logic gate takes in (as input) one or two binary values and puts out (as output) one binary value. Now that we've got some lingo, let's get logical!
Amazingly, computers only need three basic logical operations: NOT, AND, and OR. We can present them in a form called a truth table, which shows the output of a gate under all possible inputs. I'm going to use F for False (0) and T for True (1). This is also a great chance to apply your newly acquired skills with binary numbers to notice that these truth tables are sorted by the numeric order of the inputs! The gates also have graphical representations used in diagrams.
NOT:The NOT gate (usually called an inverter) has one input (referred to as "A" below) and one output (called "out"). As you might expect, it simply flips the input value, just like the word "not" in English flips the meaning of a word:
The AND gate has two inputs (variables A and B below) and one output. If you think about how we use the conjunction "and" in English, the logical AND is similar - it means BOTH things are happening. For an AND gate to produce T, both inputs must be T. Otherwise, forget it!
The OR gate is similar in structure to the AND, except that it functions (you guessed it) somewhat like the English conjunction "or" instead. An OR gate will produce a T if any input is a T.
Sometimes we find it convenient to have additional types of gates. The most common are those that combine the basic gates with an inverter, like the NAND and NOR gates, which have the exact opposite output values as the AND and OR gates, respectively. Another interesting combination is the "exclusive OR" (XOR) gate, which is similar to the OR gate but is "strictly or"as in "one thing or the other":
Next time we'll actually do something cool with these gates, but even now, we could immediately start attaching switches or tape readers to inputs and light-emitting diodes (LEDs) to outputs and build a pretty cool toy. If nothing else, maybe it helps us start to understand the empire-inspiring awe around the machine featured below: