Humans count in decimal, but computers count in binary. How does that work?
A recap of decimal
If you’re really ancient like me, you may remember a long obsolete technology known as “cassette tape”. You may even have seem an actual cassette player. And you may have noticed that they typically have a 3-digit revolution counter. For… some reason. (I think the idea is to allow you to fast-forward or rewind to specific points on the tape… except they’re way too inaccurate for that!) Older gas and electricity meters have a similar mechanism.
The way the counter works is quite simple. There are some number of plastic wheels, with the numbers from 0 to 9 printed on them. You zero the counter, so each wheel shows 0. And then the first wheel slowly rotates around, giving you 000, 001, 002, and so on. Eventually, the first wheel reaches 9. As the wheel continues to turn, it rolls back to 0 again. But a notch on the wheel causes the next wheel along to also rotate round one place, so instead of 009 being followed by 000 again, what actaully happens is 009 is followed by 010.
So we have 010. The first wheel continues to rotate around, displaying 011, 012, 013, etc. Eventually we reach 019. As before, the first wheel rolls back to 0, nudging the second wheel from 1 to 2; so 019 is followed by 020. And so on.
After a long time, we eventually reach 099. At which point, the first wheel rolls back to 0, moving the second wheel. The second wheel also rolls back to 0, moving the third wheel. So 099 is followed by 100.
Eventually we reach 999, and at that point a 3-digit counter really does roll back to 000. But we could always just stick another wheel on the end. The pattern is simple; each time a wheel rolls back to 0, it moves the next wheel one place as well.
Another way of looking at it is the way you were probably taught in school: Given a number like, say, 725, what actually means 7 hundreds plus 2 tens plus 5 ones. Looking at any number, the right-most digit is ones, and each digit to the left is worth ten times more. So we have ones, tens, hundreds, thousands, ten-thousands, etc.
For this reason, if you take any number and stick an extra zero on the end, the number becomes ten times bigger. And if you slice a digit off, the number gets ten times smaller.
Octal
Now imagine what would happen if we took our mechincal counter and repainted the dials so each one only goes from 0 to 7. Now when the first wheel rolls from 7 back to 0, the next wheel turns. So now we go from 007 stright to 010. So basically, it’s like normal counting, except we skip over any numbers with an 8 or a 9 in them.
This number system is called octal. And it works just like decimal, but everything happens in lots of eight rather than ten. For example, in decimal we have ones, tens, hundreds, thousands. Each one is ten times bigger than the previous one. But in octal, things go up in eights. So we have ones, eights, sixty-fours, five hundred and twelves, and so on.
Now it might seem to you that five hundred and twelve is a rather random-sounding number, and a thousand seems like a much easier number. But that’s only because you’re used to counting in tens. Octal is still following exactly the same pattern; each number is eight times bigger.
What this does means is that “10” means ten in decimal, but that exact same figure means eight in octal. There is no way to know, just by looking at a number, whether it’s supposed to be decimal or octal. (Well, unless it has a 9 in it; then it can’t possibly be octal.) You have to know which one it’s supposed to be. Just like if somebody says that something melts at 206 degrees, there’s no way to tell whether they mean Celsius or Fahrenheit (or Kelvin); you just have to know which scale they’re talking about (or ask them).
So the number 427, in decimal, means 7×1 + 2×10 + 4×100. That same number, in octal, means 7×1 + 2×64 + 4×512, which is a totally different number. (To be exact, 427 decimal = 653 octal, and 427 octal = 279 decimal.)
If you take a decimal number and add another zero, it becomes ten times bigger. But if you take an octal number and add another zero, it becomes eight times bigger.
Because eight is smaller than ten, octal numbers tend to have slightly more digits than the equivilent decimal number. For example, 10 octal = 8 decimal, 100 octal = 64 decimal, 1,000 octal = 512 decimal.
Hexadecimal
So, decimal is the base-10 number system, octal is the base-8 number system. You can go the other way though; use a base larger than ten. For example, hexadecimal (“hex” for short) is the base-16 number system. To do that, we need some extra symbols; by convention, we add the first six letters of the alphabet.
Imagine again our mechanical counter, but now each wheel has the digits from 0 to 9, followed by the letters A to F. So now each wheel rolls through 10 digits, and then 6 further letters, and finally rolls from F back to 0.
In this system, A hex = 10 decimal, through to F hex = 15 decimal.
| Hex | Decimal |
| 0 | 0 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| A | 10 |
| B | 11 |
| C | 12 |
| D | 13 |
| E | 14 |
| F | 15 |
| 10 | 16 |
Starting at 000 hex, the first wheel slowly rolls round, until we reach 009. We then continue 00A, 00B, 00C, and so on, eventually reaching 00F. At this point, the first wheel rolls from F back to 0, and the second wheel steps round to 1: 010. Our pattern ends up looking like this:
| 000, 001, 002, 003, … |
| 009, 00A, 00B, 00C, 00D, 00E, 00F, 010, 011, 012, … |
| 019, 01A, 01B, 01C, 01D, 01E, 01F, 020, 021, 022, … |
| 029, 02A, 02B, 02C, 02D, 02E, 02F, 030, 031, 032, … |
| … |
| 09E, 09F, 0A0, 0A1, 0A2, … |
| 0A9, 0AA, 0AB, 0AC, 0AD, 0AE, 0AF, 0B0, 0B1, 0B2, … |
| … |
| 0FE, 0FF, 100, 101, 102, … |
The pattern is exactly the same as before, it’s just that each wheel now has more symbols to go through (some of which are letters).
What this also means is that the first wheel is 1×, the second is 16×, the third is 256×, and the fourth is 4,096×. And if you take any hex number and stick another digit on, it gets 16× bigger.
Because 16 is bigger than 10 (and quite a bit bigger, actually), numbers tend to take fewer digits when written in hexadecimal. For example, 3,000 decimal is BB8 in hex.
Quaternary
We’ve looked at a few number systems. Let’s look at ternary, the base-4 number system. So each wheel counts from 0 to 3, and then wraps back to 0 again. Because 4 is such a small number, numbers written in ternary tend to take quite a few digits.
000, 001, 002, 003, 010, 011, 012, 013, 020, 021, 022, 023, 030, 031, 032, 033, 100
So, in quaternary, “100” actually means what we normally call 16. By having so few different symbols to work with, we need more of them to write anything. For example, 300 decimal = 10,230 quaternary. So, almost twice as many digits!
Binary
Computers don’t count in decimal, or octal or even quaternary. Computers count in binary, the base-2 number system. Now binary works exactly the same way as the other number systems, but because it only has two symbols to work with, even the tiniest numbers look huge when written in binary.
Most people understand that 123 isn’t the same thing as 321. However, many humans would struggle to tell the difference between 10110100 and 00101101. Seeing that many identical-looking digits, your eyes just kind of glaze over. But binary is actually very simple, and follows exactly the same pattern as all the other number systems; it just takes a hell of a lot of digits to do anything.
So, our wheel now literally just has a 0 printed on one side, and a 1 on the other. When it goes past 1, it rolls back to 0 again. So our sequence is now
000, 001, 010, 011, 100, 101, 110, 111
And, with 3 digits, we can only count to 7. Yes, in binary, 7 is a three digit number!
Just like with the other systems, each digit is worth 2× as much as the previous one. So we have 1, 2, 4, 8, 16, 32, and so on. If you add another zero on the end, the number doubles in size. If you remove a digit, it halves in size. (Some computer programs actually do this as a fast way to double or halve numbers, by the way.)
Binary arithmetic
Doing arithmetic in any number base is pretty much the same as in decimal. To add two numbers in octal (for example), you add them exactly how you would in decimal — except that, if any column adds up to 8 or more, you subtract 8 and carry 1 to the next column. For example,
| 2 | 1 | 6 | ||
| + | 4 | 3 | 4 | |
| 6 | 5 | 2 | ||
| 1 |
In the 1s column, 6 + 4 = 10, which is too big. So 10 − 8 = 3, we keep the 3 and carry 1 over to the next column. There we find that 1 + 3 = 4, plus the 1 we carried is 5.
We could do the same in hexadecimal (remember to carry if we exceed 15), we could do the same in quaternary. And we can do the same in binary.
Actually, binary is simpler than most of the other number systems. In decimal, you have to memorise that 5 + 7 = 2 carry 1 (i.e., 12). But in binary, you only need to know that 0 + 0 = 0 (easy), 0 + 1 or 1 + 0 = 1 (also pretty esay) and 1 + 1 = 0 carry 1.
So, for example, 4 + 5 = 9 in decimal, but in binary you can write
| 0 | 1 | 0 | 0 | |
| + | 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | |
| 1 |
Sure enough, 1001 binary = 9 decimal.
Multiplication is also especially easy; you only ever have to multiply by 0 (which is just 0) or 1 (which gives you the number unchanged). You don’t have to memorise big times tables, you just need to add when you see a 1 and not add when you see a 0. How easy is that?!
One thought on “Learn to count like a computer”