CS 213 Fall 2002 Lab Assignment L1 The Data Lab Manipulating Bits Assigned Aug 29 Due Thu Sept 12 11 59PM Dave O Hallaron droh cs cmu edu is the lead person for this assignment 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 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 Logistics This is an individual project The only handin will be electronic Any clarifications and revisions to the assignment will be posted on the course Web page Hand Out Instructions All files you need are in the directory afs cs cmu edu academic class 15213 f02 L1 Start 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 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 1 Looking at the file bits c you ll notice a C structure team into which you should insert the requested identifying 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 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 class machines Your score will be computed 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 your comments The 14 puzzles you must solve have been given a difficulty rating between 1 and 4 such that their weighted sum totals to 35 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 You will receive two points for 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 commenting Your solutions should be as clean and straightforward as possible Your comments should be informative but they need not be extensive Part I Bit manipulations Table 1 describes a set of functions that manipulate and test sets of bits The Rating field gives the difficulty rating the number of points for the puzzle and the Max ops field gives the maximum number of operators you are allowed to use to implement each function 2 Name bitAnd x y bitXor x y evenBits getByte x n bitMask hi lo reverseBytes x leastBitPos x logicalNeg x Description x y using only and x y using only and Return word with all even numbered bits set to 1 Extract byte n from x Generate mask with bits between hi and lo set to 1 Reverse the bytes of x Mark position of least significant 1 bit in x Compute x without using operator Rating 1 2 2 2 3 3 4 4 Max Ops 8 14 8 6 16 25 6 12 Rating 1 1 2 3 3 4 Max Ops 2 4 5 8 24 15 Table 1 Bit Level Manipulation Functions Name minusOne void tmax void negate x isPositive x isLess x y sm2tc x Description Return a value of 1 Largest two s complement integer Return x x 0 x y Convert x from signed magnitude to two s complement Table 2 Arithmetic Functions Function bitAnd computes the A ND 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 significant to 3 most significant Function bitMask generates a mask in which all bits between hi and lo are set to 1 and the remaining bits are set to 0 Function reverseBytes reverses the bytes of its input word by swapping bytes 0 and 3 and bytes 1 and 2 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 least significant one bit in the argument If the argument equals 0 it returns 0 Function logicalNeg computes logical negation without using the operator 3 Part II Two s Complement Arithmetic Table 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 complement 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 our class fish
View Full Document