Modified copypasta from an earlier post:
You have wires and electrical components. You apply a voltage going in and see what voltage comes out. That’s a logic gate. Here are some. 1 means high voltage, 0 means low.
* AND: If both are 1, the result is 1. If either one or both are 0, the result is 0.
* OR: If one or the other or both are 1, the result is 1.
* XOR (exclusive OR): If one or the other (but not both) are 1, then the result is 1.
* NOT: Operates on one bit, if it’s 1 the result is 0, if it’s 0 the result is 1.
* NAND: combined NOT and AND, outputs 1 as long as both inputs aren’t 1
For example, an AND gate is an electronic circuit that accepts two voltage inputs and has one voltage output. It has just a few electronic components (resistors and wires and stuff, you could easily make one yourself) arranged so that if both inputs are high voltage, the output is high voltage. If one or both of the inputs is low voltage, the output is low voltage. We then interpret high voltage as 1, low voltage as 0, and we can do an AND on two bits. The same works for the next two, just the electronic components are arranged a bit differently to achieve the other results. The NOT gate just inverts the one input, low to high, high to low voltage.
We can use these to do things. Let’s start building.
A half-adder gives you the sum of the two inputs plus the carry. So 1 + 1 = 0 with a 1 carry (like 5+5 is 0 with 1 carry to be 10). You make this half-adder by hooking an AND gate and an XOR gate together, that’s it. You apply high voltage to both inputs (1+1), you get high low voltage out on the add line (0) plus high voltage out on the carry line (1).
But that’s too basic. We need to add numbers we can use, notice we didn’t do anything with that carry. So let’s make a full adder which accepts the numbers to be added and can take that carry from a previous adder. This is made by combining two half-adders with another XOR gate.
Now this is still for one bit, but now we have something we can chain together since each adder accepts the carry from the previous. If you have 8 bits, you string 8 full adders together so each can get the other’s carry (carry out wire connected to the carry in wire on the next one) and output the result. Put in two 8-bit numbers (remember, this is just voltage sent to wires), and you’ll get the 8-bit result plus a carry, if any. We can now add any two numbers 0-255, and we’ll be told if we need to carry a 1.
So now we can add. Subtraction, multiplication, and division all work the same way, just a different number of those basic logic gates arranged differently.
And now we can do any math by controlling what voltages get sent where. You could do this at home with a circuit board, some transistors and resistors. Put two rows of 8 flip switches to describe the first and second input numbers (up = high voltage = 1, down = low voltage = 0) where each switch feeds high or low voltage into one of your 8 full adders. Now hook a set of lights to the output of each full adder plus another light connected to the carry output. As you flip the switches, the lights will change to represent your two added numbers.
Ta-da, you just made a basic binary adding calculator. Now instead of just adders, you could put in the components for a subtractor, a multiplier, and a divider (above), and have another switch that controls which of those is hooked up to the input switches and output lights. Now you’ve made a full-blown binary calculator.
Now let’s automate this. Let’s run your circuit board on a clock that sends a pulse of electricity every second. We can also call a particular arrangement of configuration switches a “program” that can control how the electricity flows to multiple parts of the calculator. We need memory! No problem, throw in two NAND gates connected to teach other the right way and you can set and read one bit by applying and detecting electricity. Sixteen NAND gates let you store one 8-bit byte, so instead of your calculator result just flashing lights, it can be stored in these NAND gates, and it can be used later.
Congratulations, you have a primitive computer. A modern microprocessor is the same thing, except we have billions of components and run at billions of pulses per second. Instead of transistors, resistors, and wires on a circuitboard you can touch, they are printed on silicon wafers using a type of photography, where one AND gate is only several billionths of a meter in size.
Latest Answers