The Binary Number System Acknowledgment and Disclaimer: This presentation is supported in part by the National Science Foundation under Grants 1240841, 1225680, 1225719, 1225745, 1225976, and 1226216. Any opinions, findings, and conclusions or recommendations expressed in these materials are those of the authors and do not necessarily reflect the views of the National Science Foundation. *
Important CS Principle Binary sequences -- sequences of 0s and 1s -- are used to represent all computer data. Numbers Letters and text. Images. Machine language instructions. Sounds and video. Every kind of data you find on a computer.
Binary vs. Decimal System Binary system is similar to the decimal system. Both are positional number systems. 542 is 500 + 40 + 2. Decimal is a base-10 system. 10 digits, 0 through 9 Place values are powers of 10 1s, 10s, 100s, 1000s, etc. Each place is previous place times 10
Binary vs. Decimal System Binary system is similar to the decimal system. Both are positional number systems. 101 is 4 + 0 + 1 = 5 Binary is a base-2 system. 2 digits, 0 through 1 Place values are powers of 2 1s, 2s, 4s, 8s, 16s, etc. Each place is previous place times 2
Why Do Computers Use Binary? In an electronic circuit, it’s easy to represent the difference between a switch being “on” or “off” or a voltage being “high” or “low”. It’s hard to represent 10 distinct states: “really high voltage”, “pretty high”, …, “pretty low”, “really low”.
Amazing Fact By combining 0s and 1s in various combinations we can represent any and all the data that computers use and process!
Converting Binary to Decimal (not hard!) *
Converting Binary to Decimal Here’s a simple algorithm for converting a binary number into its decimal equivalent. Convert binary 10101 to decimal
Converting Binary to Decimal Step 1. Write the binary number with some spaces between its digits. Convert binary 10101 to decimal 1. Write the number with spaces between the digits. 1 0 1 0 1
Converting Binary to Decimal Step 2. Write the binary place values above the number, going right to left: 1s, 2s, 4s, 8s, etc. Convert binary 10101 to decimal 1. Write the number with spaces between the digits. 1 0 1 0 1 2. Write the binary place values above the number, going right to left. 16 8 4 2 1 1 0 1 0 1
Converting Binary to Decimal Step 3. Add up the place values that have a 1 beneath them. Convert binary 10101 to decimal 1. Write the number with spaces between the digits. 1 0 1 0 1 2. Write the binary place values above the number, going right to left. 16 8 4 2 1 1 0 1 0 1 3. Add up all the place values that have a 1 underneath them. 16 + 4 + 1 = 21
Converting Binary to Decimal Step 4. The result, the sum, is the decimal value of the original binary number. Convert binary 10101 to decimal 1. Write the number with spaces between the digits. 1 0 1 0 1 2. Write the binary place values above the number, going right to left. 16 8 4 2 1 1 0 1 0 1 3. Add up all the place values that have a 1 underneath them. 16 + 4 + 1 = 21 4. The result is the decimal value of the binary number 21
Converting Decimal to Binary (not hard!) *
Converting Decimal to Binary Here’s an algorithm for going in the opposite direction, converting a decimal number into binary. Let D be the number we want to convert to binary. Suppose D = 25
Converting Decimal to Binary Step 1: Write down D and the binary place values, 1, 2, 4, 8, 16, and so. And put 0s under each place. Let D be the number we want to convert to binary. Suppose D = 25 1. Write down D and next to it the binary place values with spaces between them with 0s below each place. D = 25 32 16 8 4 2 1 0 0 0 0 0 0
Converting Decimal to Binary Step 2: Find the largest place value that is less than or equal to D and change its 0 to 1. Let D be the number we want to convert to binary. Suppose D = 25 1. Write down D and next to it the binary place values with spaces between them with 0s below each place. D = 25 32 16 8 4 2 1 0 0 0 0 0 0 2. Find the largest place value that is less than or equal to D and change its 0 to a 1. D = 25 32 16 8 4 2 1 0 1 0 0 0 0
Converting Decimal to Binary Step 3. Subtract that place value from D and take the result as the new value of D. Let D be the number we want to convert to binary. Suppose D = 25 1. Write down D and next to it the binary place values with spaces between them with 0s below each place. D = 25 32 16 8 4 2 1 0 0 0 0 0 0 2. Find the largest place value that is less than or equal to D and change its 0 to a 1. D = 25 32 16 8 4 2 1 0 1 0 0 0 0 3. Subtract that place value from D and take the result as a new value for D. D = 9 32 16 8 4 2 1 0 1 0 0 0 0
Converting Decimal to Binary Step 4: Repeat steps 2 and 3 until D is 0. Let D be the number we want to convert to binary. Suppose D = 25 1. Write down D and next to it the binary place values with spaces between them with 0s below each place. D = 25 32 16 8 4 2 1 0 0 0 0 0 0 2. Find the largest place value that is less than or equal to D and change its 0 to a 1. D = 25 32 16 8 4 2 1 0 1 0 0 0 0 3. Subtract that place value from D and take the result as a new value for D. D = 9 32 16 8 4 2 1 0 1 0 0 0 0 4. Repeat steps 2 and 3 with the new value of D until D is 0. D = 1 32 16 8 4 2 1 0 1 1 0 0 0
Converting Decimal to Binary Step 4 (again): Repeat steps 2 and 3 until D becomes 0. Let D be the number we want to convert to binary. Suppose D = 25 1. Write down D and next to it the binary place values with spaces between them with 0s below each place. D = 25 32 16 8 4 2 1 0 0 0 0 0 0 2. Find the largest place value that is less than or equal to D and change its 0 to a 1. D = 25 32 16 8 4 2 1 0 1 0 0 0 0 3. Subtract that place value from D and take the result as a new value for D. D = 9 32 16 8 4 2 1 0 1 0 0 0 0 4. Repeat steps 2 and 3 with the new value of D until D is 0. D = 1 32 16 8 4 2 1 0 1 1 0 0 0 D = 0 32 16 8 4 2 1 0 1 1 0 0 1
Converting Decimal to Binary Step 5: Now that D is 0 we stop. The value of 25 in decimal is 11001 (we drop the leading 0s). Let D be the number we want to convert to binary. Suppose D = 25 1. Write down D and next to it the binary place values with spaces between them with 0s below each place. D = 25 32 16 8 4 2 1 0 0 0 0 0 0 2. Find the largest place value that is less than or equal to D and change its 0 to a 1. D = 25 32 16 8 4 2 1 0 1 0 0 0 0 3. Subtract that place value from D and take the result as a new value for D. D = 9 32 16 8 4 2 1 0 1 0 0 0 0 4. Repeat steps 2 and 3 with the new value of D until D is 0. D = 1 32 16 8 4 2 1 0 1 1 0 0 0 D = 0 32 16 8 4 2 1 0 1 1 0 0 1 5. The resulting number is the binary equivalent of the original value of D. 25 in decimal is 11001.