• Part 1 (Problem Set) Due: Thursday, January 31 at the beginning of class.

  • Part 2 (Code) Due: Thursday, January 31, 11:59 PM

If androids someday DO dream of electric sheep, don’t forget to declare sheepCount as a long int.

If androids someday DO dream of electric sheep

1. Handy References

2. Lab1 Goals:

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

  • To convert between decimal, binary and hexadecimal representations.

  • To apply binary arithmetic.

  • Practice with C programming and the gdb debugger.

3. Lab Description

This lab assignment includes a problem set that you will hand in at the beginning of your class and a programming exercise that you will submit electronically by Tuesday evening via GitHub.

Before starting the lab, first clone your Lab1 repository into ~/cs31/labs.

3.1. Part 1. Problem Set

You can either write-up your solutions by hand or write them up in a text editor and print the resulting file to one of our printers. If you type your answers, make sure to not have lines longer than 80 characters (explicitly hit the Enter key to start a new line). If you have lines longer than 80 characters they will either be cut off by the printer or wrapped strangely. I would suggest using an editor window that is 80 characters wide so that you can see when the current line is too long and starts to wrap around.

For these problems you will be graded on showing how you applied the operation or conversion method we described in class: you must show your work or explain how you got the result to receive credit. Check your answers for correctness by either writing a C program to do some of the computation and printing result values or by using gdb’s print command. See the weekly lab page for details on using gdb.

Answer the questions below showing your work and/or explaining your answer. For these questions, if a question specifies a number of bits, your answer should be in a corresponding number of digits. For example, if the question asks to add 4 bit values together your answer should be a 4 bit value, not a 64 bit one. Also, 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).

  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? Does this operation result in overflow? If so, when (i.e., under what circumstances / interpretations of the values)?

    2. What is the decimal representation of the resulting addition if the two values are signed 8 bit values?

    3. What is the decimal representation of the resulting addition if the two values are unsigned 8 bit values?

    4. What is the binary representation of the result of subtracting the second from the first? Does this operation result in overflow? If so, when?

  6. Convert the following 2-byte binary numbers to hexadecimal, indicating how each part is converted (the binary values are shown with spaces between each 4 digits just 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 (2’s complement) binary and to hexadecimal. Show your work:

    1. 12

    2. -36

    3. 123

    4. -123

  9. Given the following 4 bit binary values, show the results of each bit-wise operation, showing both the binary and decimal result value for each (list the unsigned decimal value):

    1. 0110 | ~(1010)

    2. ~(0110 | 1010)

    3. 0111 & ~(1001)

    4. (1010 | 0000) & 1111

    5. 0011 ^ 1110

    6. 0111 << 2

    7. 0111 >> 2

3.2. 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:

$ ./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 ...
Printing Output

Each answer should include printing out the value(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 constructing some arithmetic expressions that will demonstrate the correctness of your answer.

You may want to try printing some values and expressions in gdb in binary, hexadecimal, and decimal to help you figure out good values to test in your C program to ensure you considering all cases.

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

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

  2. What is the maximum value that can be stored in a C unsigned int variable? Note: unsigned means that the variable only stores non-negative values.

  3. What arithmetic operation is equivalent to left shifting an unsigned int value by 1?

  4. What arithmetic operation is equivalent to left shifting an unsigned int value by 2?

  5. What arithmetic operation is equivalent to right shifting an unsigned int value by 1?

4. Requirements

  • Your solutions to part 1 must be legible and formatted such that they can be easily read by a grader.

  • 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();
      ...
    }

    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 1 and 2, 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.

5. 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.

6. Submitting

  • Please remove any debugging output prior to submitting.

  • To submit your code, simply commit your changes locally using git add and git commit. Then run git push while in your lab directory. For more info, see the section on Using a shared repo on the git help page.