This assignment makes use of the material from Chapter 2 about C's bit-level boolean and shift operators (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. This assignment is due Thursday, October 22.
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 hw3
and type this command.
hw3> make
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). You can get a bit of help about the test program by entering the command
hw3> ./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).
hw3> ./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: ~ & ^ | + << >> */ 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 to 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 shift 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. Put your explanation in a file called explanation.txt
(be sure to put your name in that file)
Turn in a zip file called CS223Hw3Surname.zip
(where Surname
is your last name) containing your version of bits.c
and your explanation.txt
.
This assignment is due Thursday, October 22.