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?

880 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

How do you teach a pulley system to raise the load when you pull the rope?

You don’t teach it, you build it that way

The code for “add” loads specific values into the input of the adder which is an arrangement of transistors that add the inputs together. There’s no need for it to understand addition, just that if you push one lever down then the output lever goes down too, if you push two down then output is up and the carry lever is down.

We built specific structures in CPUs then set up the code to use these structures to give us the desired result. The rock doesn’t think

Anonymous 0 Comments

Those operations are part of the physical wiring in the processor. Binary addition can be written as a pretty simple combination of AND, OR, NOT, and XOR logic gates, and a computer adds by simply feeding the numbers it’s adding into a bunch of such gates. Typical processors are wired for somewhere around a hundred or a hundred and fifty such basic operations, and all the other instructions in a computer are reduced down to those operations.

Anonymous 0 Comments

This is where we leave the realm of software and enter the world of hardware. Beware, it’s a dangerous place.

Hardware engineers have built a system where electricity races in a well timed and coordinated way to produce specific results based on the inputs. If you want to see the math work, more traceable examples can be found with water or marbles.

In a twist, it is the hardware that determines the software’s capabilities, not the other way around. We can only tell hardware to use what it has, and only if it has offered us a way to do so.

Anonymous 0 Comments

All these operators are made using logic gates. It’s physical systems that when you put in connections of low or high voltage, they output some sort of logic. With combinations of this you can build all the operations.

Anonymous 0 Comments

You don’t. It’s sort of like asking “how do dominoes know how to fall and knock the next one down?”

Computers are just billions of little light switches (called transistors) that are set up like dominoes, with a handful of input switches that we can directly control. Turning on some switches causes some other switches to turn on, and others to turn off. By turning on curtain input switches and leaving others off, you’ll get a different output. Each unique input combination is an “instruction.” By using basic logic principles (look up “logic gates,” and “boolean logic,” if you want to learn more. It’s really pretty simple) we can set up these switches in a way so that we get the desired output.

Anonymous 0 Comments

For an actual ELI5, imagine you have a bunch of pebbles, some are white and some are black. Based on the way you order them and how many there are, you can read them to get a number.

Now imagine you can do operations on these rocks, for example, switching the last rock will add or subtract 1, or adding another rock at the end will multiply it by 2.

Now with these operations, you devise a machine that can add two lines of rocks just by combining them and checking each column, carrying the value over. It then spits out a new row of rocks with the value.

This machine has not been taught how to add. It has just been created in a way where all it does is add. It doesn’t know anything else, and when it is activated by giving it 2 rows of rocks, it just adds them together and spits out the sum.

Now imagine creating a machine like this for each operation you are going to do, and then you have the basic operations for a computer.

TLDR: the computer is not taught anything, any more than a car is taught how to drive. These operations are as fundamental to its operation as the silicon and metal they are made out of. They are able to do operations as an emergent capability based on their physical design

Anonymous 0 Comments

Computers use a language called machine language to communicate and process information. Machine language consists of strings of binary digits, 0s and 1s, which a computer can interpret as instructions. However, writing programs in machine language is extremely difficult, so high-level programming languages like C++, Python, Java etc., are used.

While writing a program in a high-level language, we use operators to perform mathematical operations such as addition, multiplication, subtraction, and division. These operators are symbols or keywords that are pre-defined or built into the programming language.

For instance, in Python, ‘+’ is used for addition, ‘-’ is used for subtraction, ‘*’ is used for multiplication, and ‘/’ is used for division.

When we write a program using these operators, the program is converted to machine language by the compiler. The compiled program contains the machine code for instructions that the computer can understand.

For example, consider the following Python code:

a = 5
b = 10
c = a + b

In this code, we are adding two numbers ‘a’ and ‘b’ and storing the result in a variable ‘c’. When this code is executed, the program sends a request to the CPU to perform the addition operation, which is executed as a series of electrical signals. In this way, the computer computes the result and stores it in the variable ‘c’.

In summary, mathematical operations in programming are performed by using pre-defined operators that map to machine instructions, which the computer can execute.

Anonymous 0 Comments

If you are a fan of this subject and would like a deeper understanding of exactly how a computer operates, I’d recommend a game on steam called Turing Complete. You literally build everything yourself. They start you out with 1 single logic gate, the nand gate. Then the game prompts you to create other gates that have a specific function, but they are only made from the nand gate or from the gates you yourself created with the nand gate. It keeps asking you to do increasingly more complex things until you literally build and wire a turing complete computer from just nand gates. If you’re determined, a person with 0 computer science knowledge can learn to build a computer from scratch. The game doesn’t tell you how any of it works, it makes you figure it out with some helpful hints. This way you won’t just have basic knowledge, but also the understanding of the knowledge to back it up. You will 100% understand how a computer calculates based on instruction, because you will be building the hardware architecture yourself, and writing the assembly code that makes it function.

Anonymous 0 Comments

Here’s a video explaining a marble based adding machine.

How did this guy “teach” the machine to add? He didn’t. He built the machine such that adding is what it does.

A computer processor has a similar “adder” inside it, but instead of “marble” or “no marble”, the adder in your processor uses “electricity” or “no electricity”.

Anonymous 0 Comments

They’re built that way, this game can show you the details:
[https://nandgame.com/](https://nandgame.com/)