LCSC > Natural Science and Mathematics > Computer Science > Patterson-McNeill > Foundations > Notes


CS111
Notes
Spring 2009
  1. Number Systems
  2. Orders of Magnitude
  3. Integers
  4. Floating Point
  5. ASCII

Number Systems

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.


DecimalBinary OctalHexadecimal
000000 0
100011 1
200102 2
300113 3
401004 4
501015 5
601106 6
701117 7
8100010 8
9100111 9
10101012 A
11101113 B
12110014 C
13110115 D
14111016 E
15111117 F

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 Decimal

We 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
                        45297
The same pattern and process is used for converting the other bases into the decimal system.
BinaryOctalHex
11011101 = 
1 * 27 = 1 * 128 = 128
1 * 26 = 1 *   64 =  64
0 * 25 = 0 *   32 =   0
1 * 24 = 1 *   16 =  16
1 * 23 = 1 *     8 =   8
1 * 22 = 1 *     4 =   4
0 * 21 = 0 *     2 =   0
1 * 20 = 1 *     1 =   1
                          221
45261 = 
4 * 84 = 4 * 4,096 = 16,384
5 * 83 = 5 *    512 =   2,560
2 * 82 = 2 *      64 =     128
6 * 81 = 6 *        8 =      48
1 * 80 = 1 *        1 =        1
                             19,121

285BCE = 
2 * 165 =   2 * 1,048,576 = 2,097,152
8 * 164 =   8 *     65,536 =    524,288
5 * 163 =   5 *       4,096 =      20,480
B * 162 = 11 *          256 =       2,816
C * 161 = 12 *           16 =          192
E * 160 = 14 *             1 =           14
                                      2,644,942

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 Octal

You 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 Hex

Hexadecimal 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)221
The 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)110
Now 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)221 
The 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)2644942
The 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:
2644942 mod 16 = 14 or E.
You must compute the integer quotient portion so you can do the next part: 2644942 / 16 = 165308.875. Throw away the fraction part.
Now 165308 mod 16 = 12 or C.
165308 / 16 = 10331.75. Then 10331 mod 16 = 11 or B. And so on.
10331 / 16 = 645.6875; 645 mod 16 = 5.
645 / 16 = 40.3125; 40 mod 16 = 8.
40 / 16 = 2.5; 2 mod 16 = 2. 2 / 16 = 0. So we quit.


Syllabus Notes
Revised - 8 January 2009