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?

908 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

Truthfully, you’re not going to understand this from a text-based answer.

[Check out this video about designing an ALU (or arithmetic logic unit)](https://youtu.be/mOVOS9AjgFs) He’s got a whole playlist where he builds a computer from the most basic components.

Anonymous 0 Comments

Computers can only add.

Subtraction is adding a positive to a negative.

Multiply is adding multiple times.

Division is adding a positive to a negative multiple times.

Computers use bit patterns called “words” to represent operations. When a computer fetches a “word” it has all the info it needs to tell the computer how to manipulate the pattern.

So for example a simple 32-bit instruction set with fixed length operation codes has words that are 32 bits in length. The first 4 bits might tell it to add, sub, ect. Then the next bits will be the registers to and values. The but patterns just switches and the computer just blindly does what the pattern tells it to do.

Anonymous 0 Comments

Check out the game Dr. NIM

This game seems like it is thinking but it instead just clever levers that use gravity to flip some see-saws around.

Inside a computer we make electricity to flip some bits, then like the pully analogy mentioned here we make something greater than the whole.

Try out Minecraft and learn about an AND gate, then learn about a D-FlipFlop. You are now on your way to understanding first hand how computers are made.

Anonymous 0 Comments

Funny that there was just a post in technology about how AIs are not conscious.

Nice try chatGPT I see you there trying to get us to give our secrets away.

Anonymous 0 Comments

Everything in programming is built by layers that depend on the previous one, so that one operation available in one layer is “taught” how to do it in the previous one.

At the most basic layer, the computer’s calculator (the ALU part of the CPU) knows how to do these because it has special circuits that make the calculation as in “if there is a zero here and a one there, the result is that”.

Having zeros here or there actually means letting current pass through a transistor, but I’m not a hardware guy.

Anonymous 0 Comments

Best thing I can recommend is the book “Code” by Charles Petzold. He explains how computers work by starting all the way back at telegraph systems and building up to modern micro processors.

Anonymous 0 Comments

There are specific circuits to do things like add, multiply, and divide. The instructions we give to the computer are telling it to use those circuits. The computer holds numbers in something called a register, so when you give the computer instructions, it pulls the number either from an input or memory, puts it in a register, and then it runs the operation on that register and puts the result in a new register.

The registers are just a series of pins that either have a voltage or don’t. These are the 1s and 0s.

Anonymous 0 Comments

I haven’t seen this mentioned much but there is a field of mathematics that focuses on binary (1 and 0) math.

Much like the normal (0 – 9) mathematics, there are rules and operations that allow for the addition, subtraction, multiplication, etc. of binary numbers.

The computer does not need to know what it means to add two numbers together. The computer only needs to follow the rules and operations (instructions) of binary math.

Anonymous 0 Comments

For everything a computer knows how to do, it does these things based on some combination of CPU instructions. The instructions themselves are not software. They are tiny little electronic machines that perform well defined and simple operations like adding.

You could easily build a simple mechanical machine to add two 1-bit binary numbers. The adding part of a CPU is just 64 of these in electronic form chained together.

The tricky part is how the other operations are implemented. Sometimes, there are dedicated parts of the CPU that can handle those operations just like adding. Othertimes, they are partially implemented in software and partially implemented as simple electronic machines on the CPU. Part of CPU design is how complex or simple the instruction set should be.

Anonymous 0 Comments

You don’t.

It’s a split between hardware and emulation. Hardware has no concept of arithmetic. It knows only about registers, memory, and instructions.

An instruction could be: move value A to memory location B; increment register C by D, etc.

We give addition, subtraction, multiplication, and division meaning. For those, we have a specific set of instructions to perform each.