Dev 2# “To Binary” function (Python)

Michael V.
6 min readJun 16, 2020

Let’s create a new function that will convert an integer number into a list of the binary digits that represents the number. Also, we will check alternative syntaxes for our code.

To start with, I am going to create a function called “tobinary” that will accept just one parameter which is going to be the number to be transformed to its binary representation. The data type of this number could be int or str if the number is a string we will transform it to the integer type. We are going to use type annotations later in this note to explicitly restrict the type of variables used in the code we are going to create.

  1. Use def to declare the function.
  2. Name it as tobinary this way we follow the PEP 8 name convention.
  3. Use as a parameter num that is going to be the integer number to be transformed into a list of binary digits.
  4. Define the list variable that will store the binary digits.
  5. Check if the parameter is a string. If the parameter is a string transformed it to an integer number.

Until number 4 our function will look like the following image:

The same code built with different syntax is as follows:

  • int(num) will try to transform a string into a number.
  • isinstance(num, str) will check if num is a string.
  • “digits” is the list to store the binary digits. Do you know what is a binary digit?

Another way to check the type of the variable is like in the next image:

Or making the type checking in only one line:

All the code until this point gives us the same result. So is important to take into account that in programming languages we have lots of ways to write code that gives us the same result. Some times the only thing that changes is the character representation known as the syntax or grammar rules.

Continuing with the function, now we need to implement the algorithm to transform the parameter of the function into a list.

If you divide any integer number by 2 you have only two types of results: another integer number or a decimal number. For example;

  • 1 / 2 = 0.5
  • 2 / 2 = 1
  • 3 / 2 = 1.5
  • 4 / 2 = 2
  • And so forth…

If you got a decimal number, as a result, means that you have a remainder in the division, and that remainder can be only 1, do you know why?. The solution to this question is pretty simple, this is because a number only can be odd or even and nothing else. If the result is an integer the remainder is zero other way is one.

  • 1 / 2 = 0.5 — — — Remainder is 1
  • 2 / 2 = 1 — — — Remainderis 0
  • 3 / 2 = 1.5 — — — Remainderis 1
  • 4 / 2 = 2 — — — Remainderis 0
  • And so forth…

In python and many programming languages, we have the modulus operator that gives us the remainder of a number divided by another. Those remainders of a number divided by two are the binary digits to be stored in our list.

Imagine that you want to convert 9 to binary.

  • 9 / 2 = 4.5 — — — Remainder is 1
  • 4 / 2 = 2 — — — Remainder is 0
  • 2 / 2 = 1 — — — Remainder is 0
  • 1 / 2 = 0.5 — — — We have reached 0.5 and the last remainder is 1

In every step, we take only the resulting integer part for the next division.

So the result is “1–0–0–1” to have the correct answer we have to print the numbers from the last one to the first one.

If you have put attention, probably you have noticed that the thing ends when you reach either 0.5 or 0. In other words, the loop finished when the integer number to be divided is equal to zero, but if it is not zero it continues.

In code it will look like:

The “num % 2” extracts the remainder of dividing num by two, so the line “digits.insert(0, num % 2)” save that remainder at the beginning of the list, remember that the last binary digit should be saved at the beginning of the list or in other words those digits should be saved in the reverse order from how they were obtained.

The num //= 2 statement updates the number with the result of the division of the current value by two, but it only saves the integer part.

For example if num = 3 then num // 2 = 1 not 1.5

The final solution putting everything together can be:

or

Another solution can be (The best option I think):

But if you want to restrict the type:

Line 1 and 2 were used to import two types the Union type and the List type. The Union type means that we can take more that one type like in:

Which means that the num variable can be either integer or string but nothing else.

The arrow expresses the returning type of the function, so here is a list of integers. As you can see with type checking variables in the code are more explicit. We get more information about what a function does and the type of information we are working with.

Digits is the variable that will store a list of integers representing the binary digits that we are going to return from the funtion.

Executing:

We will get:

In c++ avoiding type union complexity the code will loke like:

Type union in c++ is a little more complicated than in python. We will check it another day.

Using c++ also we need a function to print our list something like:

TypeScript will be something like:

Here the union type is created by just writing “number | string” which can be read as “number or string” as the type parameter. And instead of a list here we are using arrays, which is pretty almost the same thing, though not identical things.

Programming languages sometimes have a built-in function to convert a number to another base.

For example in Julia language:

Here the built-in function is string(number, base)

And that is all for now!

--

--