How Does a Computer Add Two Numbers Together?

James HeslipComputer ScienceLeave a Comment

Computers are fascinating when you really think about it- how is it that a piece of metal goes from a simple rock in the earth, to being able to do complex arithmetic? Or to be able to remember?

In order to understand the more advanced topics, we need to start simple. How does a computer interpret a number? Again, you can shout “what’s 42+42?” as loud as you want at an iron girder, but it will not respond (not in our generation, at least!)

Decimal – our familiar number system

In our everyday life, we operate in a Decimal system, or base 10. It is believed that this is due to humans typically having ten fingers, and in our early stages, using those for counting. Decimal offers 10 different digits, 0-9. You will sometimes hear someone refer to their fingers or toes as “digits”, much like each number is a digit.

You may remember from primary school breaking a number down into its constituent parts. Take the number 1572, for example. What does this really represent?


1000s100s10s1s
1572

We can write 1572 as: (1*1000) + (5*100) + (7 * 10) + (2 * 1). Each grouping in the table, we multiply by ten. This could be rewritten as:


10* 10* 1010* 10101
1572

Or simply using a power notation:

10^310^210^110^0
1572

Important distinction for those mathematically unaware, anything to the power of 0 is 1. This will useful later. As for why, I leave the proof as an exercise for the reader.


Binary – the number system of computers

It is thought that the term binary was coined by Gottfried Leibniz in 1669, in his work “Explication de l'Arithmétique Binaire”. It means that number system has only two different digits (hence the prefix bi, like bipod- two legged). These digits are 0, or 1. Binary is considered base 2, as there are two alternatives.

Our previous table made use of base 10, but now we are working in base 2. A simple substitution (swap the 10s for 2s) gives us something which will work for binary instead of decimal:


2^32^22^12^0

OK. But how to use this thing? Well, I mentioned earlier that binary can only be a 0 or a 1. If I pick a four-digit stream of 0s or 1s, i.e. 0101 and enter that into our table (identical to the 1572 example in base 10) we get the following:


2^3 = 82^2 = 42^1 = 22^0 = 1
0101

This works out to (0 * 2^3) + (1 * 2^2) + (0 * 2^1) + (1 * 2^0), or 4+1. We can see therefore that 0101 (base 2) = 5 (base 10).


Logic – true or false?

Why bother with binary? You can be much more expressive in decimal as there are far more digits, right? Well, no. Binary can display everything that decimal can, it just might take more digits to do so. We saw previously that 5 equated to 0101.

George Boole (1815 – 1864) was a mathematician who devised a method of algebra which allows us to, given certain inputs, evaluate logically what the output should be. Computer science has modified some of Boole’s original notation, but he essentially gave us tools like AND, OR, NOT, XOR, etc.

For each of the following tables, we take two inputs (named arbitrarily A and B) and apply an operation to it. The last column is the result of the operation.

AND Truth Table

AND is self-explanatory; I only want to do something if both A and B are true:


INPUT AINPUT BA AND B
111
010
100
000

OR Truth Table

OR is true if A, B, or both are true:


INPUT AINPUT BA OR B
111
011
101
000

NOT Truth Table

NOT is slightly different to the other in that it only takes a single input. It negates the input, i.e.:


INPUT ANOT A
10
01

XOR Truth Table

XOR is a little less easy to explain; I want to do something is A is not the same as B.


INPUT AINPUT BA XOR B
110
011
101
000

Simple Binary Arithmetic

Binary addition works the same way that decimal addition does:

  • Working right to left (least significant digit upwards) add together the two numbers
  • You may need to carry over a digit if the result exceeds a value you can display in a single digit
    • i.e. in base 10, 9 + 6 = 15. This is the same as 5, carry 1.


Image

The above is a circuit diagram displaying how this is achieved:

  • First, two inputs, A and B are passed into an XOR gate (the curved-edge one) to calculate the sum
  • Then A and B are passed into an AND gate (the straight-edged one) to calculate the carry

This is an example of a “half-adder” circuit. A full-adder will have to take into consideration a potential carry from a previous calculation.


ABCarry (A AND B)Sum (A XOR B)Result
111010
010101
100101
000000

Notice that rows 2 and 3 in table above both result in 01. This is down to addition being a commutative process: 7 + 5 in base ten is the same as 5 + 7. In the case of the table, we are adding the same two numbers twice, and therefore getting the same result.

We can demonstrate the full addition process as below, by grouping the Half Adder circuit into the block for reuse/simplification:


Image

Conclusion

From what we have looked at here, a computer isn’t really all that clever. Yes, it’s pretty nifty to take some metal and have it do your maths homework, but ultimately, it’s not performing any operations that a 10 year old child couldn’t. We learn about positional notation (i.e. thousands, hundreds, etc.) from a very young age, as well as how to add. The only difference between a computer and a 10-year-old in this sense is efficiency and precision; computers don’t slow down when they’re tired, or bored.


About the Author

A picture of James Heslip the author of this blog

James Heslip

APL Team Leader


James is an APL Programmer with a keen interest in mathematics. His love for computing was almost an accident. From a young age he always enjoyed using them- playing video games and such- but it was never considered that anything more would come from it. James originally had plans to pursue a career in finance. More about James.


More from James



Other Posts