Hello, and welome to a lesson on binary!
In this lesson you will, hopefully, learn what binary is, how it works, and why it's useful.
Part 1 - What's Binary?
If you're here you probably already know how to count -- one, two, three, and so on. When you write down those numbers you likely use the numerals 0 through 9, adding a second digit when you get to ten (10), a third for one hundred (100) and so on. This system of representing numbers is known as decimal, because it is based around multiples of ten.
Binary is, much like decimal, a way of representing numbers. The word "binary" just means "relating to, involving, or composed of two things". In this case, those are the two numerals 0 and 1. Using just those two numerals we can represent any whole number (or "integer") we want.
Rather than explain how this works all at once let's count up from 0 in binary and see how it compares to decimal.
We start off just the same way decimal does, with
- 0
- 1
But at this point decimal continues to 2, and we don't have that numeral in binary, only 0 and 1...
Well, then let's shelve binary for just a moment, and see if what decimal does can give us any clues.
Decimal continues,
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Aha! Decimal ran out of unique numerals after 9, so it added a second digit to keep track of tens! (10 = 1 ten + 0 ones)
Let's try this trick with our limit of two numerals. Binary on the left, decimal equivalent on the right:
Ten = two?!
Not exactly. The number on the left is our binary number, and since we don't have a unique numeral for two we instead use a second digit to keep track of how many twos we have, so, in binary, 10 (one zero) = 1 two + 0 ones = 2.
Let's keep going,
It might still be a bit confusing, so let me add markers to show what each digit is keeping track of:
Ok, that makes sense, right? But now what do we do? We've run in to the same issue as before where our first digit has reached its max value, but so has our second digit that we used to take care of that.
Well, perhaps you've already guessed -- we're going to add a third digit to keep track of fours, just like the hundreds place that is used after we reach 99 in decimal!
In decimal, place values (from right to left) go ones, tens, hundreds, thousands, etc. -- each place value ten times as large than the last, this is because of the ten numerals available. In binary we have only two numerals, so each place value is two times as large as the one before; one, two, four, eight, sixteen, etc.
These place values are powers of the system's base. Decimal is a base 10 system, and binary is a base 2 system. In decimal we get 100=1, 101=10, 102=100, etc. and in binary we get 20=1, 21=2, 22=4, and so on. (Any number to the 0th power is equal to 1.)
You might notice that if you write them in binary the place values for binary look just like decimal's do when written in decimal. (1, 10, 100, etc.)
So then what should 5 be?
Good job!
Below is an interactive visualizer for binary numbers, I recommend you play around with it a bit before moving on to the next part of the lesson. Note that leading zeroes on a number don't affect its value at all, 0000001 is the same thing as just 1.
Part 10 - Why Binary?
Writing numbers with just 0 and 1 is all well and good, but what does it have to do with computers?
Well, a lot. While it's technically not the only option, almost all computers work with the two states of off and on, or "low" and "high" which refer to relative voltages. (While ideally you could get 0 volts for your "low" signal, reality is messy and this isn't practical, so in the end what matters is that your "high" signal is higher than your "low".)
These two states of low and high can be viewed on an abstract level as 0 and 1, which means you can use them to represent numbers, and that means you can do math with them. Math, as you may understand, is very important for computers to be able to do.
I have a secret for you, though; binary is more than just a way of representing numbers, that's just one of the useful things we can do with it. Binary can, in fact, be used to represent any sort of data you want. This is true because on a fundamental level all data is the same.
While a collection of binary digits (or "bits") can, and often does represent a regular binary integer the way you learned in part 1, there are other types of data it could be encoding. For example, there are "fixed point" and "floating point" representations, which allow you to represent numbers with a fractional portions (like 1.23). There is also "two's complement" which is a special way of representing both positive and negative numbers in binary that will give you the right answers when you do math with them as if they were regular binary numbers.
Beyond just numbers, binary bits can be used to represent the answers to a collection of yes or no questions -- an interesting side effect of which is the fact that a regular number can also do this, since numbers can be encoded in binary, which will give you a collection of bits.
As an example, files in linux use numbers to control who is allowed to read them, write to them, and execute them; on every file each group gets a number between 0 and 7, which is 3 bits of information, which are then used to answer whether they can read, write, or execute the file -- for example, 5 = 101 = read and execute, all three of these things are just different ways of describing the same data inside the computer, which is doing computations with low and high signals.
Any sort of information can be represented in one way or another as binary, you just have to be clever about it sometimes. The base-2 binary number system is just one very useful way to encode numbers which makes doing math with computers practical.
I hope you enjoyed and learned something ^-^
- Qwerasd