Lab 03: Data Storage and Its Implications

This week's lab session will primarily be on Session 3 in the Lab Book, Data Storage and Its Implications. In case you do not have it, you can click on this link to look at this chapter in Adobe Acrobat format, which can be read on the Web. This lab covers several topics relative to every-day programming, and it also anticipates some issues you will see again in the course Introduction to Microprocessors.

Before or after doing this lab, you may want to look in your COT3002 textbook (e.g. Astrachan) and review these concepts:


Exercises: Session 3 in the Lab Book

Do these exercises in the lab and turn them in. Remember to type your answers into a file and e-mail the file to your TA (not to the faculty coordinator). Do all steps in each Experiment unless noted.

Remember, when the lab-book mentions a program, say, CP03E03, don't type it in! Copy it to your current directory:
cp ~cot3002l/src/CP03E03.cpp .
This means the C++ files are in the subdirectory named 'src' in the directory of the user-name 'cot3002l' (note that's an 'ell' at the end not a one!) Don't forget the final '.' after the name of the C++ file. (This means the target of the copy command is the current directory, and just use the same name as in the source directory.)

It is a good practice to get the source code through UNIX command. However, you can also save this link to get CP03E03.cpp.

Remember the command to compile your program is:
g++ -Wall prog_name.cpp
Always use the -Wall option with g++ to show all warnings, some of which might be errors. You could also use the -o option to rename the output file to be something other than a.out


Hint for Experiment 3.1, Step 4

If you don't know what two's complement notation means, don't worry, you'll see it in a later course. But, here's an example that shows how integers would be represented if we only had 3 bits to store a single int value. The following table of bit patterns and their corresponding integer values in two's complement.

    Bit       Int
  Pattern    Value
  -------    -----
    011        3
    010        2
    001        1
    000        0
    111       -1
    110       -2
    101       -3
    100       -4


Hint for Experiment 3.1, Step 5-6

Can you be clever about guessing a value for N? You can, if you study the table of values and bit patterns for two's complement (shown in the previous hint, just above this on the Web page). Note in the table that the largest value for what looks like a 3-bit integer is 3, or 2^2-1. So N would be two here, or one less than the number of total bits available for an integer. You might know or have heard that common word sizes for computers are 16 bits, 32 bits, or even 64 bits.

If you want a calculator to follow up on these hints, you have one under X Windows; just type: xcalc &
from a X Terminal window. (When you're done, type q in the calculator window to quit.)


Hint for Experiment 3.2

In Step 1, you're starting out close to the largest possible int value and then keep adding one until you go beyond it.

In Step 3, you must alter the code so you start out just "above" the smallest possible int value, and then subtract one until you go beyond it.


Hint for Experiment 3.3

The answer you get for Step 2 probably surprises you. Unless you know more about floating point notation than we teach in this course, you probably won't be able to explain the answer. If you can, great. If not, read on.

You'll see in CDA3331 that floating point numbers are stored with a limited number of bits for a value's precision and some bits for the exponent. Let's pretend numbers are stored in decimal, and that you only have 3 digits for the precision and 1 digit for the exponent. The largest number you could store would be 999 x 10^9 (often written in scientific form as 999E9). Think about what would happen if you subtracted 10 from this number? Write it out as 999,000,000,000 before doing the math if that helps you. Remember since you can only store 3 digits for the precision, you can only store numbers like 998E9, 997E9, etc.

Now you know enough to be able explain this, if you understood the last paragraph.


Last modified 12/30/2006.