NUMBER SYSTEMS: TUTORIAL This tutorial explains the different number systems, converting a number which is represented in a specific number system to its value in another number system and number systems arithmetic. The "base" of number systems is determined by how many numerical digits the number system uses: Binary Octal Hexadec (base2) (base8) (base 10) Study the table below: Decimal Hexadecimal Octal Binary 0 0 0 0000 1 1 1 0001 2 2 2 0010 3 3 3 0011 4 4 4 0100 5 5 5 0101 6 6 6 0110 7 7 7 0111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 12 C 14 1100 13 D 15 1101 14 E 16 1110 15 F 17 1111 Decimal uses 0-9 (base 10), hexadecimal uses 0-F (base 16) octal uses 0-7 (base 8) and binary uses only 0 or 1 (base 2) This tutorial covers 3 number systems: Binary, Hexadecimal and Octal Well, 4 if you include decimal, but we all know decimal arithmetic, hopefully. Firstly let's look at binary. The Binary Number System Firstly, in order to understand how to convert number systems, we need to understand the relationship of the digits in a number and their position of those digits for their base number. Let's use the decimal number system (base 10), one that we're all familiar with: 103 102 101 100 2 4 6 1 Now we all know by looking at that number that it's Two thousand, 4 hundred and sixty one. But how do we know? We know by the number's position, we can find this value like this: 1 2 x 103 = 2 x 1000 = 2000 4 x 102 = 4 x 100 = 400 6 x 101 = 6 x 10 = 60 1 x 100 = 1 x 1 = 1 + ---2461 Apply this to the binary system and we can easily convert a binary value to its decimal form Binary to Decimal Conversion Let's do the same we did above, only this time with the base 2 number system 23 22 21 20 1 1 0 1 We have the binary number 11012 (the little 2 represents the base number). Let's convert it to decimal: 1 x 23 = 1 x 8 = 8 1 x 22 = 1 x 4 = 4 0 x 21 = 0 x 2 = 0 1 x 20 = 1 x 1 = 1 + ---13 So the 4-bit binary number 1101 is equal to 13 in decimal. Let's convert an 8-bit binary number. 27 26 25 24 23 22 21 20 1 0 0 1 1 1 1 0 So now we have the binary number 100111102 1 x 27 = 1 x 128 = 128 0 x 26 = 0 x 64 = 0 0 x 25 = 0 x 32 = 0 1 x 24 = 1 x 16 = 16 1 x 23 = 1 x 8 = 8 1 x 22 = 1 x 4 = 4 1 x 21 = 1 x 2 = 2 0 x 20 = 0 x 1 = 0 + ---158 Pretty simple to grasp, make up a few binary numbers and practice on them. You can check your answers using a scientific calculator. If you're using windows go to start > programs > accessories > calculator. At the top go to "view" and pick "scientific". Decimal to Binary Conversion Converting a decimal number to its binary equivalent is a little fiddlier than vice-versa, but it isn't too bad. We repeatedly divide the decimal number by 2 (the base number of the binary system) collecting the remainders. If we divide an even number by 2 there is no remainder, if we divide an odd number by 2 there is a remainder of 1. Decimal number: 36 2 36 / 2 = 18 - remainder 0 18 / 2 = 9 - remainder 0 9 / 2 = 4 - remainder 1 4 / 2 = 2 - remainder 0 2 / 2 = 1 - remainder 0 1 / 2 = 0 - remainder 1 Now collect the remainders from the bottom up and we have the binary number: 1001002 = 3610 Isn't so bad is it? Let’s do one more to drill it in. Decimal number: 227 227 / 2 = 113 - remainder 1 113 / 2 = 56 - remainder 1 56 / 2 = 28 - remainder 0 28 / 2 = 14 - remainder 0 14 / 2 = 7 - remainder 0 7 / 2 = 3 - remainder 1 3 / 2 = 1 - remainder 1 1 / 2 = 0 - remainder 1 Binary equivalent: 22710 = 111000112 Not too bad, try it on some numbers of your own and check your answers with a calculator or whatever. Binary Arithmetic Addition Now before we start there are four rules to remember: 0+0=0 0+1=1 1+0=1 1 + 1 = 10 Now they all seem pretty normal, except for the last one. Well when we're adding in decimal we don't carry a number onto the next column until the addition of the numbers in that column exceeds 9 (the highest singledigit value in decimal). 14 17+ -211 = 31 We carry the extra 1 from 11 to the next column and add it to 2 giving us 31, the same applies in binary, only we carry the one when the addition value exceeds 1 (the highest single-digit binary number). 1 1+ -10 = 10 We carry the one over to the next column making 10. Now another rule to remember is: 1 + 1 + 1 = 11 With this in mind, let's add two binary numbers together. 3 1101 1001 + ----10110 Let's go through it from the right column to the left. 1 + 1 = 10, carry the one over to the next column which is 0 + 0 = 0, but we have the carried 1, so it's 1. We have 10. 1 + 0 = 1, no carry. We have 110. 1 + 1 = 10, carry the one to the next column and we have 10110 2 Multiplication Binary multiplication is very easy; we use the same rules as we do in decimal multiplication. Let's look at an example. 101 10 x ---000 1010 + ---1010 Now to explain, firstly we multiply the binary number on the top by the first digit on the right, on the bottom. 101 x 0 = 000 (anything x 0 is 0), then we write the result. The we multiply the top number by the second digit on the right, on the bottom. 101 x 1 = 101 (anything x 1 is itself) we then write the result under the first result, but we shift it one place to the right and fill in the missing space with a zero. We then add the values together and we have the result of the multiplication. Let's do one more. 1011 1001 x ------1011 00000 000000 1011000 + ------1100011 Follow these steps: Copy the numbers if the bottom multiplier is 1, or else just write a row of zeros. Move the result one column to the left as you move onto a new multiplier. Add the results together using binary addition to find the answer. That about wraps up binary, there is also converting binary numbers to hexadecimal and octal, but we'll not get into that until we have got used to hexadecimal and octal. Hexadecimal Hexadecimal is base 16, it uses 16 unique digits to represent values. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F 4 Remember that if we compare this to decimal, A is 10, B is 11, C is 12, D is 13, E is 14 and F is 16. We need to remember this when converting hexadecimal to decimal. Hexadecimal to Decimal Conversion We do this in the same way we convert binary to decimal, only we use hexadecimal's base number (16). 161 160 3 E Easy enough, now we just multiply the values as we did with binary. Hex Number: 3E 3 x 161 = 3 x 16 = 48 E is equal to 14 decimal, see above. 14 x 160 = 14 x 1 = 14 48 + 14 = 6210 Now with a larger number we do the same as in binary again. 163 162 161 160 5 E B 7 5 x 163 = 5 x 4096 = 20480 14 x 162 = 14 x 256 = 3584 11 x 161 = 11 x 16 = 176 7 x 160 = 7 x 1 =7 add the results and we get: 24247 Decimal to Hexadecimal Conversion This, again, is similar to decimal-to-binary converting, only, again, we use hexadecimal's base number. Remember we repeatedly divided the decimal number by 2 (binary's base) and collected the remainders. We do the same here, only we divide by 16 (hex's base). Decimal number: 97 97 / 16 = 6 - remainder 1 6 / 16 = 0 - remainder 6 remainders from BOTTOM UP: 61 That's it, this is trickier to do the sums in your head, especially when dealing with larger numbers, let's do a larger one. Decimal Number: 934 934 / 16 = 58 - remainder 6 58 / 16 = 3 - remainder 10 3 / 16 = 0 - remainder 3 remember, 10 in decimal is A in hex 5 remainders in reverse order: 3A6 It's as simple as that. Hexadecimal and Binary A very simple conversion, we simply use this table: Binary Hex 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F There you go, the conversions are in front of you. Binary number 100101102 now we split the binary number into groups of four. 1001 - 0110 Now we refer to the above table, look up each 4-bit binary value and find its corresponding hex value. 1001 = 9 0110 = 6 Hex: 96 This is the easiest conversion, all you need is that table. But what if the binary number can't be split equally into groups of four? Binary: 1110101101 11 - 1010 - 1101 We simply add zeros on the left until it is four digits. 0011 - 1010 - 1101 hex: 3AD 6 This works the same the other way, just split the hexadecimal value into digits and look up its corresponding binary value. hex: 6AF 6 - 0110 A - 1010 F - 1111 binary: 0110 1010 1111 we can lose the zero on the left: 11010101111 There we go, that wraps it up for hex. Octal We do octal to decimal the same way we do bin and hex to decimal. 81 4 80 6 4 x 81 = 4 x 8 = 32 6 x 80 = 6 x 1 = 6 32 + 6 = 3810 I'm sure you get the drift of that, it's the same as the other number systems to decimal conversions. Decimal to Octal This again is the same as decimal to bin and decimal to hex, we just repeatedly divide the decimal value by octal's base number (8) and collect the remainders in reverse order. Decimal: 76 76 / 8 = 9 - remainder 4 9 / 8 = 1 - remainder 1 1 / 8 = 0 - remainder 1 octal = 114 if you've played around converting the other number systems to decimal you should have the hang of this by now. Octal and Binary Remember how we converted hex to binary? We split the binary number into groups of 4 and looked up their corresponding value in the table. With octal, we split the binary values into groups of three. Binary Octal 000 0 001 1 010 2 011 3 100 4 7 101 5 110 6 111 7 There's the table, let's do a conversion. Octal: 561 5 = 101 6 = 110 1 = 001 binary: 101110001 For binary to octal: Binary = 10100111011 010 - 100 - 111 - 011 2 4 7 3 octal: 2473 A very easy conversion, just have the table handy and you'll have no problems. Hexadecimal and Octal When converting hexadecimal to octal and vice versa we convert the number to a binary number first, and then convert that binary number. Octal to Hex Octal > binary > Hexadecimal Octal: 433 4 = 100 3 = 011 3 = 011 binary: 100011011 0001 - 0001 - 1011 1 1 B Hex: 11B Hex to Octal Hexadecimal > binary > Octal hex: A6F A = 1010 6 = 0110 F = 1111 101 - 001 - 101 - 111 5 1 5 7 Octal: 5157 8