University of California at Berkeley
College of Engineering
Department of Electrical Engineering and Computer Science

CS 61C, Spring 2010

HW 1

Due Friday, January 29, 2010 @ 11:59pm

Goals

This assignment checks your understanding of the material in P&H Chapter 1 as well as some number representation basics. It will also give you practice compiling and executing C programs.

Background reading

Submitting Your Solution

Submit your solution by creating a directory named hw1 that contains files named cod.txt and bitcount.c. (Note that capitalization matters in file names; the submission program will not accept your submission if your file names differ at all from those specified.) From within that directory, type "submit hw1". Partners are not allowed on this assignment.

Problem 0

Copy the contents of ~cs61c/hw/01 to a suitable location in your home directory.
  $ mkdir ~/hw
  $ cp -r ~cs61c/hw/01/ ~/hw
All the files referred in this homework will be in the folder you copied.

Problem 1

In cod.txt, answer the following questions:

Problem 2

What are the decimal values of each of the following binary numbers if you interpret them as 2's complement integers? Add your answers in cod.txt.

Problem 3

What are the decimal values of each of the following binary numbers if you interpret them as unsigned integers? Add your answers to cod.txt.

Problem 4

For each of the bit lengths and number representations below, list the binary encodings and values of the possible numbers closest to +infinity and to -infinity. (For each of the three parts to this question, you will provide two binary strings and two corresponding decimal numbers.) Add your answers to cod.txt

Problem 5

Write a function named bitCount() in bitcount.c that returns the number of 1-bits in the binary representation of its unsigned integer argument. Remember to fill in the identification information and run the completed program to verify correctness.
  /*
    Name:
    Lab section time:
  */

  #include <stdio.h>

  int bitCount (unsigned int n);

  int main ( )
  {
    printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n",
      0, bitCount (0));
    printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
      1, bitCount (1));
    printf ("# 1-bits in base 2 representation of %u = %d, should be 16\n",
      2863311530u, bitCount (2863311530u));
    printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
      536870912, bitCount (536870912));
    printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n",
      4294967295u, bitCount (4294967295u));
    return 0;
  }

  int bitCount (unsigned int n)
  {
    /* your code here */
  }

Problem 6

You have decided that you want your bitcount program above to work from the command-line (see K&R Sec. 5.10), as follows:
  # ./bitcount 17
  2
  # ./bitcount 255
  8
  # ./bitcount 10 20
  too many arguments!
  # ./bitcount
  [the same result as from problem 5]
You may assume that the single argument will always be an integer in the range from 0 to 2^31-1. You will find the function atoi helpful.

Extra for experts: Implement this exercise without using the library function atoi (or something comparable).