Chapter 7 Lab - Decimal, Binary, Octal, Hexadecimal Numbering Systems This assignment is designed to familiarize you with different numbering systems, specifically: binary, octal, hexadecimal (and decimal) and converting between them. This lab can be printed out directly or downloaded and printed. Click here for a printable PDF version of the lab. You can write your responses directly on the printed sheet. To print this sheet directly: make sure you have a printer available on your machine or network. In the browser, drag down the File menu and click on print. To download, drag down your browser’s File menu and select Save as… Then save the file with whatever name you wish to your machine. You can then print the file. Note on formatting used below: 1 x 2 ^ 2 equals 1 times 2 raised to 2. The notation 1*2**2 is the same thing. You might see this in a computer language. In the spirit of being well- rounded both will be used here. First, a quick review of how to do number conversions. Decimal: base 10 (0 1 2 3 4 5 6 7 8 9) Binary: base 2 (0 1) Octal: base 8 (0 1 2 3 4 5 6 7) Hexadecimal: base 16 (0 1 2 3 4 5 6 7 8 9 A B C D E F) Our handy basic number conversion table: Base 10 base 2 base 8 base 16 Decimal binary octal hex(adecimal) 0 0 0 0 1 1 1 1 2 10 2 2 (base 2 is at 2 digits already) 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 8 1000 10 8 (base 8 just went to 2 digits) 9 1001 11 9 ------------------------------------------------------------ (base 10 goes to two digits) 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F (beyond this base 16 goes to 2 digits) Let’s look at the value of digit “1” at PLACE given the four numbering systems: POWER (place) 4 3 2 1 0 Base 10 Base 2 Base 8 Base 16 10000 16 4096 65536 1000 8 512 4096 100 4 64 256 10 2 8 16 1 1 1 1 For example: POWER 4 (place) 3 2 1 0 Final result in Decimal Base 10 of 10001 1 x 10 ^ 4 = 10,000 0 x 10 ^ 3 =0 0 x 10 ^ 2 = 0 0 x 10 ^ 1 = 0 10000 + 0 +0+0+1 = 10001 Base 2 of 10001 1x2 ^4= 0x2^3 16 = 0 0x2^2 = 0 0x 2 ^ 1 = 0 Base 8 of 10001 1x8^4= 4096 0x8^3 = 0 0x8^2 = 0 0x8^1 = 0 Base 16 o 10001 1 x 16 ^ 4 = 65536 0 x 16 ^ 3 = 0 0 x 16 ^ 2 = 0 0 x 16 ^ 1 = 0 1 x 10 ^ 0 = 1x1= 1 1x2^0 = 1x1= 1 1x8^0 = 1x1= 1 1 x 16 ^ 0 = 1x1= 1 16 + 0 + 0 +0+1= 17 4096 + 0 + 0+0+1= 4097 65536 + 0 +0+0+1 = 65537 Going from binary/octal/hexadecimal to decimal: An easy way to remember it: take the number and raise the base to the place binary to decimal example 101 = 1*2**2 + 0*2**1 + 1*2**0 = 4 + 0 + 1 = 5 That’s 1 times the base (of 2) raised to the place (2) PLUS 0 times the base (again, 2) raised to the place (1) PLUS 1 times the base (still 2) raised to the place (0) octal to decimal example 15 = 1*8**1 + 5*8**0 = 8 + 5 = 13 That’s 1 times the base (of 8) raised to the place (1) PLUS 5 times the base (again, 8) raised to the place (0) hex to decimal examples 10 = 1*16**1 + 0*16**0 = 16 That’s 1 times the base (of 16) raised to the place (1) PLUS 0 times the base (yup, 16) raised to the place (0). 1A = 1*16**1 + 10*16**0 = 26 That’s 1 times the base (of 16) raised to the place (1) PLUS 10 (A is 10 in decimal) times the base (of 16) raised to the place (0). With hex you have to remember what the decimal equivalent of the numbers are using our handy chart above. Going from Decimal to Binary (and octal and hex) One way is to work with the remainder – continually dividing the initial number and then resulting numbers by the base until it cannot be divided without fractions. Then take the remainders, beginning with the first one produced, and write them out from right to left. Example of decimal to binary: Take the decimal number 100 to convert to binary. The decimal number is divided by the base (2) to find the quotient and remainder. The number 100 / 2 = 50 with remainder of 0 The number 50 / 2 = 25 with remainder of 0 The number 25 / 2 = 12 with remainder of 1 The number 12 / 2 = 6 with remainder of 0 The number 6 / 2 = 3 with remainder of 0 The number 3 / 2 = 1 with remainder of 1 The number 1 / 2 = 0 with remainder of 1 Now from the top take the remainders and write from right to left (or from the bottom up and write left to right). That gives us the binary number: 1100100 Example of decimal to octal: Take the decimal number 100 to convert to octal. The decimal number is divided by the base (8) to find the quotient and remainder. The number 100 / 8 = 12 with remainder of 4 The number 12 / 8 = 1 with remainder of 4 The number 1 / 8 = 0 with remainder of 1 Now from the top take the remainders and write right to left. That gives us the octal number: 144 Another easy way for smaller numbers is using the place table. We know for each place in base 2 we are multiplying by two. We have: PLACE 6 5 4 3 2 1 0 Decimal 64 Equivalent if “1” present Total of 64 all these numbers added together = 127 Binary 1 equivalent (a 6 digit binary number) 32 16 8 4 2 1 32 16 8 4 2 1 1 1 1 1 1 1 So, for example, how would you represent 45 decimal in binary? PLACE 6 Decimal 64 Equivalent if “1” present 45 = these numbers added together Binary 0 equivalent, Binary:0101101 5 32 4 16 32 1 0 3 8 2 4 8 4 1 1 1 2 0 1 1 0 1 So, ever wonder why everything in computers is 16, 32, 64, 128, 256, 512, etc? Now it should be making a lot more sense. The computer, of course, is using binary numbers. Going from binary to octal/hex and back (recall our basic conversion table) Base 10 base 2 base 8 base 16 Decimal binary octal hex(adecimal) 0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 8 1000 10 8 9 1001 11 9 ----------------------------------------------------------------10 1010 12 A 11 1011 13 B 12 13 14 15 1100 1101 1110 1111 14 15 16 17 C D E F Octal is every three bits starting from right- most digit. Example: 10010011 = 10 010 011 (grouping the digits three at a time from the right- most digit) NOW, simply find the octal value for each grouping of three in order from left and that is the final result– BINARY OCTAL 10 2 010 2 011 3 10010011 223 …you can do the same thing going backwards to binary. Hex works the same way. But, it is every 4 bits starting from right-most digit. Example: 10010011 = 1001 0011 BINARY OCTAL 1001 9 0011 3 10010011 93 Questions Ok, time for you to do a little work. Use the number conversion program to check your answers. You must show your work. 1. Take the following decimal numbers and convert them to binary, octal and hexadecimal: 20 35 127 245 768 2. Take the following binary numbers and convert to octal, hex, and decimal: 011001 011101 0110 1010101 3. Take the fo llowing octal numbers and convert to binary and hex: 77 35 20 467 4. Take the following hex numbers and convert to binary and octal: 1F FE A23 A1B2C3