Teaching Kids Programming – Binary and Decimal Conversion Algorithms


Teaching Kids Programming: Videos on Data Structures and Algorithms

Binary Numbers (Base 2) are the fundamentals of Computer Science. As the Computers are made of Chips which is powered by On/Off and High/Low Voltage.

First Few Binary Numbers are 0, 1, 01, 10, 11… as you can see there are only two types of digits: 0 and 1. Each digit has its own weight, and we can sum them up like we represent the decimals.

For example, 123 in decimal is 1*100+2*10+3*1 thus 101 in binary is 1*(2^2)+0*(2^1)+1*(2^0)=5

Convert Binary to Decimals

We can iterate the digits from left to right, then shift the existing value 1 position to the left (which is equivalent to multiply by two), then add the current value.

1
2
3
4
5
def bin2dec(b):
    ans = 0
    for i in b:
        ans = ans * 2 + int(i)
    return ans
def bin2dec(b):
    ans = 0
    for i in b:
        ans = ans * 2 + int(i)
    return ans

The ans*2 is the same as shifting one position to the left e.g.

1
ans = (ans << 1) + int(i)
ans = (ans << 1) + int(i)

We have to use the parenthese to change the order of execution as the shifting in Python has a lower priority.

In Python, we can use the int(n, 2) to convert a binary to decimal, for example:

1
int("1110", 2) # 14
int("1110", 2) # 14

Convert Decimals to Binary Numbers

We can reverse the process by repeatedly dividing by two and concatenating the modulous in the reverse direction. Then we shift the value one position to the right (which is equivalent to divide it by two).

1
2
3
4
5
6
def dec2bin(d):
    ans = ""
    while d > 0:
        ans = str(d % 2) + ans
        d >>= 1
    return "0" if ans == "" else ans
def dec2bin(d):
    ans = ""
    while d > 0:
        ans = str(d % 2) + ans
        d >>= 1
    return "0" if ans == "" else ans

Lets print the first 16 integers (from 0 to 15):

1
2
3
4
for i in range(16):
    b = dec2bin(i)
    d = bin2dec(b)
    print(b, d)
for i in range(16):
    b = dec2bin(i)
    d = bin2dec(b)
    print(b, d)

0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7
1000 8
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14

In Python, we can use the bin function to convert a decimal integer to binary. The given binary string is prefix with “0b”. For example:

1
bin(14) # "0b1110"
bin(14) # "0b1110"

Here is another implementation to convert decimal number to binary using array:

1
2
3
4
5
6
def dec2bin(d):
    ans = []
    while d: 
        ans.append(d & 1)
        d >>= 1
    return "".join(map(str, ans[::-1]))
def dec2bin(d):
    ans = []
    while d: 
        ans.append(d & 1)
        d >>= 1
    return "".join(map(str, ans[::-1]))

Using array is more efficient than direct string concatenation.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
518 words
Last Post: Algorithms to Check if Intervals are Self-Contained
Next Post: Teaching Kids Programming - Compute the Intersection of the Intervals

The Permanent URL is: Teaching Kids Programming – Binary and Decimal Conversion Algorithms

Leave a Reply