Lecture Notes 04 (24 January 2002)

Representing Numbers, Arithmetic


Overall Reading
Brookshear: pp. 24-25, 38-42 and Ch. 1.5
Decker/Hirshfield: pp. 203-206 and middle of p. 248

Outline:

  • Representing Numbers
  • Integers (pp. 38-42 [Br]; pp. 203-206 [DH])
  • Decimal Notation
  • Other Bases
  • Binary Notation
  • Converting between bases
  • Practice at Home
  • Fractions (p. 46 [Br])
  • Arithmetic in other bases
  • Addition (Ch. 1.5 [Br])
  • Multiplication (see middle of p. 248 [DH])
  • Practice at home with a Calculator

  • Representing Numbers

  • Integers (pp. 38-42 [Br]; pp. 203-206 [DH])

  • Decimal Notation
    As Humans, we commonly use decimal notation (i.e. "base 10").

    As an example, how many |'s are given here?
    | | | | | | | | | | | | | |

    Answer: 14 (i.e., 10 + 4)

    We represent an integer value by a series of digits, where each digit is from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.

    Rightmost digit is the ones digit (i.e. 100)
    Second from right is the tens digit (i.e. 101)
    Third from right is the hundreds digit (i.e. 102)
    Fourth from right is the thousands digit (i.e. 103)

    Example: 4089 = 4*103 + 8*101 + 9*100.

    Fact: You can represent 10k distinct numbers using k decimal digits.
    Fact: Every postive integer has a unique representation in this system.

    Why do you think humans chose to use base 10 as our number system?
    (Hint: think about the term 'digit')

  • Other Bases
    There really is very little special about the choice of 10 as the base for a number system. A similar system can be designed using any positive integer, b, as a base.

    We will need b distinct alphabet symbols to represent the values from {0, ..., b-1}.

    Rightmost digit counts b0 (i.e. one's)
    Second from right is the b1 digit
    Third from right is the b2 digit
    Fourth from right is the b3 digit.

    Fact: Every postive integer has a unique representation in this system.

  • Converting form alternate bases to decimal

  • Convert 243(base 6) to decimal
  • Convert 2012(base 3) to decimal
  • Convert 413(base 5) to decimal
  • Hexadecimal (base 16)
    The system certainly works for bases greater than 10 as well. The only issue is we need new alphabet symbols that go beyond the character '9'.

    For base 16, must have symbols to represent the values from {0, ..., 15}.
    For hexadecimal, it is conventional to use characters
    {A, B, C, D, E, F} in place of {10, 11, 12, 13, 14, 15).
    (see pp. 24-25 of [Br])

  • Unary System
    The number system can even been used with the value 1 as a base.

    In this case,
    Rightmost digit represents 10 = 1's.
    Second from right represents 11 = 1's.
    Third from right represents 12 = 1's.
    Fourth from right represents 13 = 1's.

    In this unary number system, there is only one symbol used for each digit, and the number is represented simply by the number of digits used (essentially, like making scratchmarks on the wall...)

    12(base 10) = | | | | | | | | | | | | (base 1)

  • Converting from decimal to another base


    Given a value in decimal, and a desired base b,

    Step 1. Divide the value by b and record the remainder.

    Step 2. As long as the quotient obtained is not zero, continue to divide the newest quotient by b and record the remainder.

    Step 3. Now that a quotient of zero has been obtained, the (base b) representation of the original value consists of the remainders listed from right to left in the order they were recorded.

    Let's try some examples...

  • Convert 99(base 10) to base 6
  • Convert 59(base 10) to base 3
  • Convert 108(base 10) to base 5
  • The beauty of an algorithm is that if you can follow the steps mindlessly, you will get the desired result.

    Of course, it actually may help your intuition to understand why the above algorithm actually works. So let's examine the method more closely...
    Let's begin be reexamining the decimal system.

  • Binary Notation
    What's all of this have to do with computers?

    Though base 10 might seem particularly convenient for humans, the most natural system for representing integers on a digital computer is the binary system (i.e. "base two").

    From binary to decimal
    Example, value "100101" can be decoded, right to left:
    1*one + 0*two + 1*four + 0*eight + 0*sixteen + 1*thirtytwo
    = 1 + 4 + 32 = 37

    From decimal to binary
    Let's convert 101(base 10) to binary.

  • Converting between bases
    So far, all of the conversions we have done have involved going either to decimal or from decimal.
    However, it is quite easy to convert directly from one base to related bases, specifically when one base is an exact power of another.

    As analogy, if you are used to working in base ten, it is easy to adapt to "base thousand". After all,
    $19,345,286 equals 286*1 + 345*1000 + 19*10002

    In Computer Science, powers of two arise as convenient bases at times:

  • Base 8 ("octal"): For instance, a value of
    10110011100 (base 2) equals 2634(base 8)
    Every three bits in binary defines a digit in octal.

  • Base 16 ("hexadecimal"):

    Every four bits in binary defines a digit in hexadecimal.

    For instance a value of
    10110011100 (base 2) equals 59C(base 16)

  • On the other hand, humans have a great deal of trouble converting directly between two unrelated bases (e.g. converting from base 6 to base 7). The reason is that the conversion algorithm above involves some primitive arithmetic operations, and as humans we are mostly comfortable with these primitives when working in decimal notation. Therefore, in such a case we probably find it easiest to convert a number from base 6 to base 10 and then from base 10 to base 7.


  • Fractions (p. 46 [Br])
    Much as we use a decimal point in base 10 to represent a fractional part of a number, we can do the same in other bases. In base 10, the first digit after the decimal point represents 1/10's. The next digit is the 1/100's, and so on.

    If doing this in binary, the bits after a decimal point represent 1/2's, then 1/4's, then 1/8's and so on.

    However, fractions are generally represented in modern computers using what is termed floating-point notation, which is similar to scientific notation. This provides a better range of both very large and very small numbers. We will not discuss these in this course, but those interested may read Ch. 1.7 of [Br].


  • Arithmetic in other bases

  • Addition (Ch. 1.5 [Br])

    We talked about the algorithms we learned in grade school for doing basic arithmetic in decimal notation.

    Those same algorithms can be applied in any of these bases!

    [lets practice]

      357257
    +  62545
    --------
    
    How do we do this if the numbers are represented in base 10?
    How do we do this if the numbers are represented in base 8?


  • Multiplication (see middle of p. 248 [DH])

    If you have time, you can try to do multiplication in another base.


  • Practice at home

    If you want to give yourself a way to practice and then check your answers, many calculators will do some conversions for you. Please keep in mind that the point of homework problems is to get you use to doing this conversion by hand, so please use the calculators only when practicing. (use of calculators will not be allowed during the exams!)

    In particular, the standard calculator application that comes as part of Windows will convert numbers between decimal, binary, octal or hexadecimal. This calculator can usually be started by looking in the Start -> Programs -> Accessories menu. When it begins, it generally looks like a very simple calculator. However, there is a menu title "View" at the top of the calculator that lets you select between "Standard" and "Scientific" views. Select the Scientific view.

    In scientific mode, you will see a box that allows you to pick between one of four number systems: Hex(adecimal), Dec(imal), Oct(al), or Bin(ary). If you want it to convert from decimal to binary, select decimal mode, then type in a decimal number, then click on binary mode. In the same way you can convert from binary to octal, and so on. You can also perform all of the arithmetic operations while working in any of the number systems. Please note, however, that this calculator does not handle fractional values in any base other than decimal.

    One warning: When working outside of decimal notation, the calculator can only handle values which fit in 32 bits or less (limit is slightly less than 4.3 billion). Also, I strongly recommend that you keep the selection on the right as "Dword" when working in one of the other bases.


    comp150 Class Page
    mhg@cs.luc.edu
    Last modified: 29 January 2002