Convert Decimal to Binary in Python

Here's a one-liner code to convert decimal to binary in Python using the built-in bin() function:

binary_number = bin(56)[2:] #Output: 111000

The bin() function converts the decimal number to a binary string with a prefix of "0b", so here use [2:] to slice off the first two characters of the string (i.e. the "0b" prefix) and return the rest of the string as the binary number.

Decimal and Binary

Decimal and binary are number systems used to represent numbers in computer programming. Decimal is a base-10 number system, which means it uses 10 digits (0-9) to represent all possible numbers. Binary is a base-2 number system, which means it uses only two digits (0 and 1) to represent all possible numbers.

There are a few different methods to convert decimal to binary in Python, and the following secion will go over each of them in detail below.

Using the built-in bin() function

The easiest way to convert a decimal number to binary in Python is to use the built-in bin() function. This function takes a decimal number as its argument and returns a string representing the binary equivalent of that number.

decimal_number = 56 binary_number = bin(decimal_number) print(binary_number) # Output: 0b111000

As you can see in the above code, the bin() function returns a string that starts with the prefix "0b". This prefix indicates that the string represents a binary number.

Using a loop and bitwise operators

If you don't want to use the bin() function, you can also convert a decimal number to binary using a loop and bitwise operators. This method involves repeatedly dividing the decimal number by 2 and keeping track of the remainders to build up the binary number.

decimal_number = 56 binary_digits = [] while decimal_number > 0: remainder = decimal_number % 2 binary_digits.append(remainder) decimal_number //= 2 binary_digits.reverse() binary_number = ''.join(str(digit) for digit in binary_digits) print(binary_number) # Output: 111000

In the above example, start by initializing an empty list binary_digits to hold the binary digits. Then enter a loop that continues as long as the decimal number is greater than zero. In each iteration of the loop, calculate the remainder of dividing the decimal number by 2, append that remainder to the binary_digits list, and then divide the decimal number by 2 using integer division (//).

Once the loop is finished, reverse the order of the binary digits in the binary_digits list (since built them up in reverse order) and join them together into a single string using the join() method.

Using the format() method

Another way to convert a decimal number to binary in Python is to use the format() method with the binary format specifier (b). This method takes a decimal number as its argument and returns a string representing the binary equivalent of that number.

decimal_number = 56 binary_number = format(decimal_number, 'b') print(binary_number) # Output: 111000

In the above example, pass the decimal number and the string 'b' as arguments to the format() method. The 'b' format specifier tells Python to format the number as a binary string.

Using the bitwise shift operator >>


how to convert decimal to binary in python

The bitwise shift operator >> is a binary operator in Python (and other programming languages) that shifts the bits of a number to the right by a specified number of positions. The operator is used in conjunction with an integer value, which specifies the number of bit positions to shift the number.

Following is an example code to convert a decimal number to binary using the bitwise shift operator (>>) in Python:

decimal_number = 56 binary_digits = [] while decimal_number > 0: binary_digits.append(decimal_number & 1) decimal_number >>= 1 binary_digits.reverse() binary_number = ''.join(str(digit) for digit in binary_digits) print(binary_number) #Output: 111000

In the above code, start by initializing an empty list binary_digits to hold the binary digits. Then enter a loop that continues as long as the decimal number is greater than zero. In each iteration of the loop, append the least significant bit of the decimal number (i.e. the result of decimal_number & 1) to the binary_digits list, and then shift the decimal number one bit to the right using the right-shift operator (>>=).

Once the loop is finished, reverse the order of the binary digits in the binary_digits list (since built them up in reverse order) and join them together into a single string using the join() method. The resulting string is the binary representation of the original decimal number.