CS 223 - Programming Assignment 2

This assignment makes use of the material from Chapter 2 about bit-level, logical and shift operations in C (Sections 2.1.8-2.1.10) and the material on integer representations from Section 2.2. This assignment makes use of files contained in this zip file.

There are two parts to this assignment, a written part and a programming part. The programming part asks you to implement seven functions using C's low level bit manipulation operators. The written part asks you to explain how a certain function works.

In the assignment folder there is a file called bits.c. This file contains seven functions that need to be implemented. The file contains a description of several rules that you must follow while implementing these seven functions. The rules force you to use only low level bit manipulation operators and certain logical operators. Each of the seven functions has a description of what the function should compute along with a couple of examples, and a list of which C operators you can use to implement that function. You place your implementation in the body of each function (replacing a dummy "return" statement that is presently contained in each function body).

After you have implemented a function, you can test your implementation. Open a command prompt at the folder hw2 and type the command

        hw2> c:\mingw\bin\mingw32-make.exe
. There is a file in this directory called makefile, and this file tells the make program how to "build" an executable file called student-tests.exe. Run the program student-tests.exe and it will thoroughly test your implementation of each function. You are done with this part of the assignment when this test program no longer reports any errors (assuming that you followed the rules on how to implement your functions). Your can get a bit of help about the test program by entering the command
        hw2> student-tests.exe  -h
In particular, the following command will test just one of your functions (this is useful if you are getting too many errors).
        hw2> student-tests.exe  -f 

For the written part of this assignment, you are to write up a detailed explanation of how the following implementation of the function logicalShift() works.

    /*
     * logicalShift - shift x to the right by n, using a logical shift
     *   Can assume that 1 <= n <= 31
     *   Examples: logicalShift(0x87654321,4) = 0x08765432
     *   Legal ops: ~ & ^ | + << >>
     *   Max ops: 16
     *   Rating: 3
     */
    int logicalShift(int x, int n)
    {
      return (x >> n) & ((1 << (32 + (~n+1))) + ~0);
    }
Your explanation should not be of the form "The function logicalShift first right shifts x by the amount n and then it ANDs that result with the left shift of 1 by the amount 32 plus ..." In other words, you are not supposed to just translate the above line of code into words, you are supposed it explain how and why it works. For example, what is the point of the ~0? What does the ~n+1 do for you? What role does the left shifted 1 play? Why isn't the right shift x >> n by itself good enough? A good explanation of this function should be a paragraph of several sentences.

Turn in a zip file containing your rewritten bits.c file and a text file containing your explanation of how the logicalShift() function works, along with the original contents of hw2.zip. This assignment is due Tuesday, September 30.


Return to the main homework page.
Return to the CS 223 home page.


compliments and criticisms