Prerequisite Material from 252 (Starts Here)

Number Systems

Decimal

Here's the decimal number system as an example:
     digits (or symbols) allowed: 0-9
     base (or radix):  10
     the order of the digits is significant

     345 is really
	  3 x 100   + 4 x 10    + 5 x 1
	  3 x 10^2  + 4 x 10^1  + 5 x 10^0
     3 is the most significant symbol (it carries the most weight)
        5 is the least significant symbol (it carries the least weight)

Binary

Here's a binary number system:
     digits (symbols) allowed: 0, 1
     base (radix):  2

     each binary digit is called a BIT

     the order of the digits is significant

     numbering of the digits
	    msb           lsb
	    n-1            0
	where n is the number of digits in the number

	  msb stands for most significant bit
	  lsb stands for least significant bit


      1001 (base 2) is really
	   1 x 2^3  + 0 x 2^2  + 0 x 2^1  + 1 x 2^0
	   9 (base 10)

      11000 (base 2) is really
	   1 x 2^4  +  1 x 2^3  + 0 x 2^2  + 0 x 2^1  + 0 x 2^0
	   24 (base 10)

  Of interest:  most assembly languages have no way to represent
   binary values!

Octal

Here's an octal number system:
     digits (symbols) allowed: 0-7
     base (radix):  8

     the order of the digits is significant

     345  (base 8) is really
	  3 x 8^2  + 4 x 8^1  + 5 x 8^0
	    192    +    32    +   5
	    229 (base 10)

      1001 (base 8) is really
	   1 x 8^3  + 0 x 8^2  + 0 x 8^1  + 1 x 8^0
	      512   +    0     +    0     +    1
	   513 (base 10)


Hexadecimal

here's a hexadecimal number system:
     digits (symbols) allowed: 0-9, a-f
     base (radix):  16

     the order of the digits is significant

     hex   decimal   binary
     0       0        0000
     1       1        0001
	  .
	  .
	  .
     9       9        1001
     a       10       1010
     b       11       1011
     c       12       1100
     d       13       1101
     e       14       1110
     f       15       1111

      a3 (base 16) is really
	   a x 16^1  + 3 x 16^0
	    160      +   3
	      163 (base 10)


A common syntax used to represent hexadecimal values (in code)
is to place the symbols "0x" as a prefix to the value.
MIPS assembly languge does this.
  Example:   0x8d    is the hexadecimal value 8d (10001101 binary)

A second common syntax is to place a suffix of 'h' onto a value,
indicating that it is hexadecimal.
  Example:   8dh  (same example as just given)
  Note that h is not a symbol used in hexadecimal, so it can indicate
  the representation used.  The Intel architectures do this in their
  assembly languages.  This representation is actually more time
  consuming (meaning the execution time of the code) to interpret,
  since the entire number must be read before it can be decided
  what number system is being used.

In General



Given all these examples, here's a set of formulas for the
  general case.

  Given an n-digit number (in weighted positional notation):

       S     S    .  .  .   S    S    S
        n-1   n-2            2    1    0
  
  the subscript gives us a numbering of the digits


  given a base b, this is the decimal value


	  the summation (from i=0 to i=n-1) of S  *  b^i
						i


Transformations Between Bases


any base --> decimal
    just use the definition (summation) given above.

    134 (base 5)
    1 x 5^2  +  3 x 5^1  +  4 x 5^0
      25     +    15     +    4
	         44 (base 10)



decimal --> another base
    one algorithm that works:
    Divide decimal value by the base until the quotient is 0.
    The remainders give the digits of the value.

    Note:  this algorithm works for decimal to ANY base.  Just
           change the base.

    examples:
	36 (base 10)    to base 2 (binary)
	36/2 = 18  r=0  <-- lsb
	18/2 =  9  r=0
	 9/2 =  4  r=1
	 4/2 =  2  r=0
	 2/2 =  1  r=0
	 1/2 =  0  r=1  <-- msb

	 36 (base 10) == 100100 (base 2)


	14 (base 10)    to base 2 (binary)
	14/2 =  7  r=0  <-- lsb
	 7/2 =  3  r=1
	 3/2 =  1  r=1
	 1/2 =  0  r=1  <-- msb

	 14 (base 10) == 1110 (base 2)


	38 (base 10)    to base 3
	38/3 = 12  r=2  <-- ls digit
	12/3 =  4  r=0
	 4/3 =  1  r=1
	 1/3 =  0  r=1  <-- ms digit

	 38 (base 10) == 1102 (base 3)


        100 (base 10)   to base 5
        100/5 = 20  r=0
         20/5 =  4  r=0
          4/5 =  0  r=4
       
          100 (base 10) = 400 (base 5)


binary --> octal
    1. group into 3's starting at least significant symbol
       (if the number of bits is not evenly divisible by 3, then
	add 0's at the most significant end)
    2. write 1 octal digit for each group

    example:
	
	100 010 111  (binary)
	 4   2   7   (octal)
	
	 10 101 110  (binary)
	 2   5   6   (octal)

binary --> hex
  (just like binary to octal!)
    1. group into 4's starting at least significant symbol
       (if the number of bits is not evenly divisible by 4, then
	add 0's at the most significant end)
    2. write 1 hex digit for each group


    example:
     
       1001 1110 0111 0000
	9    e    7    0

	  1 1111 1010 0011
	1    f    a    3


hex --> binary
     (trivial!)  just write down the 4 bit binary code for
     each hexadecimal digit

     example:

      3    9    c    8
    0011 1001 1100 1000


octal --> binary
     (just like hex to binary!)
     (trivial!)  just write down the 8 bit binary code for
     each octal digit

     example:

      5   0   1
     101 000 001


hex --> octal
     do it in 2 steps,       1. hex --> binary 
                             2. binary --> octal


Where the bases are powers of a common value, this transformation
is easy (like binary, base 4, octal, hexadecimal)

Examples:

  base 3 to base 9

     2100122 (base 3)

     One base 9 digit is substituted for each 2 base 3 digits.
     Why 2?  Answer: 3^2=9

     base
      3   9
     -------
     00   0
     01   1
     02   2
     10   3
     11   4
     12   5
     20   6
     21   7
     22   8


     2 10 01 22 (base 3)

     2  3  1  8 (base 9)   


On Representing Nonintegers


what range of values is needed for calculations
     very large:     Avogadro's number 6.022 x 10 ^ 23 atoms/mole
		     mass of the earth 5.98 x 10 ^ 24 kilograms
		     speed of light    3.0 x 10 ^ 8 meters/sec
     very small:     charge on an electron -1.60 x 10 ^ (-19)



scientific notation
    a way of representing rational numbers using integers
    (used commonly to represent nonintegers in computers)

                                     exponent
	    number =  mantissa x base
	

	mantissa == fraction == significand
	base == radix
	point is really called a radix point, for a number with
	 a decimal base, it is called a decimal point.

	all the constants given above are in scientific notation



normalization
  to keep a unique form for every representable noninteger, they
  are kept in NORMALIZED form.  A normalized number will follow the
  following rule:

	   1 <=  mantissa  <  base
  
  In this form, the radix point is always placed one place to
  the right of the first significant (non-zero) symbol (as above).

On Precision, Accuracy, and Significant Digits


   These terms are often used incorrectly or ignored.  They are
   important!

   A measurement (in a scientific experiment) implies a certain
   amount of error, depending on equipment used.  Significant
   digits tell about that error.
   For example, a number given as
    3.6  really implies that this number is in the range of
	 3.6 +- .05,   which is 3.55 to 3.65
	 This is 2 significant digits.
    3.60 really implies that this number is in the range of
	 3.6 +- .005,   which is 3.595 to 3.605
	 This is 3 significant digits.
    
   So, the number of significant digits given in a number tells
   about how accurately the number is known. The larger the number
   of significant digits, the better the accuracy.

   Computers (or calculators, a more familiar machine) have a fixed
   precision.  No matter what accuracy of a number is known, they
   give lots of digits in a number.  They ignore how many significant
   digits are involved.
   For example, if you do the operation 1.2  x  2.2. given that
      each number has 2 significant digits, a correct answer is
		  
		  1.2
		x 2.2
		-----
		   24
		+ 24
		-----
		  264 --> 2.64  -->   2.6   or 2.6 +- .05

      a calculator will most likely give an answer of 2.640000000,
      which implies an accuracy much higher than possible.  The
      result given is just the highest precision that the calculator
      has.  It has no knowledge of accuracy -- only precision.

Binary Fractions


       f     f    .  .  .   f    f    f  .  f    f    f  . . .
        n-1   n-2            2    1    0     -1   -2   -3
                                         |  
                                         |  
					  binary point

       The decimal value is calculated in the same way as for
       non-fractional numbers,  the exponents are now negative.




  example:
          101.001 (binary)
          1 x 2^2 + 1 x 2^0 + 1 x 2^-3
             4     +    1     +  1/8
                   5  1/8  = 5.125 (decimal)

  2^-1 = .5
  2^-2 = .25
  2^-3 = .125
  2^-4 = .0625   etc.

converting decimal to binary fractions

   Consider left and right of the decimal point separately.
   The stuff to the left can be converted to binary as before.
   Use the following algorithm to convert the fraction:

   fraction  fraction x 2  digit left of point
     .8          1.6              1 <-- most significant (f  )
     .6          1.2              1                        -1
     .2          0.4              0
     .4          0.8              0
     .8  (it must repeat from here!)

            ----
    .8 is  .1100

Non-Binary Fractions


same as with binary, only the base changes!

       f     f    .  .  .   f    f    f  .  f    f    f  . . .
        n-1   n-2            2    1    0     -1   -2   -3
                                         |  
                                         |  
					  radix point

       The decimal value is calculated in the same way as for
       non-fractional numbers,  the exponents are now negative.




  examples:
          101.001 (octal) to decimal
          1 x 8^2 + 1 x 8^0 + 1 x 8^-3
             64     +    1     +  1/512
                   65  1/512  = 65.0019 (approx)

          13.a6 (hexadecimal) to decimal
	  1 x 16^1 + 3 x 16^0 + a x 16^-1 + 6 x 16^-2
	     16     +    3      +  10/16     +  6/256
		  19  166/256 = 19.64 (approx)


EXAMPLE:  give 102.3 (base 5) in base 3

  102 (base 5) to decimal:
    1 * 5^2 + 0 * 5^1 + 2 * 5^0
      25    +    0    +   2
      27 (base 10) = 102 (base 5)

   .3 (base 5) to decimal:
      .3 * 5^(-1)
       3/5, or .6 (decimal)
       .6 (base 10) = .3 (base 5)

    So, 102.3 (base 5) is 27.6 (decimal)

    27 (decimal)  to base 3
    27/3 = 9  r=0 <-- ls digit
     9/3 = 3    0
     3/3 = 1    0
     1/3 = 0    1              27 (base 10) = 1000 (base 3)

    .6 x 3 = 1.8    1 (ms fractional digit)
    .8 x 3 = 2.4    2
    .4 x 3 = 1.2    1
    .2 x 3 = 0.6    0
    .6 x 3  this repeats the 4 digits
                               ____
              .6 (base 10) =  .1210
	      A bar over the top of the digits that repeat is a commonly
	      used notation.
                             ____
     102.3 (base 5)  =  1000.1210 (base 3)

Prerequisite Material from 252 (Ends Here)


Copyright © Karen Miller, 2006