Binary systems vs decimal systems
The most common way of representing numbers in our language (
I believe that Arabs are credited for inventing this notation. I might be wrong It was the Babylonians) is by using a combination of the following characters: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }. This
alphabet is made up of 10 characters, therefore it is called the
decimal system.
You probably spent your whole life looking at numbers being written in the decimal system, but other systems (alphabets) are also frequently used, mainly in various scientific fields. Some of the most common are:
|-------------+----------------------------------------------------+------|
| name | alphabet | size |
|-------------+----------------------------------------------------+------|
| binary | { 0, 1 } | 2 |
| octal | { 0, 1, 2, 3, 4, 5, 6, 7, 8 } | 8 |
| hexadecimal | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } | 16 |
|-------------+----------------------------------------------------+------|
These are just examples. There exists an infinite amount of alphabets you can use to represent numbers. You can even invent your own! (not very many people will understand what you're writing, but it's still theoretically valid!). These 3 alphabets are very commonly used because of some interesting properties, so it's good to know them.
Examples
Every existing number has a unique representation in each system. For example the number "thirty-one" can be written "31" in the decimal system, "11111" in the binary system, "37" in the octal system and "1F" in the hexadecimal system.
I won't go too much into the details of how to come up with the representation of a number in any system, you are learning binary in class and it shouldn't be too hard to figure out how to do it for octal and hexadecimal. (Converting from binary to hexadecimal is a fun and surprisingly simple exercise!). If you're curious, here is the representation of the first 21 natural numbers:
|-----------+---------+--------+-------+-------------|
| name | decimal | binary | octal | hexadecimal |
|-----------+---------+--------+-------+-------------|
| zero | 0 | 0 | 0 | 0 |
| one | 1 | 1 | 1 | 1 |
| two | 2 | 10 | 2 | 2 |
| three | 3 | 11 | 3 | 3 |
| four | 4 | 100 | 4 | 4 |
| five | 5 | 101 | 5 | 5 |
| six | 6 | 110 | 6 | 6 |
| seven | 7 | 111 | 7 | 7 |
| eight | 8 | 1000 | 10 | 8 |
| nine | 9 | 1001 | 11 | 9 |
| ten | 10 | 1010 | 12 | A |
| eleven | 11 | 1011 | 13 | B |
| twelve | 12 | 1100 | 14 | C |
| thirteen | 13 | 1101 | 15 | D |
| fourteen | 14 | 1110 | 16 | E |
| fifteen | 15 | 1111 | 17 | F |
| sixteen | 16 | 10000 | 20 | 10 |
| seventeen | 17 | 10001 | 21 | 11 |
| eighteen | 18 | 10010 | 22 | 12 |
| nineteen | 19 | 10011 | 23 | 13 |
| twenty | 20 | 10100 | 24 | 14 |
|-----------+---------+--------+-------+-------------|
Bits and bytes
While the concepts explained so far are general mathematics, and quite like the rest of maths, deal with abstract concepts of
numbers, the ideas of bits and bytes are more relevant to computer science, and more precisely about the architecture of modern "computers".
You probably heard that computers "speak" a language made only of 1s and 0s. Everything from this website, the pictures you took, the programs you're running, the music you listen to and the movies you're watching, everything is translated in "computer language" that is made of 1s and 0s.
Why is that?
Computers are not very intelligent machines. All they can do is detect 2 states "on" and "off". If you assign the value "zero" to state "off" and "one" to state "on", you just created an alphabet that is understandable by the computer.
Having an alphabet is not enough. In order for this language to make sense, we need a syntax that gives us the ability to create words (and a grammar that gives us the ability to create sentences - however this is beyond the current topic). This can be done by doing two things:
1- Strings of letters
You need to put side by side a number of units that can take any value of your alphabet. In the physical world, how this was done varied a lot from
earlier computers, to the circuits of
digital computers and the
future computers that maybe we'll be lucky enough to see some day. However, all throughout history, programmers (and scientists) only cared about one thing: whether the state was 1 or 0.
Since the physical components change throughout history, but the logic remain the same, we need a way to refer to these components unambiguously. I don't know who came up with this word, but everybody seems to agree now on the word "bit". I like to imagine bits as tiny little lamps that can be either on or off at any given moment (although in reality it's a bit different).
A bit, in our language, is equivalent to a "letter" in the English language. A "letter" can have any of the 26 values of the English alphabet, and a "bit" can have any of the 2 values of the computer alphabet. Also, just like letters, you can put several bits together to form words. This will be the subject of the next paragraph.
2- Several words together
We just saw that we can create words by putting bits together. Ok now here's a question: how many words are there in the following string of bits?
10110100101111010011110101100110
As you can see there's no easy way of determining the number of "words" in our language. The English language uses separators like "space" or "newline" or punctuation to determine the limit between one word and the next. Unfortunately, computers do not do this, on one hand because it would cost too much to do so, and on the other hand, because all they can do is really detect if a bit is "on" or "off".
Instead the computer language has another solution for determining words: fixed size.
For a computer, a word is always made of 8 bits.
Why 8? Why not 10 or 35?
The decision to have 8bit words is a little arbitrary. We could've chosen any other size. However 8 (being a power of 2) has some interesting characteristics that make the life of programmers and scientists a lot easier.
You might have guessed where I'm going with this: A
byte is a word made of 8 bits. Now that you know that, look back at the string of bits I gave at the beginning of this paragraph. How many words can you spot?
- 10110100 (180)
- 10111101 (189)
- 00111101 (61)
- 01100110 (102)
Exercise 1: Note that I added in parenthesis the values of the translation from binary to decimal. What is the highest value a byte can have?
Exercise 2:
1 byte (B) = 8 bits
1 kilo-byte (KB) = 1024 B
1 mega-byte (MB) = 1024 KB
How many MB are there in 5242880 bits?
PS1 @OnLiNe: Do not hesitate to ask questions if anything is unclear.
PS2 @everybody:This is a poorly written draft of what I hope will become an article for my blog. I would love getting as much help as I can for this. What can I modify/add/remove/.../... in order to improve it?