0
0
Intro-computingHow-ToBeginner ยท 2 min read

How to Convert Decimal to Binary - Computing Fundamentals

To convert a decimal number to binary, repeatedly divide the number by 2 and record the remainders; then read the remainders in reverse order to get the binary equivalent.
๐Ÿ“‹

Examples

Input2
Output10
Input13
Output1101
Input0
Output0
๐Ÿง 

How to Think About It

Think of converting decimal to binary like repeatedly splitting a number into halves and noting if there is a leftover 1 or 0 each time. You divide the number by 2, keep track of the remainder (0 or 1), and continue dividing the quotient until it reaches zero. The binary number is the collected remainders read from last to first.
๐Ÿ“

Algorithm

1
Get the decimal number as input.
2
If the number is 0, return '0' as binary.
3
While the number is greater than 0:
4
Divide the number by 2 and record the remainder (0 or 1).
5
Update the number to the quotient of the division.
6
Reverse the collected remainders to form the binary number and return it.
๐Ÿ’ป

Code

computing_fundamentals
def decimal_to_binary(n):
    if n == 0:
        return '0'
    bits = []
    while n > 0:
        bits.append(str(n % 2))
        n //= 2
    bits.reverse()
    return ''.join(bits)

print(decimal_to_binary(13))
Output
1101
๐Ÿ”

Dry Run

Let's trace converting decimal 13 to binary through the code

1

Initial number

n = 13, bits = []

2

Divide and record remainder

13 % 2 = 1, bits = ['1'], n = 13 // 2 = 6

3

Next division

6 % 2 = 0, bits = ['1', '0'], n = 6 // 2 = 3

4

Next division

3 % 2 = 1, bits = ['1', '0', '1'], n = 3 // 2 = 1

5

Next division

1 % 2 = 1, bits = ['1', '0', '1', '1'], n = 1 // 2 = 0

6

Reverse bits

bits reversed = ['1', '1', '0', '1']

Iterationn before divisionRemaindern after divisionbits collected
11316['1']
2603['1', '0']
3311['1', '0', '1']
4110['1', '0', '1', '1']
๐Ÿ’ก

Why This Works

Step 1: Divide by 2 and find remainder

Each division by 2 splits the number into halves, and the remainder tells if the current bit is 0 or 1.

Step 2: Collect remainders

Remainders collected in order represent the binary digits but in reverse order.

Step 3: Reverse to get binary

Reversing the collected remainders gives the correct binary number from most significant bit to least.

๐Ÿ”„

Alternative Approaches

Using built-in function
computing_fundamentals
def decimal_to_binary_builtin(n):
    return bin(n)[2:]

print(decimal_to_binary_builtin(13))
This is the simplest and fastest method but depends on language built-ins.
Recursive method
computing_fundamentals
def decimal_to_binary_recursive(n):
    if n == 0:
        return ''
    return decimal_to_binary_recursive(n // 2) + str(n % 2)

print(decimal_to_binary_recursive(13) or '0')
Uses recursion to build binary string but may be less efficient for large numbers.
โšก

Complexity: O(log n) time, O(log n) space

Time Complexity

The number of divisions is proportional to the number of bits in the binary number, which is about log base 2 of n.

Space Complexity

We store each remainder bit, so space grows with the number of bits, also O(log n).

Which Approach is Fastest?

Using built-in functions is fastest and simplest, while manual division is educational and recursive methods are elegant but less efficient.

ApproachTimeSpaceBest For
Manual divisionO(log n)O(log n)Learning and understanding
Built-in functionO(1)O(log n)Quick conversion in real code
Recursive methodO(log n)O(log n)Elegant code, small inputs
๐Ÿ’ก
Always reverse the collected remainders to get the correct binary number.
โš ๏ธ
Forgetting to reverse the remainders leads to an incorrect binary result.