CS31 Lab 1: Data Representation and Arithmetic Operations

Part 1 (written part):
           due at the beginning of lecture on Thursday, February 1

Part 2 (programming part):
          due before midnight on Thursday, February 1

This lab is to be done alone (no partners). The assignment includes a written part and a programming part. The written part should be turned in at lecture and the programming part should be submitted via GitHub.

Lab 1 Goals:

Contents:


Getting Your Lab 1 Starting Point Code

For CS31 lab assignments, we will use git to distribute starting point files, to save our progress towards a solution, and to submit our solutions. On subsequent labs you will use git to share code with your partner.

  1. If you have not already done so, create a cs31/labs subdirectory into which you will clone the git repo containing your Lab 1 starting point code:
    $ cd
    $ mkdir cs31
    $ cd cs31
    $ mkdir labs
    $ cd labs
    $ pwd           # should be /home/[your-username]/cs31/labs/
    
  2. Clone your Lab1 repo into your labs subdirectory. Follow the directions off the git Help page and grab the ssh-URL for your repo:
    $ git clone [your-ssh-URL]
    
    You should have already completed the one-time set-up steps for Lab 0, but if not do that first: First-time set-up and generating ssh keys
  3. At this point you can cd into your local repo and start writing, compiling, and testing your lab 1 solution code:
    $ cd Lab1-[your-username]
    $ ls
    Makefile README.md lab1.c 
    
    Open README.md and lab1.c in your favorite editor (atom, vim, emacs).
  4. To compile and test your program:
    $ make
    $ ./lab1
    

Part 1. Written Assignment

Answer the questions below showing your work and/or explaining your answer. If a question specifies a number of bits, your answer should be in a corresponding number of bits. For example, if the question asks you to add 4-bit values together your answer should be a 4-bit value not a 64-bit one. Assume that for signed values the high-order bit is the sign bit. For example, 1000 should be interpreted as negative as a 4-bit signed value, but positive as an 8-bit signed value (00001000).

You can check your answers for correctness by writing a C program or by using gdb's print command. See the weekly lab page for details on using gdb.

  1. What is the largest positive value that can be represented with a 2's complement 8 bit number? Explain.
  2. What is the largest positive value that can be represented with an unsigned 8 bit number? Explain.
  3. Convert the unsigned 8 bit binary value 10100110 to decimal. Show your work.
  4. Convert the signed 8 bit binary value 10100110 to decimal. Show your work.
  5. For the following 8 bit binary values (show your work):
    value 1: 01011101
    value 2: 01100101
    
    1. What is the binary representation of the result of adding them together as unsigned 8 bit values? Does this operation result in overflow?
    2. What is the decimal representation of the resulting addition if the two values are unsigned 8 bit values?
    3. What is the decimal representation of the resulting addition if the two values are signed 8 bit values?
    4. What is the binary representation of the result of subtracting the second from the first as unsigned 8 bit values? Does this operation result in overflow?
  6. Convert the following 2 byte binary numbers to hexadecimal, indicating how each part is converted (the binary numbers are shown with spaces between each group of 4 digits to make them easier to read):
    1. 0000 0110 0001 1111
    2. 1100 0101 1110 0101
    3. 1010 0111 1101 0110
  7. Convert the following hexadecimal numbers to binary, indicating how you converted each digit:
    1. 0x23
    2. 0x852
    3. 0xc1a6
    4. 0xefab
  8. Convert the following decimal values to 8 bit binary and to hexadecimal. Show your work:
    1. 12
    2. -36
    3. 123
    4. -123
  9. Show the result of each bit-wise operation below. Give the result in both binary and decimal. When converting to decimal, treat the result as unsigned.
    1. 0110 | ~(1010)
    2. ~(0110 | 1010)
    3. 0111 & ~(1001)
    4. (1010 | 0000) & 1111
    5. 0011 ^ 1110
    6. 0111 << 2
    7. 0111 >> 2

Part 2. C Programming

Write a C program that prints out answers to each of the questions below. For each question, print out a string that is your answer to the question, and then print out some expressions and their results to support your answer. For example, the beginning of a run of your program might look like this:

$ ./lab1
Question 1: my answer to question 1 is ...
    This can be verified by examining the result of the expression ...
    when x is the int value ... and y is ... the expression is ... 
    when x is ... and y is ... the expression is ...

Question 2: my answer to question 2 is ...
    This can be verified by ...
For each question, you should be writing C code that demonstrates your answer is correct. If question 1 asked you to find the sum of 0x2 and 0x6, this would be insufficient:
   printf("Question 1\n");
   printf("0x2 + 0x6 is 0x8.\n");
This would be better:
   unsigned x, y, z;
   x = 0x2; y = 0x6;
   z = x + y;
   printf("Question 1\n");
   printf("%x + %x is %x.\n", x, y, z);

Answer these questions by writing a C program that prints out the answer and prints out example expression(s) that support your answer.

  1. Given the following variable declaration and initialization, how many ways can printf display the value of x? Show as many as you can find.
    int val;
    val = 97;
    
  2. What is the maximum positive value that can be stored in a C int variable?
  3. What is the maximum value that can be stored in a C unsigned int variable?
  4. What arithmetic operation is equivalent to left shifting an unsigned int value by 1?
  5. What arithmetic operation is equivalent to left shifting an unsigned int value by 2?
  6. What arithmetic operation is equivalent to right shifting an unsigned int value by 1?

Hints and Requirements for this part


Submitting your Solution to Part 2

To submit your code, commit your changes locally using git add and git commit. Then run git push to push your local committed version to the remote master:

  git add lab1.c
  git commit -m "lab1 is done"
  git push

You should run these commands not just when you are done but whenever you feel you have made progress towards a solution. Choose a commit message ("lab1 is done" in the example above) that describes what you have accomplished.

You can see if you have any uncommitted changes by running git status. For example, in this run of git status, the "Changes not staged for commit" output tells me that I have made changes to lab1.c since my last add and commit, and I need to add, commit, and push them to the repo:

git status
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)
        modified:   lab1.c
Please don't add, commit or push the executable files produced when you run make. If you run make clean before running git commands, this will help ensure that you do not do this by mistake.

For more git help see: the git Help page