Due by 11:59 pm, Thursday, February 2, 2023

If androids someday DO dream of electric sheep
Figure 1. Can’t Sleep (https://xkcd.com/571/). If androids someday DO dream of electric sheep, don’t forget to declare sheepCount as a long int.

This lab will be done with a partner. You can find your lab partner by logging into CS 31’s Spring 23 GitHub organization. Look for a Lab2-userID1-userID2 repository with your username in it.

Your lab partner for Lab 2 is listed here: Lab 2 partners

Our guidelines for working with partners: working with partners, etiquette and expectations.

1. Lab Goals

  • Explore the binary data representation of different types: int, unsigned, float, and char.

  • Convert between decimal, binary, and hexadecimal representations.

  • Apply arithmetic operations to binary numbers.

  • Manipulate binary data with C bit-wise operators.

  • Practice with C programming and the gdb debugger.

2. Lab Description

This lab assignment includes a written problem set (Part 1) and a programming exercise (Part 2).

  1. You will do Part 1 (the written part) on paper, or you can complete it on a tablet and email me a PDF (rwicent1). You will turn the written portion in on or before the start of your lab section on Friday, February 3.

  2. You will do Part 2 on the CS lab machines, editing code and submitting it via git. Part 2 is due Thursday, February 2.

3. Getting Lab 2 Starting Point Code

Like the previous lab, we’ll use git to distribute the starting code for the lab. As a reminder, for each lab assignment you will do the following:

  1. Get the ssh-URL to your Lab git repository from the GitHub server for our class: CS31 GitHub organization.

  2. Using your URL, and from your cs31/labs/ subdirectory, run git clone to clone a local copy of your repo:

    $ cd ~/cs31/labs/
    $ git clone 
  3. cd into your new lab repo directory and work on the lab assignment.

More details are available in the setup for CS31 lab assignment instructions.

4. Part 1. Problem Set

This part of the lab is a written assignment available as a PDF: lab_binary.pdf.

Although this is a partnered lab, we strongly encourage you to work on your own on the written portion of the lab and then confer with your partner about the answers to go over them together. The final exam will include questions that look a lot like these questions, so knowing how to do them on your own is important.

If you save the PDF to a file on your CS account, you can print it using lpr. By default, lpr will print to the printer in the lab where you are working. Be sure to pick up your printouts.

$ lpr lab_binary.pdf

You will turn in this portion of the lab to your lab instructor at the start of lab next week. You may also complete the written portion on a tablet and email me (rwicent1) a PDF of your completed work before the start of lab next week.

You and your partner will turn in a joint copy of the written portion of the lab. (Do not each turn this is in separately.)

5. Part 2. C Programming

For this part, you will write a single C program that when run, 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 that support your answer to the question. For example, the beginning of a run of your program might look like this:

$ ./lab2
Question 1: our 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: our answer to question 2 is ...
    This can be verified by ...

Each answer should include printing out the result(s) of computation(s) that demonstrates your answer’s correctness. DO NOT just print something like this:

printf("The answer to question 1, what 0x2 + 0x6, is 0x8\n");

Instead, have C code that computes the answer to show or to prove that your answer is correct:

unsigned x, y, z;
x = 0x2; y = 0x6;
z = x + y;
printf("The answer to question 1, what %x + %x is %x\n", x, y, z);

For some questions, the code proving your answer correct may be as simple as the example above. For others, however, you will have to think about how to construct some arithmetic expressions that demonstrate the correctness of your answer.

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, show how you can use 3 different format strings in printf to display that display the value of val in different ways.

    Hint: If you aren’t sure how to proceed, type man 3 printf in the terminal on a CS machine. Scroll down to the section titled "Conversion specifiers" and read through the different format strings available to you. There should be 4 that work without compiler errors but you only need to find 3.

    int val;
    val = 97;
  2. What is the maximum value that can be stored in a C unsigned int variable (unsigned)?

  3. What is the maximum positive value that can be stored in a C int variable (signed)?

  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?

6. Requirements

Part 1

  • The answers to Part 1 should be entered in the part 1 PDF and is due at/before the start of your next lab session. (You may also email me a PDF of your completed work to rwicent1 before the start of your lab session.)

Part 2

  • The answers to Part 2 should be implemented in the datarep.c program, and when compiled and run, should output answers to each part.

  • The answer to each question should be implemented as a separate function called by main. For example, main might look like:

    int main() {
      question1();  // call the question1 function
      question2();  // etc
      ...
    }

    You are welcome to add additional helper functions.

  • Each answer should contain enough examples to support it. Do not, for example, enumerate every possible int value. For most questions, it should be enough to have 3 to 5 examples to support your answer. Do not have more than 10 for any one question. For questions 2 and 3, you don’t really need more than one example if your example clearly demonstrates that your claim is true.

  • Examples in support of your answer must be computed by the C program. For example, do not just print out the string "3 + 6 = 9" instead write C code that computes the expression and prints out its result, like this:

    int x, y;
    x = 3;
    y = 6;
    printf ("%d + %d = %d\n", x, y, (x+y));
  • Your C program, when run, should print out the answer to each question in order, with supporting examples, in an easy to read format. Use formatted printf statements, and do not print out lines that are longer than 80 characters (break long output up into multiple lines). Look at printf examples and documentation to use nice formatting for any tabular output your program might produce.

  • Your code should be commented, modular, robust, and use meaningful variable and function names. This includes having a top-level comment describing your program and listing your name. In addition, every function should include a brief description of its behavior.

7. Tips

  • Remember that type is important in C, and that if you give different formatting codes to printf, you can print out the same value as different types (for example, if you print out a value using %u you may see a very different value that if you print it out using %d). If you are not seeing the values that you expect to see, check your printf format string and use gdb to examine your running program.

8. Submitting your Lab

Please remove any debugging output prior to submitting.

To submit your code, commit your changes locally using git add and git commit. Then run git push while in your lab directory.

Also, it is good practice to run make clean before doing a git add and commit; you do not want to add to the repo any files that are built by gcc (e.g. executable files). Included in your lab git repo is a .gitignore file telling git to ignore these files, so you likely won’t add these types of files by accident. However, if you have other gcc compiled binary files in your repo, please be careful about this.

Here are the commands to submit your solution (from your ~/cs31/labs/Lab2-userID1-userID2 directory):

$ make clean
$ git commit -am "correct and well commented Lab2 solution"
$ git push

Verify that the results appear (e.g., by viewing the the repository on CS31-S23). You will receive deductions for submitting code that does not run or repos with merge conflicts. Also note that the time stamp of your final submission is used to verify you submitted by the due date or by the number of late days that you used on this lab, so please do not update your repo after you submit your final version for grading.

If you have difficulty pushing your changes, see the "Troubleshooting" section and "can’t push" sections at the end of the Using Git for CS31 Labs page. And for more information and help with using git, see the git help page.

9. Handy Resources