Binary and Hexadecimal Unlike humans, most of whom have 10 fingers, computers only really have two. They call the first one '0' and the second '1'. After that they have to jump right to '10' then '11'. It is no problem for them to count this way, if we only had two fingers we would be used to counting that way too. It may seem confusing at first, but it is really just because we are so used to counting from 0-9, that this is the case. And no, the reason we sometimes use hexadecimal, 0-15, is not because some computer or person somewhere has 16 fingers. Hexadecimal provides an easy way convert binary to decimal and back again, while also providing a better shorthand notation for big numbers. For example all of the following are the same: Decimal : 3678 Binary : 111001011110 Hexadecimal: E5E As you can see, the binary expression can get long and cumbersome fairly quickly. This is one reason why Hexadecimal is used, to provide a more compact expression. And as you will see in just a bit, converting from binary to hex and back is generally easier than using decimal at all. To avoid confusion, all numbers that follow will be followed by a d,b, or h indicating whether the number was decimal, binary or hexadecimal respectively. Binary First lets look at how binary works with an example of say 35d which is 100011b. If you break down each digit it works like this: 1 0 0 0 1 1 | | | | | | | | | | | 1*2^0=1 | | | | 1*2^1=2 | | | 0*2^2=0 | | 0*2^3=0 | 0*2^4=0 1*2^5=32 Then add them up, 32+0+0+0+2+1=35 Each successive place holder goes up by an exponent of 2. Like so: 2^0 = 1 2^1 = 2 2^2 = 4 2^3 = 8 2^4 = 16 2^5 = 32 2^6 = 64 2^7 = 128 2^8 = 256 2^9 = 512 And so on. So when going from decimal to binary you can see it is fairly straight forward. Just multiply the nth digit which will be either a 0 or 1 by 2^(n-1), then add everything up. When going from decimal to binary, start by subtracting the original number by the largest exponent of 2 and then keep going until you hit zero. For example with 35d, the largest power of 2 to go into it is 2^5 or 32, so we know we need 6 digits. 35-32 = 3 The next digit would be 2^4 or 16 which does not go into 3, neither does 8 or 4. So these place holders will be zeros. Then since 2^1 or 2 does go into 3, that digit will be a 1. 3-2 = 1 This leaves the last digit as a 1 for a result of 100011b Hexadecimal Hexadecimal provides an easy way to present what is essentially binary data in a much more compact form. Hexadecimal goes from 0-F and then starts over again. 0-9 are the same as in our number system and A-F go from 10-15. There is nothing magical about using A-F, whoever decided to use those symbols just thought it worked out easiest. A = 10 B = 11 C = 12 D = 13 E = 14 F = 15 So you can get 16 different combinations with the hexadecimal system or 2^4. It should come as no surprise then that each hexadecimal digit can replace four binary digits. Lets look at the first example, where 3678d = E5Eh. E 5 E | | | | | 14*16^0=14 | 5*16^1=80 14*16^2=3584 Then add up 3584 + 80 + 14 = 3678. Decimal to hexadecimal is just the same as decimal to binary, except you see if powers of 16 can go into a given number instead of powers of 2. One slick trick exists for going from binary to hexadecimal though. Each set of four binary digits converts to just one hexadecimal digit. This is because 2^4 = 16. Let's look once more at the very first example and break it into sets of four binary digits. 1110 0101 1110 E 5 E This will keep you from having to convert from binary to decimal and then to hexadecimal. In fact, hexadecimal would not be used if it did not convert from and to binary so easily. The following table shows the first 16 numbers in all three system. 0 0 0 1 1 1 2 10 2 3 11 3 4 100 4 5 101 5 6 110 6 7 111 7 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F FRACTIONS and DECIMAL POINT In order to store a fraction or use a decimal point, the exponent now is decreased by one power for every digit to the right of the decimal point. So, for the example that we have above, and just inserting a decimal, the exponents change. decimal point || \/ 1 0 0 . 0 1 1 | | | | | | | | | | | 1*2^-3 = 1/8 = 0.125 | | | | 1*2^-2 = 1/4 =0.25 | | | 0*2^-1=0 | | 0*2^0=0 | 0*2^1=0 1*2^2=4 4.375 HOMEWORK: 1.) Convert the following decimal numbers to binary and hex: 167, 82, 89, 831 2.) Convert the following binary numbers to decimal and hex: 101011100, 101100, 11101110011, 11010111 3.) Convert the following hex numbers to decimal and binary: 73, 512, DE, 17E 4.) Convert 179 decimal to base 6 5.) Convert 2334 from base 5 back to decimal 6.) a.) Add in binary 1010111001 and 110101010, convert the result to decimal b.) convert each to decimal and add them, does this match a? 7.) Convert the following fractions from binary to decimal: 10101.1100, 101.100, 1110111001.1, 1101.0111 8.) Convert the following fractions from hex to decimal: 7.3, 51.2 9.) Convert the following decimal fractions to binary: 5.75, 17.1875, 3.33 (only go out four digits for the last one) 10.) Convert the following binary numbers to octal: 101011100, 101100, 11101110011, 11010111 11.) List all possible octal digits. Extra Problems: 12. Convert 176 decimal to base 7. 13. Convert 233 base 5 to decimal 14. Subtract 10111010 from 111001111 (both binary numbers), convert the answer to decimal. Then convert the binary numbers to decimal and do the subtraction. Do you get the same thing? 15. Repeat problem 14, only add the two numbers.