CS 367-002
Homework #2 - Manipulating Bits
Due: Thursday, Feb 11, at classtime
Late homework will be penalized 25% per day (including weekend days)
Introduction
The purpose of this assignment is to become more familiar with bit-level representations and manipulations. You'll do this by solving a series of programming "puzzles." Many of these puzzles are quite artificial, but you'll find yourself thinking much more about bits in working your way through them.
Logistics
You may work in a group of up to two people in solving the problems for this assignment. The only "hand-in" will be electronic. Any clarifications and revisions to the assignment will be posted on the course Website.
Handout Instructions
Start by copying datalab-handout.tar to a (protected) directory in which you plan to do your work. This file is posted on the course web site and you can also copy it from the cs367 IT&E Zeus account into your Zeus account using:
cp ~cs367/datalab-handout.tar ./
Once you have the file, type the command:
tar xvf datalab-handout.tar
This will cause a number of files to be unpacked in the directory. The only file you will be modifying and turning in is bits.c. The file btest.c allows you to evaluate the functional correctness of your code. The file README contains additional documentation about btest. Use the command make btest to generate the test code and run it with the command ./btest. The file dlc is a compiler binary that you can use to check your solutions for compliance with the coding rules. The remaining files are used to build btest. Looking at the file bits.c you'll notice a C structure team into which you should insert the requested identifying information about the one or two individuals comprising your programming team. Do this right away so you don't forget. The bits.c file also contains a skeleton for each of the 8 programming puzzles. Your assignment is to complete each function skeleton using only straightline code (i.e., no loops or conditionals) and a limited number of C arithmetic and logical operators. Specifically, you are only allowed to use the following eight operators:
! ˜ & ˆ | + << >>
A few of the functions further restrict this list. Also, you are not allowed to use any constants longer than 8 bits. See the comments in bits.c for detailed rules and a discussion of the desired coding style.
Evaluation
Your code will be compiled with GCC and run and tested on one of the ITE Linux machines. Your score will be computed based on:
- Correctness of code running on one of the class machines.
- Performance of code, based on number of operators used in each function.
- Style, based on your instructor's subjective evaluation of the quality of your solutions and your comments.
The puzzles you must solve have been given a difficulty rating between 1 and 4, such that their weighted sum totals to 10. We will evaluate your functions using the test arguments in btest.c. You will get full credit for a puzzle if it passes all of the tests performed by btest.c, half credit if it fails one test, and no credit otherwise. Regarding performance, our main concern at this point in the course is that you can get the right answer. However, we want to instill in you a sense of keeping things as short and simple as you can. Furthermore, some of the puzzles can be solved by brute force, but we want you to be more clever. Thus, for each function we’ve established a maximum number of operators that you are allowed to use for each function. This limit is very generous and is designed only to catch egregiously inefficient solutions. Finally, we’ve reserved points for a subjective evaluation of the style of your solutions and your commenting. Your solutions should be as clean and straightforward as possible. Your comments should be informative, but they need not be extensive.
You will be solving the following puzzles:
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmin(void) {
return 2;
}
/*
* copyLSB - set all bits of result to least significant bit of x
* Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int copyLSB(int x) {
return 2;
}
/*
* getByte - Extract byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByte(0x12345678,1) = 0x56
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int getByte(int x, int n) {
return 2;
}
/*
* isNotEqual - return 0 if x == y, and 1 otherwise
* Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int isNotEqual(int x, int y) {
return 2;
}
/*
* isNegative - return 1 if x < 0, return 0 otherwise
* Example: isNegative(-1) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 3
*/
int isNegative(int x) {
return 2;
}
Advice
You are welcome to do your code development using any system or compiler you choose. Just make sure that the version you turn in compiles and runs correctly on IT&E Linux machines. If it doesn’t compile, we can’t grade it so you won’t get any points for doing it.
Checking your Program
The dlc program, a modified version of an ANSI C compiler, will be used to check your programs for compliance with the coding style rules. (This program only runs on a Linux x86 system such as the systems in the IT&E Lab.) The typical usage is ./dlc bits.c
Type ./dlc –help for a list of command line options. The README file is also helpful. Some notes on dlc:
The dlc program runs silently unless it detects a problem.
Don’t include in your bits.c file, as it confuses dlc and results in some non-intuitive error messages.
Check the file README for documentation on running the btest program. You’ll find it helpful to work through the functions one at a time, testing each one as you go. You can use the -fflag to instruct btest to test only a single function, e.g., ./btest -f minusOne.
Hand In Instructions
Submit
- A hardcopy of bits.c. This hard copy should include the names of each team member.
- Submit the file bits.c electronically using the online submit program:
˜cs367/367bin/submit 2 bits.c
You must be logged onto Zeus to do this, and your bits.c program must be in the directory on Zeus where you type this command. You can repeat this command as many times as you like with new copies of bits.c. Only the last version you submit will be saved. Complete information on this process will be posted available online.