# Decimal to Binary in Python

In the previous article we discussed how a string can be reversed, now let us explore number system conversions in Python. We are given a non-negative integer, our task is to write a function that takes the integer as an argument and then returns the binary value. The result should be stored in a string for convenience.

What is binary value? Our computer stores and processes everything in the form of binary and in the binary all data is expressed in either 0 or 1. It is important for us to understand the decimal to binary conversion process. We can write any decimal as sum of powers of two. For example:

12 can be written as 8 +4
17 can be written as 16+1

If we are given 0 as input however, we can simply write 0 as output. The powers of 2 are a complete sequence, another example of complete sequence is Fibonacci sequence. Any number can be expressed through Fibonacci sequence digits. Let us discuss a naïve solution to solve the problem, and this is the solution we use in our minds to convert decimal numbers to binary. First we find out if the highest power of 2, whether it is smaller or equal to the given number. For example:

If we are given the number 17, let us consider the powers of 2: 1/ 16, 0/8, 0/4, 0/2. 16 would be the highest, beyond 16 it would be 32 which would be higher than 17. So we write 16 first and then subtract 16 from 17 to get the remaining number as 1.

if we take 12, : 1/8 and 1/4, 0/2 and 0/1 the highest number less than or equal is 8. Once we subtract 8 from 12, we obtain 4 as the value. The highest factor greater than or equal to 4 is 4, so we write it as 8+4.

### Writing a program to handle conversion of Decimal to Binary in Python

For an efficient solution to the problem, the idea is to start from the lowest power rather than the highest power because it would be easier to find out the values for lower powers. For example, we can easily find out what would be the last bit if the number is even then the last bit would be zero and if the number is odd, the last bit is going to be 1. We can quickly check based on this condition by remainder of 2 (If n%2==0, then the number is even). We can apply this process repeatedly.

```def decToBinary(n):
if n==0:
return "0"
res=""
while n>0:
res=res+str(n%2)
n=n//2
return res[::-1]

n=int(input("Enter decimal number to convert into binary value : "))

print(decToBinary(n))
```
``````Output:
Enter decimal number to convert into binary value : 6
110``````

The logic is based upon the earlier even or odd logic, and remainder concept. We take a number as input, if the number is zero we immediately return 0, however if n is greater than 0.. we run a loop and within the loop we keep on dividing n by 2. First iteration we check the remainder for 2, second iteration we check the remainder for 4 and so on. In the third iteration, the number is divided by 8. We keep doing it while n is greater than 0. Finally all the bits together are obtained in reverse order, that is why while returning the result we return the reverse of it using the expression [:: -1].

However, there is also another alternative method to handle this program through the use of in-built functions.

### Alternative method to handle conversion of Decimal to Binary in Python

```def decToBin(n):
res=bin(n)
return res[2:]

n=int(input("Enter decimal number to convert into binary value : "))
print(decToBin(n))
```
``````Output:
Enter decimal number to convert into binary value : 6
110``````

The bin( ) function converts a given integer value into it’s equivalent binary representation. However, normally by default the bin function adds the prefix ‘0b’ to every number it returns which is supposed to represent the binary notation. In the next article we will look at Binary to Decimal conversion in Python.

### 1 thought on “Decimal to Binary in Python”

1. Howdy! I could have sworn I’ve been to this website before but after browsing through some of the posts I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!