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?

892 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

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.

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