CS61C Summer 2014 Lab 2 - C Intro, Number Representation



Copy the contents of ~cs61c/labs/su14/02 to your home directory. E.g.

    $ cp -r ~cs61c/labs/su14/02 ~/lab02


Exercise 1: Practice with Numbers

This exercise can be done WITHOUT a lab account!

Part 1: Cisco Binary Game -- The object is to match the number on the right with the unsigned binary representation on the left. This requires a combination of clicking and typing. And then there's time pressure to make things more fun! The game gets progressively faster and more difficult are you progress. See if you can score over 20,000 points and get to Level 5!

Part 2: IEC Prefixes -- Practice converting between powers of 2 and IEC prefixes (in both directions!) until you can do so consistently and relatively quickly. This will help you a lot later on in the course!


Exercise 2: Simple C program

The following C program, also in output0.c, is supposed to output two zeros but currently doesn't work! Make changes to the two lines specified to produce the desired behavior. Don't change anything else in the program. The following references may help: ASCII Table and printf.

#include <stdio.h>

int main(void) {
    int n;
    n = 0; /* fix 1: only change this line */
    printf("fix 1: %c\n",n);

    n = 0;
    printf("fix 2: %c\n",n); /* fix 2: only change this line */

    return 0;

To verify your solution, compile it and run the resulting executable:

$ gcc -o output0 output0.c
$ ./output0
fix 1: 0
fix 2: 0

Note: In general the -o "NAME" tag specifies the name of your executable. Without it (i.e. gcc output0.c), the executable defaults to a.out.


Exercise 3: C control flow

Berkeley is well-known for its academics as well as its eccentrics! Look at the code contained in eccentric.c. In it are four different examples of basic C control flow. Compile and run the program to see what it does. Modifying ONLY the initialization values of variables, make the program produce the following output:

$ gcc -o eccentric eccentric.c
$ ./eccentric
Berkeley eccentrics:
Happy Happy Happy



Exercise 4: The Biggest Integer

In class we discussed number representation. In particular, we discussed unsigned integers and two's complement, the almost ubiquitous format for signed integers. Look at biggestInt.c. You may wish to read through the comments but at this point it is not critical that you understand exactly how the program works. Basically, it is a C program that will tell you some useful information about certain C data types. It does this by exploiting the fact that C does not check for overflow and wrap around conditions. Compile and run the program and use the provided information to answer the following questions:

  1. Based on the value of the most significant bit (MSB) of an unsigned int, how many bits does the C data type unsigned int have on your current machine?
  2. Based on the value of the largest positive signed long, how many bits does the C data type long have on your current machine?
  3. Based on the value of the most negative signed int, do the unsigned int and signed int have the same number of bits on your current machine?
  4. Two's complement number representations have one more negative number than they do positive numbers (i.e. the most negative number does not have a positive counter-part). The final piece of information printed is the signed value of what you get when you try to negate the most negative value. Why does this happen?