0 Introduction ENCE 3241 Data Lab 60 points Due February 19, 2010, by 11:59 PM The purpose of this assignment is for you 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. You may work in pairs for this assignment, if you wish. No groups of more than two people are permitted. You do not have to work with the same person you worked with on other labs. I highly recommend you work in pairs. Compiler: We will grade using, and your programs must compile and run under CygWin on, GCC version 3.4.4 under Linux kernel version 2.6. If you do not use this environment for development of your solutions, we strongly recommend that you test them under it. Source: This lab was derived, through varying paths and amounts of change, from the Data Lab provided in the instructor s package that goes along with Bryant and O Hallaron. 1 Getting started We provide you with a zip file datalab.zip. Download this file to your computer, and unpack it as usual or with the following command: % tar xf datalab.tar (Ooops! Ignore this) This will create a directory datalab containing many files. The only file you may modify is bits.c. Your assignment is to complete each function skeleton using only straighline code, i.e. no loops or conditionals, and a limited number of arithmetic and logical operators. Specifically, you are allowed to use the following eight operators:! ~ & ^ + >> <<
A few of the functions further restrict this list. See the comments in bits.c for detailed rules and a discussion of the desired coding style. 2 Instructions Your goal is to modify your copy of bits.c so that it passes all the tests without violating any of the coding guidelines. 2.1 Modifying bits.c and checking it with dlc Carefully read the instructions in bits.c before you start. These give the coding rules you are required to follow. I have not provided the dlc progrm to check your code for compliance with the rules; to verify the compliance of your modifications to bits.c, you have to follow the rules. 2.2 Testing for correctness with btest If your modifications to bits.c meet the coding rules, it is then time to check whether the functions work correctly. We have provided a makefile and some additional C code to combine with your code, creating a test harness; this program will be called btest. Build it with the following command: % make btest Then, test your code with: %./btest -e3 tee btest.output btest can give voluminous output; the above will save it into a file called btest.output, in addition to displaying it on the screen. Also, the above command limits the number of errors reported per puzzle to three if you wish, you can vary the number after -e to change the limit, or omit the flag for no limit. Please note: you must rebuild the test harness each time you modify bits.c.
2.3 Puzzles The following are the bit-manipulation functions which you must implement. Each puzzle has a difficulty rating from one to four. Additionally, each puzzle has a target number of operations to earn full credit, your solution cannot exceed this. 1. bitand(x, y): Calculate the bitwise and of x and y, i.e. (x&y), using only ~ and. Difficulty rating: 1; maximum operation count: 8. 2. tmin(): Return the minimum two s complement integer (Note the restriction on constants). Difficulty rating: 1; maximum operation count: 4. 3. isnotequal(x, y): Return 0 if x == y, and 1 otherwise. Difficulty rating: 2; maximum operation count: 6. 4. negate(x): Return x. Difficulty rating: 2; maximum operation count: 5. 5. logicalshift(x, n): Shift x to the right by n, using a logical shift. Difficulty rating: 3; maximum operation count: 16. 6. isgreater(x, y): Return 1 if x > y, else return 0. Difficulty rating: 3; maximum operation count: 24. 7. tc2sm(x): Convert from two s complement to sign-magnitude where the MSB is the sign bit. Difficulty rating: 4; maximum operation count: 15. 8. bitcount(x): Returns the number of 1 s in the binary representation of x. Difficulty rating: 4; maximum operation count: 40. 2.4 Grading The score given by btest is the rating score for your work. To compute your grade, we will use the following table. Each difficulty level has two puzzles.
Rating Points per puzzle Total points 1 3 6 2 6 12 3 9 18 4 12 24 Example: btest reports 3.20/4.00 for bitparity(), which is a rating-4 puzzle. This converts to a grade of 9.6/12 on that puzzle. 1 Additionally, we will deduct up to 20% per-puzzle if your operation count exceeds the limit. If the number of operations used is equal to or less than the limit, there is no deduction; if the number of operations is twice the limit or more, we will deduct 20%. Between these, we will linearly scale the deduction based on your operation count. Example, continued: The limit for bitparity() is 20 operations. The 9.6-point solution uses 30 operations. This is halfway between the limit (20) and twice the limit (40), so the deduction is half of 20%, or 10%; the 9.6-point score on that puzzle is reduced by 10% to 8.64/12. 3 Suggestions In solving these puzzles, you may wish to consult the following sources: Also: The textbook, 2.1.8-2.1.10, including the practice problems whose solutions are in the back of the chapter. Also, see 2.3.6 and 2.3.7. C by Dissection, the first part of Chapter 3 on logical operators, and Appendix C on bitwise operators. K&R 2 2.9. Use the right computer. Use the right compiler. 1 You may notice that the above is simply multiplying the btest score by 3. This is correct, though the following paragraph complicates things. 2 The C Programming Language by Kernighan and Ritchie
Ensure both dlc and btest are happy with your work. Change only files you re supposed to. Follow submission instructions. Keep backup copies. Use the identities of Boolean algebra or truth tables to show some logical equivalence of Boolean functions. 4 Submission instructions 4.1 Readme file Please submit only the files listed in this section using the Digital Drop Box. Submit: 1. bits.c 2. README The readme file contains meta-information about you and your work, and should contain the following sections. This file must be well-organized and clear, and please name it README exactly. 3 Team. Names, student ID numbers, section numbers, and email addresses of all team members. GCC version. Include the first line of output from gcc --version. (This double-checks that you are using the tools we require for this assignment.) Miscellaneous info. Any other information you want to tell us about your programs. (This section is optional.) 3 Please don t name it readme, Readme, README.TXT, or anything other than README.