Eli5: Computers can calculate based on instructions. But how do you teach computers what does it mean to add something, multiply, divide, or perform any other operation?

914 views

Edit: Most of the answers here are wonderful and spot on.

For those who interpreted it differently due to my incorrect and brief phrasing, by ‘teaching’ I meant how does the computer get to know what it has to do when we want it to perform arithmetic operations (upon seeing the operators)?

And how does it do it? Like how does it ‘add’ stuff the same way humans do and give results which make sense to us mathematically? What exactly is going on inside?

Thanks for all the helpful explanations on programming, switches, circuits, logic gates, and the links!

In: 583

43 Answers

Anonymous 0 Comments

You start with something called a logic gate, which is a circuit built from transistors that produces a voltage either on or off based on the voltage status of one or more wires called inputs. Let’s start with a simple logic gate called OR. This logic gate will produce a voltage on the output wire if there is voltage on any of the input wires. Here is a truth table using 1 to indicate voltage, and 0 to indicate no voltage:
A B | OR
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1

Their are other gates called AND and NOT. AND produces a 1 if ALL of the inputs are 1. NOT takes a single input and produces the opposite state on the output.

These two numbers, 0 and 1, are the only two numbers that exist in a digital electronic circuit. However, if you follow the laws of mathematics, you can convert any number, including negative numbers, into an encoding of this binary system. You can represent the number 2, for example, by writing it as 10, and 3 as 11. This means you can have a number in a computer represented by two circuits next to each other, which can equal 0 (00), 1 (01), 2 (10) or 3 (11). Bigger numbers simply require more parallel circuits. A processor stores these numbers in dedicated circuits called registers, which are made out of logic gates and retains their values by making use of logical mathematics in order to have a memory. There is more than one way to create a memory circuit, but it generally involves placing a number of logic gates into a feedback loop.

Now that we have numbers and a place to store them, we need a final group of circuits built using this mathematical logic called an ALU, or arithmetic and logic unit. The heart of this circuit, which contains a decoder to select operations based on instructions, is called the adder. An adder is a compound logic gate built from and, or, and not which adds 2 binary bits, 1 or 0, and produces an output value, and a carry bit. So if you add 1 and 0, you get a 1, with a 0 carry, and if you add 1 and 1, you get a 0, and a 1 carry. 1+1=10. There are many of these adder circuits built in parallel. One for each bit in the size of the register. This carry bit gets added to the next adder in the line, going from right to left. So, if you have a carry bit added to the above example, 1+1+1=11. In computers numbers are generally either 8, 16, 32, or 64 bits in length. So really, for the mathematical equation 1+1=2: 00000001+00000001=00000010. That is everything you need to know to design a very basic computer to add.

How do you provide the other functions? You have to rearrange the numbers so that everything is addition. How do you subtract? You don’t. You make the subtrahend negative, and add it to the minuend. For this, you need a way to make a negative number. We use something called two’s compliment. A negative number can be created from any positive number by flipping every bit in the register, and adding 1. -1 starts from 1: 00000001, flipped: 11111110 and adding 1: 11111111. -1=11111111. So 1-1 is 0000 0001 + 1111 1111 = 1 0000 0000 in an 8 bit register. Whoops, we only have 8 bits in our register. What about that extra 1 which carried out? Throw it away. 0000 0000. 1-1=0. Now you can add and subtract. We design the decoder in the ALU to produce these outputs selectively when the instruction input is an addition or subtraction instruction, which is part of a special code called an instruction set. Programs are compiled to this instruction set using a human readable language and a program called a compiler.

So how can we multiply and divide? Multiplication is accomplished by repeatedly adding one number to itself, to a count of the other number. Division is done by repeated subtraction of the numerator from the denominator and keeping track of the count, like opposite multiplication.

These are the basics of digital arithmetic. There are all sorts of optimizations and tricks that people much smarter than me have figured out, but this is enough to create a machine that can do basic math. According to the work of Turing, any machine that can do basic math can do *any* algorithm, given enough memory space and enough time. So we have just outlined a complete universal computer. The rest is just a matter of programming.

Hope this helps.

You are viewing 1 out of 43 answers, click here to view all answers.