Convert Decimal to Binary in Python
Decimal and binary, two fundamental numbering systems, exhibit distinct characteristics. The decimal system employs base ten digits (0 to 9), whereas the binary system, operating in base two, uses only 0s and 1s.
While decimal is widely utilized, the simplicity of binary makes it prominent in computer programming and related engineering fields. This article explores the Python implementation of the conversion of decimal code to binary. Ensure you are acquainted with the basics of recursion and the concept of if-else statements before delving into the code.
Convert decimal to binary in Python Using Recursive solution
In this sample, we will write the particular function(DecimalToBinary) to implement for obtaining quotients(input to next function call) and the remainder(output value), and then we will call it repeatedly till the input value is greater than and equal to 1
Code:
Output:
Time Complexity: O(log N) - The recursive approach divides the decimal number by 2 in each recursive call, making the time complexity logarithmic.
Convert decimal to binary in Python using an in-built function
In Python, the conversion from a decimal value to its corresponding binary representation can be achieved using the bin() function. This function accepts a value as its parameter and produces its binary equivalent.
Note: bin() returns the binary value with the prefix 0b, so formatting should be done to remove 0b depending on the use case.
Code:
Output:
Time Complexity: - The built-in bin function operates in constant time.
Decimal to Binary in Python Without in-built function
We can also convert Decimal to Binary in another way apart from using the built-in function from Python.
Code:
Output:
Time Complexity: - Similar to the recursive solution, the manual method has a logarithmic time complexity.
Quick Ninja Method
For those seeking an expedited approach to converting decimal to binary in Python, the Quick Ninja Method offers a streamlined solution. This concise technique is ideal for professionals aiming for a rapid and efficient transformation without compromising efficiency.
Code:
Output:
Time Complexity: - The Quick Ninja Method utilizes the bin function, which has a constant time complexity.
Using the bitwise shift operator >>
Another powerful technique for converting decimal to binary in Python involves cleverly using the bitwise shift operator >>. This operator allows us to shift the bits of a binary number to the right, effectively dividing it by 2. Leveraging this property, we can iteratively apply the shift operation to obtain the binary representation of a decimal number.
Here's a concise implementation using the bitwise shift operator:
Output:
In this code, the decimalToBinary function iteratively appends the rightmost bit of the current decimal number to the binary representation. Then, it shifts the decimal number to the right using the >> operator.
Time Complexity: - The bitwise shift operator reduces the decimal number by half in each iteration.
Using built-in format method
The format method within Python provides a flexible and convenient means to convert decimal numbers into their binary representation. This method allows for precise control over the formatting of the output, making it a valuable tool for developers seeking clarity and customization in their binary representations.
Here's how you can employ the format method for decimal to binary conversion:
Output:
In this example, the 'b' format specifier inside the format method instructs Python to represent the given integer as a binary string. This concise one-liner simplifies the conversion process, providing an elegant solution for professionals seeking a balance between simplicity and readability.
Time Complexity: - Similar to the manual methods, the format method has a logarithmic time complexity.
Conclusion
- Most Computers and Digital systems use binary because of their reliable storing of data.
- The Decimal system (base-10) uses a combination of numbers from 0 to 9 to form digits, with each digit worth ten times more than the last digit.
- The Binary system (base-2) uses a combination of 0 or 1 to form digits, each number worth twice the last digit.
- Decimal to Binary conversion is achieved using the Division By 2 technique.
Some of the ways to convert Decimal to Binary in Python are by using a custom recursive function, built-in functionbin(<value>) or using “{0}”.format(int(