The Binary Number System
Introduction to the Binary Number System
I recently introduced the Decimal Number System, the one we are used as humans.
As I said in that post, as humans we commonly have 10 fingers and we can count up to 10, hence the popularity of that system in our history.
The Binary Number System is the second most important system for our species, as it led the electronics and computer revolution.
In electronics, we have 2 states: 0 or 1. There’s 0 volts, or there’s 5 (or 9, 12, whatever). A gate is open, or it’s closed.
It’s either one or the another.
The digit in the binary number system is called bit.
As the decimal number system, also the binary number system is positional.
We sum each digit in the binary number system mutiplied by the power of 2 depending on their position, starting at position 0 from the right.
Given that:
\[2^0\] equals to 1
\[2^1\] equals to 2
\[2^2\] equals to 4
\[2^3\] equals to 8, and so on..
We can represent numbers using a series of bits:
1
can be represented as \[1\times2^0\]
10
can be represented as \[1\times2^1 + 0\times2^0\]
111
can be represented as \[1\times2^2 + 1\times2^1 + 1\times2^0\]
Leading zeros in a number can be dropped, or added if needed, because they do not mean anything on the left of the top left 1
: 110
can be represented a 0110
or 00000110
if needed. It holds the same exact meaning, because as the system above explained, we are simply multiplying a power of 2 times zero.
Using binary numbers we can represent any kind of number in the decimal number system.
We need to have an adequate number of digits to represent enough numbers. If we want to have 16 numbers, so we can count from 0 to 15, we need 4 digits (bits). With 5 bits we can count 32 numbers. 32 bits will give us 4,294,967,296
possible numbers.
64 bits will give us 9,223,372,036,854,775,807
possible numbers. It grows pretty quickly.
Here is a simple conversion table for the first 4 digits, which we can generate using just 2 bits:
Decimal number | Binary number |
---|---|
0 | 00 |
1 | 01 |
2 | 10 |
3 | 11 |
Here is a simple conversion table for the first 8 digits:
Decimal number | Binary number |
---|---|
0 | 000 |
1 | 001 |
2 | 010 |
3 | 011 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
If you notice, I repeated the above sequence, adding 1
instead of 0
in the series from 4 to 7.
Here is a simple conversion table for the first 16 digits:
Decimal number | Binary number |
---|---|
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
13 | 1101 |
14 | 1110 |
15 | 1111 |
Again, I repeated the sequence we used to get the first 8 numbers, prepended 0 to the first set 0-7 and prepended 1 to 8-15.
I’ll soon talk about performing operations like sum and division with binary numbers, the hexadecimal numbers system, how to convert from binary to decimal and to hexadecimal, without looking at tables like these, and vice versa.
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025