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?
1000s | 100s | 10s | 1s |
---|---|---|---|
1 | 5 | 7 | 2 |
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* 10 | 10* 10 | 10 | 1 |
---|---|---|---|
1 | 5 | 7 | 2 |
10^3 | 10^2 | 10^1 | 10^0 |
---|---|---|---|
1 | 5 | 7 | 2 |
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^3 | 2^2 | 2^1 | 2^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 = 8 | 2^2 = 4 | 2^1 = 2 | 2^0 = 1 |
---|---|---|---|
0 | 1 | 0 | 1 |
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 A | INPUT B | A AND B |
---|---|---|
1 | 1 | 1 |
0 | 1 | 0 |
1 | 0 | 0 |
0 | 0 | 0 |
OR Truth Table
OR is true if A, B, or both are true:
INPUT A | INPUT B | A OR B |
---|---|---|
1 | 1 | 1 |
0 | 1 | 1 |
1 | 0 | 1 |
0 | 0 | 0 |
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 A | NOT A |
---|---|
1 | 0 |
0 | 1 |
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 A | INPUT B | A XOR B |
---|---|---|
1 | 1 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
0 | 0 | 0 |
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.
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.
A | B | Carry (A AND B) | Sum (A XOR B) | Result |
---|---|---|---|---|
1 | 1 | 1 | 0 | 10 |
0 | 1 | 0 | 1 | 01 |
1 | 0 | 0 | 1 | 01 |
0 | 0 | 0 | 0 | 00 |
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:
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.
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.
Ask James about APL / APL Consultancy / APL Legacy System Support