Computers, on a very low level, are built upon binary (ones and zeros). Think about that — all of the text you're reading on your screen started life as either a one or a zero in some form. That's incredible! How can it turn something so simple into a sprawling sheet of characters that you can read on your device? Let's find out together!
When you or I count, we typically use 10 numbers in some variation of combination to do so:
When you count to
10, you're really using a combination of
0 in order to construct a larger number that we cognizantly recognize. The number
10 persists in our minds even when we have it written out; ten.
Knowing that we can separate the number from our thoughts allows us to further categorize the number, mentally breaking it down into smaller groupings. For example, the number
34 can be broken down into three groups: the ones, the tens, and the hundreds.
For the number
34, we break it down into:
3 tens, and
4 ones. We can then multiply the higher number with the lower number (the column they're in) to get the numbers
3 tens) and
4 ones). Finally, we add them all together to make the number we all know and love:
This breakdown showcases a limitation with having 10 symbols to represent numbers; with only a single column, the highest number we can represent is
Remember that the number
10 is a combination of
0? That's due to this limitation. Likewise - with two columns - the highest number we can represent is
Now this may seem rather simplistic, but it's an important distinction to be made to understand binary. Our typical decimal numeral system is known as the base 10 system. It's called as such because there are 10 symbols used to construct all other numbers (once again, that's:
Binary, on the other hand, is base two. This means that there are only two symbols that exist in this numeral system.
For the Latin enthusiasts, binary comes from "binarius" meaning "two together". Deca, meaning 10, is where "decimal" comes from. Additionally, the term "radix" is sometimes used instead of "base" when describing numeral systems, especially in programming.
Instead of using numbers, which can get very confusing very quickly while learning for the first time, let's use **
X**s and **
O**s as our two symbols for our first few examples. An
X represents if a number is present and that we should add it to the final sum; an
O means that the number is not present and that we should not add it.
Take the following example:
In this example, both
2 are present, so we add them together to make
3. You'll see that since we can only have a value present or not present — because we only have two symbols in binary — this conversion has fewer steps than using decimal. For example, if you only wanted the number two, you could simply mark the
1 as "not present" using the
You can even replace the two symbols with
0 to get the actual binary number of
10 in order to represent
So, how does this play out when trying to represent the number
50 in binary?
As you can see, we create columns that are powers of
2 for similar reasons as using powers of
10 in decimal; you can't represent
32 without creating a new column otherwise.
Remember, in this system, a number can only be present or not; there is no
2. This means that only the symbols
0are present. Keeping this in mind, it then means that we can only have
11as the highest represented number without another column.
11in binary is
3in decimal. This shows that with only 2 binary digits, only the decimal numbers that can be represented are:
3. As a result, we need to add a
4column in order to represent that number in binary.
Continuing on with this pattern: without an
8column, you can only have a
1which would yield a maximum value of
7. It's important to note that these values are always one less than a power of 2.
Once each of these powers is laid out, we can start adding
1s where we have the minimum amount of each value. For example:
64less than or equal to
50? No. That's a
32 <= 50? Yes, therefore that's a
50 - 32 = 18
- Moving down the list, is
16 <= 18? Yes, that's a
18 - 16 = 2
8 <= 2? No, that's a
4 <= 2? No, that's a
2 <= 2? Yes, that's a
2 - 2 = 0
- Now that we're left with
0, we know that the rest of the digits will be
Add up all those numbers:
And voilà, you have the binary representation of
While there are plenty of ways to find the binary representation of a decimal number, this example uses a "greedy" algorithm. I find this algorithm to flow the best with learning the binary number system, but it's not the only way (or even the best way, oftentimes).
Binary isn't the only non-decimal system. You're able to use any number as your base as long as you have enough symbols to represent the digits. Let's look at another example of a non-decimal system: hexadecimal.
Hexadecimal is the base 16 number system.
Hexa means "six" in Latin, and deca means "ten", so these are combined to form "sixteen".
Now you may wonder how you can count to 16 in a single column when we only use 10 symbols to represent numbers. The answer, to many developers, is to fill the remaining last 6 with other symbols: alphabetical letters.
These are the symbols that make up the hexadecimal numeral system for many developers.
A, in this case, represents the number
F represents the number
15. In this numeral system, there are the sixteens, the two-hundred fifty sixes (gathered by multiplying 16 by itself — 162), and other powers of 16.
Given this information, how would we represent the number
Assuming we have a ones column, a sixteens column, and a two-hundred fifty sixes column, we can calculate the number in a similar way to the binary example earlier:
256less than or equal to
50? No. That's a
16 <= 50? Yes. So we know it's at least
- Now, how many times can you put
16 * 2 = 32and
32 <= 50, so it's at least
16 * 3 = 48and
48 <= 50so it's at least
16 * 4 = 64. However,
64 > 50, therefor the sixteenth place cannot be
4, therefore it must be
- Now that we know the most we can have in the sixteenth place, we can subtract the sum (
48) from our result (
50 - 48 = 2
- Now, how many times can you put
- Now onto the ones place: how many ones can fit into
1 * 1 = 1and
1 <= 2, so it's at least
1 * 2 = 2and
2 <= 2and because these numbers are equal, we know that there must be
Now if we add up these numbers:
While reading through this, you may wonder, "Where did the
256 come from?". Let's take a step back to analyze this question.
If you recall, we use these 15 symbols to represent digits in hexadecimal:
Using just a single digit (or, phrased in another way: A single number column), the biggest number we can represent is
15 in decimal.
This is similar to how the biggest number we can represent with a single digit in decimal is
In order to add a number larger than
15 in the hexadecimal system, we need to add another digit/column. This column would represent the sixteens place. Having
F in both this column and the ones column, the highest number we can represent would be
255 in decimal. As a result, we need to add a two-hundred-fifty-six column to represent any numbers higher.
For those that have experience in algebra, you'll notice that these are all powers of 16.
100is 102 for the decimal system,
256is 162. We can follow this pattern to the next number in the hexadecimal column:
4096, which is 163. You can even apply it to
1which is 160.
Binary works in the same manner. The first 5 columns/digits of binary are:
16. These numbers align respectively to their binary exponents: 20, 21, 22, 23, 24.
It's also worth noting that decimal numbers can be written out the same way.
732for example, in base 10, can be written as (7 × 102) + (3 × 101) + (2 × 100).
Remember that at the end of the day, hexadecimal is just another way to represent a value using a specific set of symbols. Just as we're able to convert from binary to decimal, we can convert from hexadecimal to binary and vice versa. In binary, the set of symbols is much smaller than in hexadecimal, and as a result, the symbolic representation is longer.
After all, they're just reflections of the numbers that we represent using a specific set of symbols. In binary, those symbols are more restrictive than in hexadecimal, and therefore the symbolic representation is longer.
Funnily enough, if you've used a "hex" value in HTML and CSS, you may already be loosely familiar with a similar scenario to what we walked through with the hexadecimal section.
For example, take the color
#F33BC6 (a pinkish color). This color is a combination of
3 two-column hexadecimal numbers back-to-back. These numbers are:
They reflect the amount of red, green, and blue (respectively) in that color. Because these numbers are two-digit hexadecimal numbers, the highest a number can be to reflect one of these colors is
255 (which is FF in hexadecimal).
If you're unfamiliar with how red, green, and blue can combine to make the colors we're familiar with (such as yellow, orange, purple, and much more), it might be worth taking a look at some of the color theory behind it. You can find resources on the topic on Wikipedia and elsewhere.
These numbers, in decimal, are as follows:
And construct the amount of
Blue used to construct that color
Even without seeing a visual representation, you can tell that this color likely has a purple hue - since it has a high percentage of red and blue.
Although hexadecimal has a much more immediately noticeable application with colors, we started this post off with a question: "How does your computer know what letters to display on the screen from only binary?"
The answer to that question is quite complex, but let's answer it in a very simple manner (despite missing a lot of puzzle pieces in a very "draw the owl" kind of way).
Let's take a real way that computers used to (and still do, to some extent) represent letters internally: ASCII. ASCII is an older standard for representing each textual character as a different number inside your computer. Take the following (simplified) chart:
When the user types "This", what the computer interprets (using ASCII) is
You might be wondering, "Why are there a bunch of missing numbers"?
I've removed them to keep the examples simple, but many of them are for symbols (EG:
/, and more), and some of them are for internal key commands that were used for terminal computing long ago that your computer now does without you noticing.
It's also worth mentioning that ASCII (which does have more characters than what's presented here) was eventually replaced in various applications by Unicode and other text encoding formats as it lacks various functionality we expect of our machines today, such as emoji and non-latin symbols (like Kanji). ASCII still sticks around to some extent though, as the first 255 characters in Unicode are the same as they originally were in ASCII.
While I've used the above chart to reflect A as
65, it'd be more accurate to say that your computer interprets the symbol as
01000001 internally. This is again due to the fact that your computer must interpret every number and letter as binary.
While this has been only a high-level overview of how your computer interprets these non-decimal numbers (and some of their applications), it can provide some basic insights to what your computer is doing every time you make a keystroke or see a color on screen. Under the hood, everything is binary, and now you understand the introduction to how to convert binary to numbers you and I may understand better: to decimal!