FOUNDATIONS OF PROGRAMMING : CODES AND OBJECTS : 05.01 NUMBER SYSTEMS
Number Systems: Converting User Input
# Number Systems def main(): # User input b = "0b" + input("Enter a binary number: ") print("Your binary number is: " + b) # Convert a binary number to a decimal # int(the number, the base) d = int(b, 2) print("The decimal equivalent is: " + str(d)) main()
Binary System
Binary System Base: 2 Description: Binary numbers are what computer language is made of. They are based on two digits: 0 and 1. The combination of 0s and 1s can be converted into other types of numbers or even words! Example: You can use the binary system numbers to write other types of numbers or words: 00110001 00110010 00110011 (the binary for the decimal 123).
Comparing Number Systems
Comparing Number Systems Use this chart to compare the different number systems. The decimal system contains the numbers humans find most familiar. With binary numbers, do you see a pattern with the digits 1 and 0? The hexadecimal numbers count highest while using only one digit. Decimal Binary Hexadecimal 0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 Decimal Binary Hexadecimal 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F
Number Systems: Converting Hexadecimal
# Number Systems def main(): # Assign a decimal number to d d = 156 print("The decimal value is: " + str(d)) # Convert d to a binary number h = hex(d) print("The hexadecimal value is: " + h) # Convert a binary number to a decimal # int(the number, the base) d = int(h, 16) print("The decimal value again: " + str(d)) main() Output: The decimal value is: 156 The hexadecimal value is: 0x9c The decimal value again: 156
Number Systems: Converting from Binary to Decimal
# Number Systems def main(): # Assign a decimal number to d d = 7 # Convert d to a binary number b = bin(d) print("The binary value is: " + b) # Convert a binary number to a decimal # int(the number, the base) d = int(b, 2) print("The decimal value is: " + str(d)) main() Output: The binary value is: 0b111 The decimal value is: 7
Number Systems: Converting from Decimal to Binary
# Number Systems def main(): # Assign a decimal number to d d = 7 print("The decimal value is: " + str(d)) # Convert d to a binary number b = bin(d) print("The binary value is: " + b) main() Output: The decimal value is: 7 The binary value is: 0b111
Number Systems: Side-by-side Comparison
# Number Systems def main(): # Print Table Headings print(" Number Systems Chart ") print("Decimal Binary Hexadecimal") # Print body of table for d in range (0, 16): b = bin(d) h = hex(d) print(" " + str(d) + " " + b + " " + h) main() Output: Number Systems Chart Decimal Binary Hexadecimal 0 0b0 0x0 1 0b1 0x1 2 0b10 0x2 3 0b11 0x3 4 0b100 0x4 5 0b101 0x5 6 0b110 0x6 7 0b111 0x7 8 0b1000 0x8 9 0b1001 0x9 10 0b1010 0xa 11 0b1011 0xb 12 0b1100 0xc 13 0b1101 0xd 14 0b1110 0xe 15 0b1111 0xf
examples of how you can convert binary numbers to decimals using your hands.
Binary for 31 If you lifted all of your fingers, that would be 11111. That's binary for the decimal number 31. (Hint: Add up all the numbers you wrote on your fingers that are showing...what do you get?) Binary for 30 With these fingers lifted, you're showing 11110. If you add up the numbers written on your fingers, you get 30, which is the decimal number for 11110. Binary for 6 If you lifted your fingers with 4 and 2 written on them, that would be 00110, which is the binary number for 6. Binary for 3 If you lifted just your thumb and second finger, you'd be showing 00011, which is the binary number for 3. Binary for 1 If you kept all fingers down except the finger with the decimal number 1 drawn on it, you'd be showing 00001, the binary number for 1. Can you come up with the binary number for 12? Answer Hold up the finger with the number 8 and the finger with the number 4. You're actually holding up 12, right? This is because 8+4 = 12. Now look at your fingers. You should have 01100 because your second and third fingers are up, but the rest are down. That's the binary for the number 12!
Converting in Python
Converting in Python Image of binary to decimal and hex conversion © Shutterstock.com Now you understand the different number systems, but you can probably tell they take a while to figure out in your head. That's just another area where Python can save the day. Using the bin() and hex() functions, you can get Python to make conversions for you! Take a look at this simple code: age = 17 binaryAge = bin(age) print(binaryAge) Let's try converting different number systems using the bin() and hex() functions in the Python Guided Activity.
Decimal System
Decimal System Description: Probably the most familiar number system to humans, decimals are based on ten digits, from 0-9. Example: Money uses the decimal system, as all amounts use numbers based on the ten-digit system, such as $1.50.
Hexadecimal System
Hexadecimal System Base: 16 Description: Hexadecimal numbers are common in the computer world, most often used in relation with colors and memory locations. They are based on sixteen digits, from 0-9 and A-F. Example: All colors have a hexadecimal code, the # symbol followed by a string of numbers and letters. #FFD700 is the hexadecimal code for the color gold.
Number Systems
Number Systems Our world runs on two things: language and mathematics. We use language to communicate, and we use math to...do pretty much everything else. Think about it. If you're an architect, you need words to describe what you're doing and you need numbers to make it work. If you're a musician, words come in handy for lyrics but numbers arrange the music and set the tempo. In programming, the type of number used depends on the type of data. Each number system has a different base, which is the number of different digits it uses. For instance, the decimal number system is base 10, which means it uses 10 digits (0-9). Check out some different number systems and how they're used.
Converting Systems
The great thing about the different number systems is that you can convert among them. If you have one type of data in decimal form, but you need to use a system that reads only binary (like, say, a computer), you can do that. In the decimal number system, each place value is a power of ten. The ones column is 10 to the power of 1. The hundreds column is 10 to the power of 2. And so on. The same is true for binary. One easy way to convert is to use an online calculator. There's also a nifty way to do conversions on your own, sans computer.
Binary to Hexadecimal
The hexadecimal system is comprised of 16 symbols: 0-9 and A-F. This number system has a base of 16, which makes it ideal to use as a way to represent a binary number, but with fewer digits. That's because each binary number is considered a bit, and four bits can be represented with one hexadecimal number. Take a look at the breakdown: Binary: 11000111 1100 in binary is 12 in decimal, which is C in hexadecimal. 0111 is binary for 7 in decimal, which is 7 in hexadecimal. (Binary) 11000111 = C7 (Hexadecimal) The good news is that hexadecimal numbers are much shorter to write than binary numbers. This can be a huge time saver! Take a look at the binary and hexadecimal numbers for the decimal value 15—which would you rather type out? Binary 1111 Hexadecimal F