This week I celebrated my birthday and I thought it would be fun to talk about binary and hexadecimal numbers. Yes that’s right, fun.
One of the thoughts first had when I thought I would write this blog was that less outwardly nerdy people might read it and work out what all those nerd were laughing at with their nerd jokes. Then again, most people aren’t interested in those kind of jokes.
A lot of people know that computers use 1s and 0s to do their thing but less people know how or why. I’m going to talk about that a little bit. The reason that there can be a 1 or a 0 is that computers are made up of heaps of switches. You are reading this on a device that has many many millions of them. These work in the same way as a light switch. Not a fancy dimmer switch though, these switches can only be on or off.
Also, in simplistic terms computers work by taking instructions from a program, running those instructions on data and then presenting the results. In order for instructions and data to represent meaningful things, you need to be able to use those switches in a way to represent data and instructions. This is the idea behind the binary number system.
We humans have 10 fingers, including thumbs. It’s no coincidence then that a great many cultures (not all) use what is called a base-10 counting system or decimal numbers. Once we count to ten, we need to start using our toes or remembering what many tens we are up to (was I up to 66 or 76?).
Computers can do the same, but with only one switch you can only represent 1 or 0. As there are only two numbers something can be we call this base-2 counting, or binary numbers. To represent useful numbers you need to use more than one switch.
We call these switches bits.
Let’s look at a simple example with four bits. Bit one will represent the number 1 or 0, bit two will represent 2 or 0, bit three 4 or 0 and bit four is 8 or 0. Depending on which bit or bits are turned on we can represent with combinations of these any number between 0 and 15.
Below are some examples. On the left are three different numbers represented using 4-bit binary. On the right is how those numbers are calculated. Don’t worry too much about the calculation:
0000 = 0 (0 x 8) + (0 x 4) + (0 x 2) + (0 x 1) = 0 0101 = 5 (0 x 8) + (1 x 4) + (0 x 2) + (1 x 1) = 5 1111 = 15 (1 x 8) + (1 x 4) + (1 x 2) + (1 x 1) = 0
It often occurs to me that someone really smart had to have been responsible for thinking that this is even possible, but now we treat this stuff as given and use it all the time.
Often when writing numbers out like this in binary, the number is preceded by a 0 and then a ‘b’ to show that the number is in binary. So 15 in binary is 1111 and would be written 0b1111 to avoid confusion with one-thousand, one-hundred and eleven. Larger numbers can be represented using more bits. Depending on the computer processing the numbers, it may be able to calculate the number in one go, or it may need to split up the calculation into manageable parts (like performing long division with and pen and paper).
In the 1970s the first microprocessors to be implemented in pocket calculators and the like were 4-bit processors. This meant that they could only work on numbers 4 bits in value at one time. Later, more complex processors were developed that could work on 8 bits at one time and this let them represent numbers between 0 and 255. These types of processors were used in the first personal computers like the Apple II and the Commodore 64 (the 64 stood for the amount of memory, not the number of bits). 16 and 32 bit processors became used in IBM compatible PCs and video game consoles and only recently personal computers have started to use 64 bit processors.
64 bits are a whole lot of bits. It means that very large numbers can be represented. As an example, the maximum number in a 64 bit system would be represented by 64 ‘on’ switches. In binary we would write this as:
and the decimal number that is equal to is:
I don’t even know how to say that number but I know it ends in “illion”. Obviously representing large numbers in binary quickly becomes impractical. Even with 8 bits numbers can be time consuming to write in a form computers can operate on. Representing numbers with 8 bits in binary is still a pain:
0b1111 1111 = 2550b0100 0100 = 68
Luckily, some more clever people came up with a method to do this in a simpler way. With a small change there is a system that is easier for people to read whilst being simple for computers too. This system is called hexadecimal and it is base-16 (hexas meaning six and decem meaning ten in Latin and ancient Greek respectively). Because of the way binary systems work base-16 is actually very simple for a computer to decode and it is much better for people than binary. Hexadecimal systems use 0x (a zero and then a x) before their number so as not to confuse us again with good old decimal numbers.
Counting in hexadecimal uses the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. The first ten numbers should make sense. After that:
0xA = 10 0xB = 11 0xC = 12 0xD = 13 0xE = 14 0xF = 15
A computer can work this out because one digit from 0 to F of hexadecimal is equal to four bits of binary, which we said can count from 0 to 15 also. If you put two hexadecimal numbers together you can represent 8 bit numbers.
0x00 = 0 0x09 = 9 0x0A = 10 0x10 = 16
I should note that none of this comes naturally, it all comes from practice. I hope that by reading this you simply understand that much of this stuff isn’t super complicated, its just simple patterns that take learning.
In the title of this post I noted that this week is my birthday when I have turned 0x20 years old. If 0x10 is sixteen, then 0x20 is twice as many.
I turned 32 years old.