how does this work?
learn about the world of binary numbers
what is binary?
In our daily lives, we use base 10 to represent numbers. This means that each digit of a number is multiplied by a power of ten, and the digits in each place is between 0 and 9. An example is:
123 = 1 * 10^2 + 2 * 10^1 + 3 * 10^0 = 100 + 20 + 3 = 123
This is a very natural way of representing numbers for us, but this is not how computers represent numbers! Instead, computers use base 2, so each digit of a number is multiplied by a power of 2 and the digits in each place is either 0 or 1. An example is:
101 = 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 4 + 0 + 1 = 5
Do you notice a pattern in what power of 2 is being used in each place? Hint: if we number the places from right to left, then the power of 2 that is the place number! Here's an example:
Consider the number 123 in base 10 (decimal). Then, the 1 is in the 2nd place, 2 is in the 1st place, and 3 is in the 0th place. Computer scientists in general like counting from 0!
In binary, consider the number 110. Then, the first 1 is in the 2nd place, the second 1 is in the first place, and the 0 is in the 0th place.
Try it out! Consider the number 1010. What digit is in which place?
Now that we know how to identify the place numbers of binary numbers, let's try converting a binary number to decimal! Feel free to use a four-function calculator, but don't use an binary converter calculator!
Now, it's time for a challenge! Let's go the other way around. To do this, start from the largest power of 2 that is less than or equal to your target number (we'll call this x), subtract from your target, record the place of x and repeat. Here's an example of what to do:
Convert the number 11 to binary:
The largest power of 2 less than 11 is 8 = 2^3. So we put a 1 in the 3rd place, then subtract 8 from 11. We have 3 left over and our current result is 1 _ _ _, where _s denote numbers we don't know yet.
The largest power of 2 less than 3 is 2 = 2^1. So we put a 1 in the 1st place, then subtract 2 from 3. Since 2^2 = 4 is greater than 3, we put a 0 in the 2nd place. We have 1 left over and our current result is 1 0 1 _.
The largest power of 2 less than or equal to 1 is 1 = 2^0. So, we put a 1 in the 0th place, then subtract 1 from 1. We have 0 left over (so we're done!) and our final result is 1 0 1 1. You can double check your work by converting 1010 to decimal, which is 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 1 * 2^0 = 8 + 2 + 1 = 11.
Now it's your turn to try! Try converting the decimal number to binary:
Awesome job! Now you know what binary is and how to convert to and from decimal. This strategy also generalizes to other bases (i.e. base 3 or base 16), but we won't go over this here. As a general rule of thumb, if something is in base-N, then the digits in each place can go up to N-1, and each digit is multiplied by a power of N.
letters are numbers too?
That's right! Our computer also represents letters, which are called "characters" as numbers. Specifically, there is a conversion method called ASCII. http://www.asciitable.com/ is a great reference for determining which characters map to which numbers. Our converter takes the numbers of each of the characters of your message (including space and punctuation) and converts these numbers into binary. Specifically, we make sure that each character corresponds to 8 digits of binary by adding leading 0s if needed. That way, our decoder knows which digits correspond to which characters.
Unfortunately, this is also why our encoder doesn't support non-ASCII characters. If we convert the characters numbers, then try to convert it back, it might give us different characters back. This is because non-ASCII characters have number codes that are greater than 127, which is the largest number we can represent with 7 bits (each ASCII character is 7 bits).
Let's quickly check your understanding of ASCII representations! Feel free to use the ASCII table linked above. Convert the ASCII character to a numerical representation or the number to an ASCII character:
Awesome job! Now that you know how to convert letters to numbers and numbers to binary, you can put it all together to encode your message by hand. It's a pretty tedious process though, so I'd suggest using the converter instead.
With this information, you should be able to encode and decode your own mesasges by hand! If you have any questions or suggestions on improving this activity, please don't be afraid to reach out to the Illinois WCS officer board at email@example.com. Please include "Babbling Binary Website" somewhere in your subject line!