Learn about different numbers bases,
and how to convert to and from binary.
Completed
What is information?
Communication & History: Ancient Civilizations
Number History & Operators
Today
Number Bases
1s & 0s, all the way down
Upcoming
More Binary
Numbers to Letters and Back
Majority of systems use a base 10 system, not all though. Some of the content earlier this week briefly touched on that; but today we'll continue down that route, with a focus on binary (base 2).
There are a few base systems that are important in computing; specifically binary, octal, and hexadecimal. Binary tends to be more important with hardware, octal is a bit outdated but does pop up occasionally, and hexadecimal is widely used in the web and graphics.
Binary is the most important of the three, at least conceptually. However, most people don't actually do anything with binary in their day-to-day work. You should still have a decent understanding of it though.
Let's learn how we convert to-and-from binary.
Using paper and pencil, convert the following:
1. Decimal to Binary
a. 3
b. 8
c. 16
d. 100
Using paper and pencil, convert the following:
2. Binary to Decimal
a. 101
b. 111
c. 1111
d. 11 0101
Let's see how to do this with code.
I forgot to show you how we can convert from binary to decimal in Python... It's actually really easy... you dont have to do much of anything.
Remember how I emphasized that under the hood its all binary?
When we create a variable, instead of writing a decimal number in, we can write a binary instead.
Keep in mind, it all becomes binary anyway, we just have to communicate to Python that it's a binary number. Binary numbers are specified, at least in Python, with the prefix 0b. So, if the binary number was 10010, we'd write 0b10010. Other languages use this same prefix too, but there may be some out there that do somethign else.
Then, we just tell it to print the variable. Since computers have been programmed to display decimal values for us by default, it'll print as a decimal value. Here's a screenshot to show you how simple it is.
That's literally it. On the left side, you can see I stored the binary number 10 into the variable val1, and on the right side, you can see it printed as 2.
It can be that simple.
Using Python, convert the following:
3. Decimal to Binary
a. 3
b. 8
c. 16
d. 100
Using Python, convert the following:
4. Binary to Decimal
a. 101
b. 111
c. 1111
d. 11 0101