In: Computer Science
Program 5: Decimal to Binary
In Computer Science we often work with different number systems to represent data. The most commonly used number system (which we refer to as "decimal") is a "base 10" number system, which means that we use the values 0-9 to represent all numbers.
The binary number system is a "base 2" number system, which means that we use the values 0 and 1 to represent all numbers. We refer to each value in a binary number as a "bit" (i.e. the number 0101 consists of 4 bits). Here are some numbers represented in both base 10 and base 2:
Decimal | Binary |
0 | 0 |
1 | 1 |
2 | 10 |
3 | 11 |
10 | 1010 |
25 | 11001 |
65 | 1000001 |
127 | 1111111 |
128 | 10000000 |
This is the algorithm for converting a decimal number into a binary number:
Note that each successive bit you compute is for the next highest place (the next highest power of 2) -- so it will be inserted at the front (or the left-end) of the binary number.
For example, consider the following decimal number:
decimal = 5
Here is how the algorithm works on this number:
5 ÷ 2 = 2 remainder 1 Record this bit: 1 5 // 2 = 2 2 ÷ 2 = 1 remainder 0 Record this bit: 01 2 // 2 = 1 1 ÷ 2 = 0 remainder 1 Record this bit: 101 1 // 2 = 0 (algorithm stops)
For this problem, write a program that prompts the user for an integer greater than or equal to zero. If the user supplies a value less than 0 you should re-prompt them. Then convert the number to binary, printing out each step in the process as you go.
Note: Your binary value can't be recorded as an integer. Since you are building the number up bit-by-bit, you will probably need to keep track of 0's that are in higher place values than the the highest 1 bit, in other words, leading zeros. Integers do not support leading 0's (i.e. 0001 will be reduced to 1). Therefore, for this program you should use a String to hold your bits (i.e. "0001" is allowed). You will need to figure out a way how to add bits to your String (hint: concatenation)
Here are a few sample runnings of your program:
Enter a number greater than or equal to 0: -5 Invalid, try again Enter a number greater than or equal to 0: -9 Invalid, try again Enter a number greater than or equal to 0: 9 9 % 2 = 1 ---> 1 9 // 2 = 4 4 % 2 = 0 ---> 01 4 // 2 = 2 2 % 2 = 0 ---> 001 2 // 2 = 1 1 % 2 = 1 ---> 1001 1 // 2 = 0 9 in binary is 1001
Enter a number greater than or equal to 0: 86 86 % 2 = 0 ---> 0 86 // 2 = 43 43 % 2 = 1 ---> 10 43 // 2 = 21 21 % 2 = 1 ---> 110 21 // 2 = 10 10 % 2 = 0 ---> 0110 10 // 2 = 5 5 % 2 = 1 ---> 10110 5 // 2 = 2 2 % 2 = 0 ---> 010110 2 // 2 = 1 1 % 2 = 1 ---> 1010110 1 // 2 = 0 86 in binary is 1010110
Enter a number greater than or equal to 0: 255 255 % 2 = 1 ---> 1 255 // 2 = 127 127 % 2 = 1 ---> 11 127 // 2 = 63 63 % 2 = 1 ---> 111 63 // 2 = 31 31 % 2 = 1 ---> 1111 31 // 2 = 15 15 % 2 = 1 ---> 11111 15 // 2 = 7 7 % 2 = 1 ---> 111111 7 // 2 = 3 3 % 2 = 1 ---> 1111111 3 // 2 = 1 1 % 2 = 1 ---> 11111111 1 // 2 = 0 255 in binary is 11111111
Enter a number greater than or equal to 0: 256 256 % 2 = 0 ---> 0 256 // 2 = 128 128 % 2 = 0 ---> 00 128 // 2 = 64 64 % 2 = 0 ---> 000 64 // 2 = 32 32 % 2 = 0 ---> 0000 32 // 2 = 16 16 % 2 = 0 ---> 00000 16 // 2 = 8 8 % 2 = 0 ---> 000000 8 // 2 = 4 4 % 2 = 0 ---> 0000000 4 // 2 = 2 2 % 2 = 0 ---> 00000000 2 // 2 = 1 1 % 2 = 1 ---> 100000000 1 // 2 = 0 256 in binary is 100000000
This program should be named as follows: LastNameFirstName_assign4_problem5.py (for example, "KappCraig_assign4_problem5.py")
Program
# loop to get the positive number
while True:
number=int(input("Enter a number greater than or
equal to 0: "))
# if number is positive, exit loop
if number>0:
break
# else print message and continue loop
print("Invalid, try again")
binary = '' # string for bianry
n = number # n is number
# loop to calculate binary
while n>0:
# find the quotient by dividing n by 2
quotient=n//2
# find the remainder by modulus n by 2
remainder=n%2
# append remainder to the left of binary
string
binary = str(remainder) + binary
# print quotient and remainder
print(n,"% 2
=",remainder,"--->",binary)
print(n,"// 2 =",quotient)
# assign quotient to n
n = quotient
# print binary representation
print("\n",number,"in binary is",binary)
Program screeshot for indentation reference
Sample outpte 1
Sample output 2
Sample output 3
Sample output 4