Home Python Course #4: Introduction to Binary Numbers (Converting Decimal to Binary)
Post
Cancel

Python Course #4: Introduction to Binary Numbers (Converting Decimal to Binary)

After you have learned about Boolean algebra in Python Course #3: Introduction to Boolean Algebra it is time to talk about the binary system and binary numbers based on Boolean algebra. Everything stored on your computer is stored in a binary representation. When you plan on working with embedded systems like Arduino or Raspberry Pi, understanding binary numbers is crucial to making your projects successful.

Continuous vs. Discrete

The storage of a computer, be it a hard drive (HDD), solid-state drive (SSD) or memory (RAM), is fundamentally built out of tiny little cells that store an electric or magnetic charge. No charge in such a cell usually represents a 0 (or false when thinking back at Boolean algebra), and when a charge is present, it is interpreted as 1 (or true). Those ones and zeros are called bits.

RAM Bits

But how can those arrays of ones and zeros be used to store complex information? Before answering this, it is essential to talk about the difference between continuous and discrete data.

The classic physics we perceive in our daily life is continuous. Think about a car with a constant acceleration \(a\), the velocity of the car is described as a function:

\[v = v_0 + a\cdot t\]

whereas \(v_0\) is the initial velocity. And the distance \(x\) traveled over time by this car is defined as:

\[x = v_0t + \frac{1}{2}at^2\]

When looking at a plot depicting the car’s motion, you can see that all three plots are “smooth,” meaning that there are no gaps in the graphs and that there are no sharp points.

Car Accelerated Motion Continous

When you want to accelerate your car to a certain velocity, you have to start from 0 and go through all values in between. There is no way to accelerate an object from 0 m/s to 10 m/s in an instant. This property is called continouos (there is actually a mathematical definition but this rough description sufices for the point I want to make).

Now think about how you would draw such a plot by hand when you only got a calculator, a piece of paper, and a pencil? You would most likely enter different values for \(t\) and draw markers for every point in your plot:

Car Accelerated Motion Discrete

This is called discrete. Because there are only values defined for specific values of \(t\), everything stored in a computer is discrete because it is composed of arrays of ones and zeros.

Representing a Number with only 1 and 0

All the numbers you interact with, for instance, on your bank account, the speed of a car at a fixed point in time, or the number of your friends, are decimal. And decimal means those numbers are base 10 because every digit from left to right represents a range that is 10 times greater than the number before. Take, for example, the number 2345 it is composed out of:

\[2000 + 300 + 40 + 5 = 2345\]

Going from right to left, a zero is added to the end of every number. This adding of zeros can also be expressed as \(10^x\) where \(x\) is the digit’s position starting at 0 from the left. Now you can express 2345 like this (it is important to remember that \(y^0 = 1\)):

\[2\cdot 10^3 + 3\cdot 10^2 + 4\cdot 10^1 + 5\cdot 10^0 = 2345\]

In front of every \(10^x\) you can place one of the ten digits 0-9. So every decimal number that has \(n\) digits can be expressed as:

\[\sum_{i=0}^n a_i\cdot 10^i\]

But what do you do when you only have 0 and 1? You build a number system of base 2, also called binary, where every digit of a number represents a number from the series 0, 1, 2, 4, 8, 16, 32, 64, etc. that are all results of \(2^x\). So if you want to represent the decimal number 23 in binary, you take the sum of \(1\cdot 16\), \(0\cdot 8\), \(1\cdot 4\), \(1\cdot 2\), and \(1\cdot 1\):

\[1\cdot 16 + 0\cdot 8 + 1\cdot 4 + 1\cdot 2 + 1\cdot 1 = 1\cdot 2^4 + 0\cdot 2^3 + 1\cdot 2^2 + 1\cdot 2^1 + 1\cdot 2^0= 23\]

the 1 and 0 in front of the \(2^x\) are the digits for the binary representation of 23:

\[10111\]

When working with numbers from different number systems, it can be confusing what number is from which system. You can indicate the number system by adding a small subscript number at the end that represents the base:

\[23_{10} = 10111_2\]

With this representation, you can store any number as strings of 1 and 0 on your computer.

How to Converting Decimal Numbers into Binary Numbers?

To convert a number represented in decimal to binary, you can use the following Python print(...) statement with a format string (indicated by the f in front of the first "):

1
2
x = 3
python(f"{x:b}")

The x enclosed in braces { } means that the value of x should be printed as part of the string between quotes. The b after the colon tells Python to print x as a binary number. But how do you convert a decimal number using only pen and paper? Let’s use example 23 again. For the conversion, you have to divide 23 by 2 with the remainder until the result of the division is 0. And while you divide, keep track of the remainder in every step:

\[\begin{eqnarray} \frac{23}{2} &= 11 & \quad R:1\\ \frac{11}{2} &= 5 & \quad R:1\\ \frac{5}{2} &= 2 & \quad R:1\\ \frac{2}{2} &= 1 & \quad R:0\\ \frac{1}{2} &= 0 & \quad R:1\\ \end{eqnarray}\]

When you reach the division result 0, write down the remainders from bottom to top. Those are the digits of the binary representation: \(10111\). Try it out yourself with 2345 and check your result in Python.

How to Convert Binary Number into Decimal?

When you get a binary number and want to know its decimal representation, you can also use Python. Enter your binary number with 0b in front and then print it:

1
2
x = 0b101
print(x)

To convert a binary number into a decimal number by hand, you can make use of the general mathematical representation of binary numbers:

\[\sum_{i=0}^n a_i\cdot 2^i\]

Take every digit and multiply it with corresponding \(2^i\) and sum the up. Remember that the last bit on the left in on postition 0 (also called the least significant bit or LSB). Lets take 101011 and find out it’s decimal representation:

\[1\cdot2^5 + 0\cdot 2^4 + 1\cdot 2^3 + 0\cdot 2^2 + 1\cdot 2^1 + 1\cdot 2^0 = 32 + 0 + 8 + 0 + 2 + 1 = 43\]

Converting binary numbers into the decimal system concludes this article. You’ve learned that computers store values only in the form of ones and zeros, the difference between continuous and discrete data, and that computers can only store discrete data. Additionally, you’ve learned how to convert a decimal number into a binary number and the other way around. I encourage you to try out the conversion on your own with the number:

\[1011010111\]

If you have any questions about this article, feel free to join our Discord community to ask them over there.

This post is licensed under CC BY 4.0 by the author.