It’s all just 0 and 1: Understanding Binary
We often hear or say that computers are just a bunch of 1s and 0s. This series of short articles try to explain what it means!
This article is the first part of a series; you can find the others here:
- PART 1 — Understanding Binary
- PART 2 — About Bit and Bytes
- PART 3 — Binary Operations (soon to be release)
- PART 4 — Binary in electronics (soon to be release)
PART 1 - Understanding Binary
The first time I was introduced to Binary at school, more than 20 years ago, there was a piece of paper on the wall with this text:
There is 10 kinds of people, those who understand binary and the others.
I didn’t understand the joke at this time. Maybe it’s also the case for you, but don’t worry, before the end of your reading, you’ll be able to explain it to anyone yourself!
To count in Binary, we first need to understand how we count in our day-to-day life, without even thinking about it.
Counting in Decimal
The Decimal term comes from the Latin “Decem” which means Ten, as we use ten symbols to count, using the commonly named Arabic Numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
If you didn’t know already, in computer science, we start counting from 0, not from 1.
When counting, we simply move from symbol to symbol, following the given sequence. When we’re at the end of this sequence, we simply start again with a new row of symbols to the left, with each row representing a different unit. The first is the simplest one: you just use the symbol’s value. The second is the symbol’s value multiplied by ten. The third is multiplied by a hundred, etc.
By doing some math, we can explain how we calculate numbers. I’ve decomposed the number 1337 into rows in the following picture, starting from index 0 to index 3. We can very quickly identify the correlation between the symbol’s row and its calculated value.
Decimal basically means “ten symbol-based numbers” or just “base 10 numbers”. Now we’ll see how easy it is to apply the exact same method to represent numbers in Binary, which are “base 2 numbers”, using only the 0 and 1 symbols.
Before things get complicated with all these numbers, we’ll use the “base number” subscript to distinguish them when both Decimal and Binary numbers are present. For example, 42 in Decimal would be written with the ₁₀ in subscript, and Binary would have the ₂ in subscript as follows:
Counting in Binary
If we apply our previous schema, but we replace the base 10 with by a base 2, here is what we get for “1011” in Binary:
As you can see, it’s pretty simple. Instead of multiplying each row by 10 as in Decimal, we multiply by 2 each new row to the left. 2 being the number of symbols.
The above schema can be easily rewritten with the rows values instead of index, as follows:
Here are two more examples of how to convert Binary numbers into Decimal numbers, following the same principle:
From here, you know exactly how to count in Binary, which is, in fact, simply converted to Decimal. But what about doing the opposite, starting from a Decimal number, calculating its Binary equivalent?
By the way, at this moment of your reading, you should already be able to understand the joke I wrote about earlier in the article (of course, it only works written):
There is 10 kind of people, those who understand binary and the others.
Decimal to Binary
There are multiple ways of converting a Decimal number to a Binary (including a calculator…). But the one I found to be the easiest is the division by 2 with the remainder.
Using this method, you start with your Decimal number, divide it by 2, write the result on the left, and keep the remainder below. You then divide by 2 again this result, still holding the remainder down, and put the new result on the left again. As we divide by 2, the only remainders possible are 0 or 1. Once you reach the result 0, you can take all the remainders, and you have your Binary number.
Let’s convert the Decimal 42 into its Binary equivalent step by step:
Let’s do the same method to convert our first decimal number 1337:
From this point, you know how to read Binary numbers and convert Decimal into Binary!
But as you may have already seen, Binary numeration is quite tedious… This is why engineers in the mid-fifties created a new base for Binary numbers, a 16 symbol-based numeration called Hexadecimal.
When 16 are better than 2
You’ve likely been in contact with Hexadecimal numbers in your life without knowing it. Some internet providers, for example, create WiFi passwords using Hexadecimal numbers. This numeration allows representing a four symbol Binary number into a single symbol Hexadecimal number, so 1010 would become A.
The 16 numbers are using the known Decimal numbers for the 10 first symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9), and the 6 first letters of the Latin Alphabet for the remaining symbols: A, B, C, D, E and F. Using this symbols the letters once converted to Decimal’s value are: A=10, B=11, C=12, D=13, E=14 and F=15.
Now, using the exact same system as for Binary, here is how to convert Hexadecimal to Decimal:
And for the Decimal to Hexadecimal:
And finally, converting Hexadecimal into Binary is quite simple, as each Hexadecimal symbol represents 4 Binary symbols, you can convert them separately, and concatenate the resulting Binary numbers as follows:
You know almost all the basics about Binary, how to read it, and convert it in a more convenient format! I hope you learned something and enjoyed this article as much as I enjoyed writing it. Let me know your feedback and questions in the comments!
UPDATE: After the publication of this article, I’ve created a small website here to be able to quickly play around with conversion, you can also check the source code from Gitlab here.