CS 213, Fall 2002Lab Assignment L1The Data Lab: Manipulating BitsAssigned: Aug. 29, Due: Thu., Sept. 12, 11:59PMDave O’Hallaron ([email protected]) is the lead person for this assignment.IntroductionThe purpose of this assignment is to become more familiar with bit-level representations and manipulations.You’ll do this by solving a series of 14 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.LogisticsThis is an individual project. The only handin will be electronic. Any clarifications and revisions to theassignment will be posted on the course Web page.Hand Out InstructionsAll files you need are in the directory/afs/cs.cmu.edu/academic/class/15213-f02/L1Start by copying datalab-handout.tar to a (protected) directory in which you plan to do your work.Then give the command: tar xvf datalab-handout.tar. This will cause a number of files to beunpacked 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 containsadditional documentation about btest. Use the command make btest to generate the test code and runit with the command ./btest. The file dlc is a compiler binary that you can use to check your solutionsfor compliance with the coding rules. The remaining files are used to build btest.1Looking at the file bits.c you’ll notice a C structure team into which you should insert the requestedidentifying information about yourself. Do this right away so you don’t forget.The bits.c file also contains a skeleton for each of the 14 programming puzzles. Your assignment is tocomplete each function skeleton using only straightline code (i.e., no loops or conditionals) and a limitednumber of C arithmetic and logical operators. Specifically, you are only allowed to use the following eightoperators:! ˜ & ˆ | + << >>A few of the functions further restrict this list. Also, you are not allowed to use any constants longer than 8bits. See the comments in bits.c for detailed rules and a discussion of the desired coding style.EvaluationYour code will be compiled with GCC and run and tested on one of the class machines. Your score will becomputed out of a maximum of 68 points based on the following distribution:35 Correctness of code running on one of the class machines.28 Performance of code, based on number of operators used in each function.5 Style points, based on your instructor’s subjective evaluation of the quality of your solutions and yourcomments.The 14 puzzles you must solve have been given a difficulty rating between 1 and 4, such that their weightedsum totals to 35. We will evaluate your functions using the test arguments in btest.c. You will get fullcredit for a puzzle if it passes all of the tests performed by btest.c, half credit if it fails one test, and nocredit 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 functionwe’ve established a maximum number of operators that you are allowed to use for each function. This limitis very generous and is designed only to catch egregiously inefficient solutions. You will receive two pointsfor each function that satisfies the operator limit.Finally, we’ve reserved 5 points for a subjective evaluation of the style of your solutions and your com-menting. Your solutions should be as clean and straightforward as possible. Your comments should beinformative, but they need not be extensive.Part I: Bit manipulationsTable 1 describes a set of functions that manipulate and test sets of bits. The “Rating” field gives thedifficulty rating (the number of points) for the puzzle, and the “Max ops” field gives the maximum numberof operators you are allowed to use to implement each function.2Name Description Rating Max OpsbitAnd(x,y) x&y using only ˜ and | 1 8bitXor(x,y) xˆy using only & and ˜ 2 14evenBits() Return word with all even-numbered bits set to 1 2 8getByte(x,n) Extract byte n from x 2 6bitMask(hi, lo) Generate mask with bits between hi and lo set to 1 3 16reverseBytes(x) Reverse the bytes of x 3 25leastBitPos(x) Mark position of least significant 1 bit in x 4 6logicalNeg(x) Compute !x without using ! operator 4 12Table 1: Bit-Level Manipulation Functions.Name Description Rating Max OpsminusOne(void) Return a value of -1 1 2tmax(void) Largest two’s complement integer 1 4negate(x) Return -x 2 5isPositive(x) x > 0? 3 8isLess(x,y) x < y? 3 24sm2tc(x) Convert x from signed magnitude to two’s complement 4 15Table 2: Arithmetic FunctionsFunction bitAnd computes the AND function. That is, when applied to arguments x and y, it returns x&y.You may only use the operators | and ˜.Function bitXor should duplicate the behavior of the bit operation ˆ, using only the operations & and ˜.Function evenBits returns a word with all of its even numbered bits set to 1. Bits are numbered from 0(least significant) to 31 (most significant).Function getByte extracts a byte from a word. The bytes within a word are ordered from 0 (least signifi-cant) to 3 (most significant).Function bitMask generates a mask in which all bits between hi and lo are set to 1, and the remainingbits are set to 0.Function reverseBytes reverses the bytes of its input word by swapping bytes 0 and 3, and bytes 1 and2. As before, the bytes within a word are ordered from 0 (least significant) to 3 (most significant).Function leastBitPos generates a mask consisting of a single bit marking the position of the leastsignificant one bit in the argument. If the argument equals 0, it returns 0.Function logicalNeg computes logical negation without using the ! operator.3Part II: Two’s Complement ArithmeticTable 2 describes a set of functions that make use of the two’s complement representation of integers.Function minusOne returns a value of negative one, without using the minus operator.Function tmax returns the largest two’s complement integer.Function negate returns.Function isPositive returns 1 if , and 0 otherwise.Function isLess determines whether x is less than y.Function sm2tc converts its input argument from signed magnitude to two’s
View Full Document