When I last looked at number systems nearly two years ago, I promised to do a follow up to the question “Why”. Why would someone choose to use binary, octal and hexadecimal number-bases instead of the familiar base-10 (decimal) system we use in our day-to-day life. I may have written it over 2 years ago without doing the promised follow-up, but it still seems to be the most visited page on this rather decrepit blog, so I thought I really should start to answer the question.
The answer lies in the fact that computers – as is often stated – store information using two states: ON, and OFF. (Note to the pendantic: this is not strictly true, but is – as they say – a necessary truth for the purposes of discussion).
Above is a Flip Flop circuit known as an SR NOR Latch, and it is a very basic form of memory. I will explain briefly how it works, but all you really need to know is that each of the two symbols represent what is known as a logic gate, that logic gates are made of transistors, and that this particular arrangement of transistors means that a value – a ‘state’ – can be kept as ‘memory’.
For those interested in how it works; it rests around a simple statement: if a switch is ON, it is NOT OFF. Similarly, if a switch is OFF, it is NOT ON. Simple, yes? Simple, but it forms the basis of a series of similar ‘statements’ which form the backbone of computing, known as ‘Truth tables’. A similar statement could be “If I hold a coin in either my left hand OR my right hand, then I hold a coin” and “If I hold a coin in neither of my hands, then I do not hold a coin”. Again, a simple statement, but statements like these really do form the backbone of computing.
|Left hand||Right hand||Left hand OR Right hand|
In the SR NOR latch, you can see two large symbols. These symbols represent a NOR gate, where ‘gate’ is simply a term used to describe the electronics that make the gate (transistors and resistors mostly, if you’re curious) and NOR stands for NOT OR. If you look again at the NOR gates, you will see two lines going in from the left – its Inputs – and one line coming out on the right – its output. The gate takes its two inputs and uses a simple rule to generate an output: If both its inputs are OFF, then the output is ON. If any of the two inputs are ON the output is OFF.
|Input 1||Input 2||Input 1 NOR Input 2|
Above is the truth table for the NOR gate laid out much like the coins in the first table. It is unfortunate that in English ‘nor’ as a word has a very different meaning that NOR using in computing, but then, strictly speaking, the word OR is very different in computing too, but that’s another joke for another time. Suffice to say that NOR mean “Take whatever the result would be if you asked my ‘or’, but give me the opposite”.
Now look at the lines marked ‘S’ and ‘R’. The letters S and R stand for “Set” and “Reset”. You can see each of these lines form one of the inputs of the NOR gates. The second Input of each gate is the Output of the other gate. The series of diagrams below show what happens when you start this system.
In step 1 – when the system is first turned on, we assume all inputs are 0. If all inputs are 0, then both of the outputs – A and B – must be 1. This can be confirmed by looking again at the truth table above. However, the output marked A forms the second input to the ‘S’ NOR, and the output marked B forms the second input to the ‘R’ NOR, giving this:
In step 2, the outputs from the first stage are shown as the inputs B and A. In this case, both NOR gates have one input with at least one line ON (shown here as 1), which means, both outputs come out as 0 – OFF. Take the outputs from this stage, and feed it back in again, and the outputs flip again:
The outputs keep flip-flopping. Left to its own devices, this is utterly useless. However so far we haven’t touched the R or S line. What happens if we put something in the R line?
Simply by briefly setting R to 1, we have changed the nature of the diagram: A and B for the first time are asymmetrical: they hold different values. A is now 0, and B is now 1. Not terribly useful for memory, but what if you now drop R back to 0? You get this:
The system is now stable. So long as you don’t touch S, no matter what else you do to R, A will always remain at 0, and B at 1. This circuit has remembered that at some stage in the past, we toggled the value of R from 0 to 1. What if you want to set the value of A to 1? You toggle S:
Here, we’ve toggled S, but we have a problem: A and B now have the same values. In order for the flip-flop to work as memory, and not simply a system that flips between two states as it was when we started, we need these values to be different (this is what the bars over the Q in the opening image means: “Not Q”, it must hold the value “Not Q” or the thing just doesn’t work). If we hold S for just one more cycle though, we get this:
There is nothing particularly special about having to hold S for two cycles, the same is actually true of reseting the system so that A is 0 again (by toggling R).
The important point here is that using a relatively simple piece of electronics that can be described in a diagram using just two symbols, we have working memory (and if you’re really interested, it takes 2 transistors and 6 resistors to make one). It may start off unstable, giving meaningless results, but once it has been deliberately set ON or OFF, it retains that value – it ‘remembers’ the last ‘instruction’ given to it. Incidentally, the term used for when a value is set and retained like this is ‘latched’, thus the name S(et) R(eset) NOR Latch.
Okay, so we can store two states: ON or OFF. But we don’t live in a world where two states should be enough for anyone, we live in a world where entire music collections and literary works are stored in memory, on disks, and transmitted across the internet. We live in a bigger world than ‘on’ and ‘off. So how do we get from these two states, to whole music collections?
Simple really. We just string a lot of them together.
What happens if we string two of these together? Instead of our two states of ON and OFF, we now have four possible states: OFF and OFF, OFF and ON, ON and OFF and ON and ON. By adding another bit of memory, we can now store 4 possible states. If we string one more, we can store EIGHT possible values. String four together? SIXTEEN possible values. String eight of these together and we can store a massive TWO HUNDRED AND FIFTY SIX possible values, just by stringing together 8 little bits of electronics that can be turned ON or OFF. With each consecutive ‘bit’ of memory added, you double the amount of states – the amount of information – that can be recorded.
Which brings us neatly back on to binary: why would anyone use binary instead of our day-to-day decimal? Well, if ON is 1 and OFF is 0, then you have a base-two number system. ON/OFF/OFF/OFF can be written 1000, which in base 2 is 8. ON/ON/OFF/ON can be written 1101, which is 13. Or, to put it another way, if you want to store the meaning of life, the universe, and everything, that can be stored as ON/OFF/ON/OFF/ON/OFF.
So that answers why we use binary, but why Octal and Hexadecimal? The answer to that question is strangely arbitrary, but it’s one that we’re stuck with. I hope to get around to answering that a lot quicker than two years.
(that’s 42, which in binary is 101010)