There are several number systems that are important to computer science. Primary among them is the binary (base 2) number system. Other systems used are the octal (base 8) system and the hexadecimal or hex (base 16) system. Below is a table of values that you need to memorize.
Seeing a number written as 10 does not tell you what its value is. Its value may be ten or two or eight or sixteen depending on its base. You must be told what base you are working in!
Every number system as a set of allowable digits. The decimal system uses the digits 0-9; the binary system uses the digits 0 and 1; the octal system uses the digits 0-7; the hex system uses 0-9 and A-F.
Converting from One System to Another
Other Bases into DecimalWe use a positional notation to write our numbers. That is, the position of each digit gives its place value. Let's start with base 10 numbers.
45297 = 4 * 104 = 4 * 10,000 = 40,000 5 * 103 = 5 * 1,000 = 5,000 2 * 102 = 2 * 100 = 200 9 * 101 = 9 * 10 = 90 7 * 100 = 7 * 1 = 7 45297The same pattern and process is used for converting the other bases into the decimal system.
You need to be able to convert a number from binary, octal, or hex to decimal. Luckily we don't do this very often.
Binary to OctalYou need to know how to convert from binary to either octal or hex. This is easy, especially compared to converting to decimal. Since you have the above table memorized, all you have to do is divide the binary number into the correct number of digits starting from the units position and substitute the correct value.
Octal uses three bits so we divide up the binary number into groups of three. Remember to start from the right!
11011101 = 11 011 101 3 3 5
Binary to HexHexadecimal uses four bits so we divide up the binary number into groups of four. Remember to start from the right!
11011101 = 1101 1101 D D
Decimal to Binary Or Hex
Converting a decimal value into binary (or hex) is a repeated process of dividing, using the remainder, and dividing again. We are going to use the division you did in third grade for this algorithm. This is also called integer division.For example, let's convert 22110 into binary.
110 r 1 2)221The remainder becomes the rightmost digit in our answer; the quotient is used for the next dividend. Because we are converting to binary, 2 is our divisor.
55 r 0 2)110Now 0 is pre-pended to the 1 we got before. And we repeat our divisions until the quotient is 0.
I find it easier to start at the bottom and stack up my divisions, keeping track of my remainders. Read this example as a series of divisions from the bottom up, but read the conversion as the list of remainders from the top down.
0 r 1 2)1 r 1 2)3 r 0 2)6 r 1 2)13 r 1 2)27 r 1 2)55 r 0 2)110 r 1 2)221The answer is read from the top to the bottom using the remainders: 11011101.
The subtraction method for converting decimal to binary will be illustrated in class.
To convert from decimal to hex, divide by 16 and remember to convert the remainder into hex digits (0-F). For example, let's convert 264494210 into hex.
0 r 2 16)2 r 8 16)40 r 5 16)645 r 11 (B) 16)10331 r 12 (C) 16)165308 r 14 (E) 16)2644942The answer is read from the top to the bottom: 285BCE
This division is more difficult to do by long hand, but it is possible. Or you can use your calculator.
If your calculator will do modulo division, then you can get through the
calculations easier. Modulo (or mod) gives the remainder from integer division.
Using the same problem we would get: