C代写:CS213 Manipulating Bits

CMU的基础Lab作业,主要是练习位操作和浮点数的各种操作。
作业需要跑过一堆巨细无比的测试,需要考虑到所有的边界情况。

Introduction

The purpose of this assignment is to become more familiar with bit-level representations of common patterns, integers, and floating-point numbers. 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

  • This is an individual project. All handins are electronic using the Autolab service.
  • You should do all of your work in an Andrew directory, using either the shark machines or a Linux Andrew machine.

Logging in to Autolab

All 213/513 labs are being offered this term through a Web service developed by CMU students and faculty called Autolab. Before you can download your lab materials, you will need to update your Autolab account. Point your browser at the Autolab front page.
You will be asked to authenticate via Shibboleth. After you authenticate the first time, Autolab will prompt you to update your account information with a nickname. Your nickname is the external name that identifies you on the public scoreboards that Autolab maintains for each assignment, so pick something interesting!
You can change your nickname as often as you like. Once you have updated your account information, click on “Save Changes” button, and then select the “Home” link to proceed to the main Autolab page.
You must be enrolled to receive an Autolab account. If you added the class late, you might not be included in Autolab’s list of valid students. In this case, you won’t see the 213 course listed on your Autolab home page. If this happens, contact the staff and ask for an account.
If you are still on the waitlist for the course, then download a copy of the file datalab-handout.tar from the course schedule web page. You can get working on the lab and then get an Autolab account once you are enrolled.

The Puzzles

This section describes the puzzles that you will be solving in bits.c.

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. See the comments in bits.c for more
details on the desired behavior of the functions. You may also refer to the test functions in tests.c. These are used as reference functions to express the correct behavior of your functions, although they don’t satisfy the coding rules for your functions.

Name Description Rating Max Ops
bitOr(x,y) Return x or y 1 8
specialBits() Return 0xFFCA3FFF 1 3
isZero(x) Determine if x = 0 1 2
anyEvenBit(x) Return 1 if any even-numbered bit in x is set to 1 2 12
rotateLeft(x,n) Rotate x to the left by n 3 25
bitReverse(x) Reverse the order of bits in x 4 45

Table 1: Bit-Level Manipulation Functions.

Two’s Complement Arithmetic

Table 2 describes a set of functions that make use of the two’s complement representation of integers. Again, refer to the comments in bits.c and the reference versions in tests.c for more information.

Name Description Rating Max Ops
negate(x) Return −x 2 5
leastBitPos(x) Mark the position of the least significant 1 in x 2 6
divpwr2(x,n) Compute x/2^n 2 15
isLess(x,y) Determine if x < y 3 24
isPower2(x) Determine if x is a number of the form 2^k 4 20

Table 2: Arithmetic Functions

Floating-Point Operations

For this part of the assignment, you will implement some common single-precision floating-point operations. In this section, you are allowed to use standard control structures (conditionals, loops), and you may use both int and unsigned data types, including arbitrary unsigned and integer constants. You may not use any unions, structs, or arrays. Most significantly, you may not use any floating point data types, operations, or constants. Instead, any floating-point operand will be passed to the function as having type unsigned, and any returned floating-point value will be of type unsigned. Your code should perform the bit manipulations that implement the specified floating point operations.
Table 3 describes a set of functions that operate on the bit-level representations of single-precision, floating-point numbers. Refer to the comments in bits.c and the reference versions in tests.c for more information.

Name Description Rating Max Ops
float_abs(x) Compute abs(x) 2 10
float_i2f(i) Convert integer i to floating point 4 30
float_times64(x) Compute 64x 4 35

Table 3: Floating-Point Functions.
The included program fshow helps you understand the structure of floating point numbers. To compile fshow, switch to the handout directory and type:

linux> make

You can use fshow to see how a bit pattern represents a floating-point number, using either a decimal or hex representation of the pattern:

linux> ./fshow 2080374784

Floating point value 2.658455992e+36
Bit Representation 0x7c000000, sign = 0, exponent = f8, fraction = 000000
Normalized. 1.0000000000 X 2ˆ(121)

linux> ./fshow 0x15213

Floating point value 1.212781782e-40
Bit Representation 0x00015213, sign = 0, exponent = 0x00, fraction = 0x015213
Denormalized. +0.0103172064 X 2ˆ(-126)

You can also give fshow floating-point values, and it will decipher their bit structure.

linux> ./fshow 15.213

Floating point value 15.2130003
Bit Representation 0x41736873, sign = 0, exponent = 0x82, fraction = 0x736873
Normalized. +1.9016250372 X 2ˆ(3)

Evaluation

Your score will be computed out of a maximum of 63 points based on the following distribution:

  • 35 Correctness of code.
  • 28 Performance of code, based on number of operators used in each function.

Correctness points. 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 use the the dlc compiler to check that your function follows the coding rules. We will use the BDD checker to verify that your function is correct. You will get full credit for a puzzle only if it follows all of the coding rules and it passes all of the tests performed by the BDD checker, and no credit otherwise.
Performance points. 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. We will use the dlc compiler to verify that you’ve satisfied the operator limit. You will receive two points for each correct function that satisfies the operator limit.