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
little late, but i think I have a good explanation.
if you think of exponentiation as repeated multiplication, multiplication as repeated addition, then what is addition? – the answer is repeated boolean algebra operations. boolean algebra is a bit complex, but there’s a theorem that any boolean expression (such as a long one representing the addition of two numbers) can be rewritten to only use three fundamental operations; AND, OR, NOT*. these operations are SO simple that they can very easily be implemented in a circuit. actually we have an integrated circuit that’s so good at this purpose its the basic building block of every cpu – and its called a transistor. so by chaining millions of transitors your able to program a computer to do, for instance, exponentiation, or any other basic arithmetic operation at a macro scale. from there, any more complicated operation are just repeated arithmetic and can therefore be calculated with a deconstruction into boolean algebra. most significantly though, now that you’ve unlocked all of mathematics, you can control other pieces of electronics, such as a display, by calculating what color each pixel should be and letting the display circuitry do the rest.
* an AND operation is if you have two input wires, if and only if A AND B are both powered, should the output wire have power. an OR gate is the same thing as AND, but powered if A OR B is powered.
a NOT operation is the output is powered if and only if the input wire is not.
If you are asking how computers do math here is the line of reasoning you can use:
– we can build circuits that perform (boolean) logical operators. E.g True AND True = True
– we know how to compose these circuits together to perform more complex operations
– to perform decimal math that we are familiar with, we need to model it into a logical operation and build its equivalent in circuits
Imagine a lightswitch: flick the switch and the light goes on. This is a physical process because electricity has been directed due to the switch.
Imagine two light switches arranged such that both of them have to be ON for the light to be ON. Switch1 *AND* Switch2 must be on. This is called AND.
Imagine two light switches arranged such that either light switch would turn on the light. Switch1 *OR* Switch2 will turn on the light. This is called OR.
Imagine the same as the OR example, but either light switch will turn on the light provided the other switch is off. One switch will turn on the light exclusive of the other one. This is Exclusive OR or XOR for short.
All of these can be created with simple ON/OFF light switches and the exact same concepts can be created with transistors in computers because one of the uses for a transistor is as a switch.
This gets pretty complicated, but by combining these functions – we call them Logic Gates – you can do all sorts of math *with physical switches*!
In a modern computer those physical switches are transistors, and by putting them in certain orientations addition, subtraction, multiplication, and division are possible. Literal physical devices are built with transistors with names like ADDER that literally add based on these gates.
Early computers were made with relays that clacked away making these switching paths. I actually built one and it’s amazing to watch and listen to it it process programs without a CPU. Here’s a (https://www.youtube.com/watch?v=g1lPCwukqSw). Every click you hear is literally a switch changing position from ON to OFF or vice-versa.
CPU cores are built (like, physically) to perform basic operations thanks to logical doors. It’s a series of electrical states and impulses, that goes one way or another in order to change the states. These states are binary, but by convention they are reunited 8 by 8 in what we call bytes. A byt can represent 16 different values, from 0 to F, which gives you hexadecimal. The second to last step is Assembly : Assembly is a set of core instructions that allows you to change from one state to another. For example, SET x will put x (any number) into memory state. And the last step, called user instructions (even if it’s automatic) or command prompts, are requests associated with a certain amount of Assembly instructions. Programming languages are sets of command prompts, usually written in readable English, that allows you to get the results you want, or build a layout up to even more complex instructions (algorithms, then programs, then operating systems).
So basically, from top to bottom : when you ask your computer to divide A by B, you send the instructions A, divide, B. They’re converted into Assembly : SET A, then several commands. These commands are built up as a series of binaries that are converted into electrical impulses, sent in specific places at a specific speed. And these impulses physically modifies the properties of your core. That’s possible thanks to semiconductors, especially silicium, that realigns its own atoms depending on electrical impulses. This leads to many physical movements (especially given the unbelievable speed of nowadays CPUs), that produces heat as an outcome. That’s why CPUs get so hot and need a cooling system : atoms dancing like devils in order to math for you.
I’ll defer this one to the excellent Sebasian Lague youtube series [https://www.youtube.com/watch?v=QZwneRb-zqA](https://www.youtube.com/watch?v=QZwneRb-zqA) ‘How do computers work’.
Basically, transistors allow ‘logic’ to transact with electricity with operations like ‘and’ ‘or’, ‘xor’, ‘not’ etc. Group enough of those logic gates together and you can calculate almost anything.
Latest Answers