How do computers add,subtract,etc?

  • Thread starter Thread starter aychamo
  • Start date Start date
  • Tags Tags
    Computers
Click For Summary

Discussion Overview

The discussion explores how computers perform arithmetic operations such as addition and subtraction, focusing on the underlying mechanisms, including binary representation, logic gates, and the role of programming in these processes. Participants share their understanding of assembly language, binary arithmetic, and the hardware components involved in computation.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants express curiosity about how computers understand basic arithmetic, questioning how a computer "knows" that 1 + 1 equals 2.
  • There is a discussion about the process of adding binary numbers, with examples provided to illustrate how numbers are converted and added in binary form.
  • One participant describes a simplified model of how a computer adds binary numbers using registers and carry bits, emphasizing the right-to-left addition process.
  • Another participant explains that the ability of a computer to perform arithmetic operations stems from a set of instructions programmed by low-level programmers, which the CPU executes.
  • Some participants discuss the role of logic gates in performing arithmetic, noting that operations like addition can be executed using AND and XOR gates.
  • There is a question raised about the feasibility of using a different base, such as hexadecimal, as the fundamental language for computers, with some skepticism about its practicality.
  • Participants clarify that while hexadecimal is easier for humans to read, all logic operations are fundamentally performed in binary.
  • Discussion includes the physical components of computers, such as transistors and logic gates, and how they relate to arithmetic operations.
  • Some participants mention the historical context of microprogramming and its relevance to modern processors.

Areas of Agreement / Disagreement

Participants generally agree on the basic principles of how computers perform arithmetic using binary and logic gates, but there are differing views on the implications of using different numerical bases and the historical context of programming techniques.

Contextual Notes

Some limitations in understanding arise from the complexity of the underlying hardware and the abstraction provided by programming languages, which may obscure the direct relationship between high-level commands and low-level operations.

Who May Find This Useful

This discussion may be useful for individuals interested in computer science, electrical engineering, or anyone curious about the foundational operations of computers and how they perform arithmetic calculations.

aychamo
Messages
375
Reaction score
0
How do computers do math? I mean, it seems kinda like the math part is the most basic of the functions of the computer.

But I mean, how does it KNOW that 1+1=2?

To give a background, I understand binary: 1+1=10. I have a rudimentary understanding of assembly, I tried to program in it many years ago, so I could look at this:

mov ax, 1
add ax, 1

But how does that add 1 into ax, and then have the value of 2? (or b10).

I can almost see how like shl, and shr (is that square and square root?) can work, but shifting the bits, but not adding, etc.

I'm assuming the microchip (x86?) has to be preprogrammed to know the order of numbers.. But I have no clue. May someone shed light on this?
 
Computer science news on Phys.org
How do you add decimal numbers?
Code:
  1
 158
+ 23
-----
 181

So why can't you do that in binary as well?
Code:
 111
  101
+  11
------
 1000

Do you refer to 1, 2, 3, 4, ... as the "order of numbers"? If so, no, a computer doesn't the inifinite series of numbers programmed in it.
 
Well, I know that I can add numbers, because I can visualize it. I can see that if I have three objects, and take in an additional 4 objects, that I can now count 7 objects. But how does a computer do that?

Even this equation: 4+1=5. If you just put the value "4" in a computer, and you add the value of 1 to it, how does it know how to add?
 
How do you add 1 to 4? Probably "intuitively", because these are simple numbers. But what about 412793 + 23871?

If you tell a computer to add 1 to 4, it first takes both numbers and converts them to binary. 1 becomes 1, 4 becomes 100. Now it adds them 1 + 100 = 101 and converts the answer back to decimal: 5.
 
I think I am doing a poor job at conveying what I am trying to ask.

How does the computer know how to add 1 + 100 to give 101 ? I mean, how do you program a computer to be able to add numbers?
 
Let's start with 4 empty registers, and call them A, B, C and R. We want to add A to B, use the C register for carrying results and R for storing the result. (Obviously this is done a lot more efficiently inside the computer, but I'm trying to simplify things.)

Initially:
C = 0000
A = 1011
B = 0011
R = 0000

The computer goes from right to left, and does the following: it adds* the two bits in A and B, saves the result in the R register. After the first operation the registers look like this:

C = 0010
R = 0000

This is because 1 + 1 = 10, so we put 0 in the result and save 1 in the carry. Now the computer continues for the 2nd pair of bits, again 1 and 1. 1 + 1 = 10, but we also have a carry from earlier! So it adds the carry to the result 10 + 1 = 11 and registers it:

C = 0110
R = 0010

And it does the same again and again until it's done.

* You may still not understand how computer can add 1 and 1, or 1 and 0, or 0 and 0. This is simple and can be done with two gates, AND and XOR. Consider the following truth table:
Code:
 A | B | AND | XOR
---+---+-----+-----
 0 | 0 |  0  |  0
---+---+-----+-----
 0 | 1 |  0  |  1
---+---+-----+-----
 1 | 0 |  0  |  1
---+---+-----+-----
 1 | 1 |  1  |  0
---+---+-----+-----
If (A XOR B) is 1, the result is 1. Otherwise, the result is 0. As for the carry, that is determined with by the (A AND B) result - if it's 1, there is carry, otherwise there isn't.

Again I should stress that the computer does this much more efficiently. For example, it doesn't use a whole register for the carrying - it uses one bit in a special flag register to know whether or not the last operation had a carry. Also, it doesn't store the result in a completely new register, it stores it in the one of the registers A or B (as you probably guessed by the look of the ADD command in Assembly).
 
Chen is showing you the mechanics, the reason the computer knows how to do those mechanics is because at some point in time a programmer wrote a set of instructions and assigned a number to that set of instructions. When you give the compiler a ADD command it transforms that mnemonic into the number associated with the set of instructions for adding 2 numbers (or registers). When the CPU encounters the number associated with a set of instruction it blindly executes that command. This is the work of low level programmers to create the set of instructions that the CPU actually executes. I believe one of the least favorite courses toward a CS degree is the course in compilers where you will write the code for a simple compiler.
 
Ahhhhhhhhh, I honestly think I understand it now! It was the thing about the (logic, aren't they called??) gates.

Since this is how computers do the most basic stuff, with binary and the gates and all that, would it be kinda impossible to have a different base language for computers? I mean, say you used hex as the most basic thing, could you do logic gates with hexadecimal?

By the way, thank you both for writing that stuff out. That hit it home. I really appreciate it.
 
Yes, they are called logic gates. What kind of meaning would (4 AND 7) have, though? I don't know if it would be impossible, but certainly very very difficult and not worthwhile (what would be the advantages?).
 
  • #10
All logic is done in binary, the reason we use hex is the ease of translation between hex and binary. 1 hex digit translats to 4 binary digits. Not only is the Hex is easier to "read" then binary, it is far more compact. So the Hex is for humans, but it make the binary very accesable.

0001 = 1
0010=2
0011=3
0100=4
0101=5
0110=6
0111=7
1000=8
1001=9
1010=A
1011=B
1100=C
1101=D
1110=E
1111=F

so 4 and 7 = 0010 and 0111 = 0010
 
Last edited:
  • #11
Integral said:
so 4 and 7 = 0010 and 0111 = 0010
Let's see you plug that into a single gate operation, though. :smile: (That is what I meant, obviously there is a meaning to 4 AND 7.)
 
  • #12
Serial?

You are correct each individual gate handles a single bit at a time. Thus the old 8 bit computers (where I did a bit of machine language dabbling) had sets of 8 logic gates, new ones,... we're up to 64 now, I believe.
 
  • #13
And these gates, in actuality are they just some type of thing that controls electricity?
 
  • #14
Yep.

Just a bunch of really small transistors, for the most part.
 
  • #15
Here is a thread which dicsusses operation of a basic solid state device.
 
  • #16
Basically, all of this comes from building appropriate gates. The same way you can make an "and" gate, that produces the proper "output" given two inputs, you can put together one that "adds" and gives you also a carry-on bit, which you can then add to the next two binary digits.

The design of these type of circuits is actually one level below compilers. IIRC, it is called "microprogramming", and it can be done using logic gates and "flip-flop" circuits, which allow you to synchronize the operation of a circuit with a "clock" signal, so that operations are performed in the proper sequence.
 
  • #17
ahrkron,

Not quite. Microprogamming refers to CISC (Complex Instruction Set Computing) processors, in which large instructions are internally broken down into a routine of more fundamental instructions. Microprogramming has gone the way of the dinosaur, thankfully.

The simple answer is that a computer processor is made of tiny transistors, which can switch electrical current. The transistors are combined together into groups with function as logic gates, and can perform simple operations like AND, OR, NOT, XOR, and so on. These logic gates can then be grouped into larger-scale strucures which can take in two 8-bit (or 16-, or 32-, or 64-) strings of binary digits and produce their sum or difference. Some intelligent person who designed the processor wired up his logic gates in the proper way to perform those operations.

- Warren
 
  • #18
Integral said:
All logic is done in binary, the reason we use hex is the ease of translation between hex and binary. 1 hex digit translats to 4 binary digits. Not only is the Hex is easier to "read" then binary, it is far more compact. So the Hex is for humans, but it make the binary very accesable.

0001 = 1
0010=2
0011=3
0100=4
0101=5
0110=6
0111=7
1000=8
1001=9
1010=A
1011=B
1100=C
1101=D
1110=E
1111=F

so 4 and 7 = 0100 and 0111 = 0100

Hey I know this an old thread, but tell me if it is just chance, or does it actually mean something, the above. I'm not sure what I mean, but check it out.

The operation: (4 and 7), into binary gives 0100 and 0111 = 0100. So it gives 4. That's kinda like in statistics or whatever when you have two sets and you do the "and" thing, you get whatever is in common with the two sets. And the first four digits of the numbers that comprise 7 are also in the number 4. That's neat, right?
 

Similar threads

  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 26 ·
Replies
26
Views
3K
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 16 ·
Replies
16
Views
15K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
3K