Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

INTEGER CODING RULES: Replace the return statement in each function with one or more lines of C code that implements the function. Your code must

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

INTEGER CODING RULES: Replace the "return" statement in each function with one or more lines of C code that implements the function. Your code must conform to the following style: int Funct(arg1, arg2, ...) { /* brief description of how your implementation works */ int var1 = Expr1; ... int varM = ExprM;

varJ = ExprJ; ... varN = ExprN; return ExprR; }

Each "Expr" is an expression using ONLY the following: 1. Integer constants 0 through 255 (0xFF), inclusive. You are not allowed to use big constants such as 0xffffffff. 2. Function arguments and local variables (no global variables). 3. Unary integer operations ! ~ 4. Binary integer operations & ^ | + > Some of the problems restrict the set of allowed operators even further. Each "Expr" may consist of multiple operators. You are not restricted to one operator per line.

You are expressly forbidden to: 1. Use any control constructs such as if, do, while, for, switch, etc. 2. Define or use any macros. 3. Define any additional functions in this file. 4. Call any functions. 5. Use any other operations, such as &&, ||, -, or ?: 6. Use any form of casting. 7. Use any data type other than int. This implies that you cannot use arrays, structs, or unions.

You may assume that your machine: 1. Uses 2s complement, 32-bit representations of integers. 2. Performs right shifts arithmetically. 3. Has unpredictable behavior when shifting if the shift amount is less than 0 or greater than 31.

EXAMPLES OF ACCEPTABLE CODING STYLE: /* * pow2plus1 - returns 2^x + 1, where 0

/* * pow2plus4 - returns 2^x + 4, where 0

FLOATING POINT CODING RULES

For the problems that require you to implement floating-point operations, the coding rules are less strict. You are allowed to use looping and conditional control. You are allowed to use both ints and unsigneds. You can use arbitrary integer and unsigned constants. You can use any arithmetic, logical, or comparison operations on int or unsigned data.

You are expressly forbidden to: 1. Define or use any macros. 2. Define any additional functions in this file. 3. Call any functions. 4. Use any form of casting. 5. Use any data type other than int or unsigned. This means that you cannot use arrays, structs, or unions. 6. Use any floating point data types, operations, or constants.

NOTES: 1. Use the dlc (data lab checker) compiler (described in the handout) to check the legality of your solutions. 2. Each function has a maximum number of operations (integer, logical, or comparison) that you are allowed to use for your implementation of the function. The max operator count is checked by dlc. Note that assignment ('=') is not counted; you may use as many of these as you want without penalty. 3. Use the btest test harness to check your functions for correctness. 4. Use the BDD checker to formally verify your functions 5. The maximum number of ops for each function is given in the header comment for each function. If there are any inconsistencies between the maximum ops in the writeup and in this file, consider this file the authoritative source.

/* * STEP 2: Modify the following functions according the coding rules. * * IMPORTANT. TO AVOID GRADING SURPRISES: * 1. Use the dlc compiler to check that your solutions conform * to the coding rules. * 2. Use the BDD checker to formally verify that your solutions produce * the correct answers. */

#endif //1 /* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Legal ops: ~ & * Max ops: 14 * Rating: 1 */ int bitXor(int x, int y) { return 2; } /* * tmin - return minimum two's complement integer * Legal ops: ! ~ & ^ | + > * Max ops: 4 * Rating: 1 */ int tmin(void) {

return 2;

} //2 /* * allOddBits - return 1 if all odd-numbered bits in word set to 1 * where bits are numbered from 0 (least significant) to 31 (most significant) * Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1 * Legal ops: ! ~ & ^ | + > * Max ops: 12 * Rating: 2 */ int allOddBits(int x) { return 2; } /* * negate - return -x * Example: negate(1) = -1. * Legal ops: ! ~ & ^ | + > * Max ops: 5 * Rating: 2 */ int negate(int x) { return 2; } //3 /* * isAsciiDigit - return 1 if 0x30 > * Max ops: 15 * Rating: 3 */ int isAsciiDigit(int x) { return 2; } /* * conditional - same as x ? y : z * Example: conditional(2,4,5) = 4 * Legal ops: ! ~ & ^ | + > * Max ops: 16 * Rating: 3 */ int conditional(int x, int y, int z) { return 2; } /* * isLessOrEqual - if x > * Max ops: 24 * Rating: 3 */ int isLessOrEqual(int x, int y) { return 2; } //4 /* * logicalNeg - implement the ! operator, using all of * the legal operators except ! * Examples: logicalNeg(3) = 0, logicalNeg(0) = 1 * Legal ops: ~ & ^ | + > * Max ops: 12 * Rating: 4 */ int logicalNeg(int x) { return 2; } /* howManyBits - return the minimum number of bits required to represent x in * two's complement * Examples: howManyBits(12) = 5 * howManyBits(298) = 10 * howManyBits(-5) = 4 * howManyBits(0) = 1 * howManyBits(-1) = 1 * howManyBits(0x80000000) = 32 * Legal ops: ! ~ & ^ | + > * Max ops: 90 * Rating: 4 */ int howManyBits(int x) { return 0; } //float /* * floatScale2 - Return bit-level equivalent of expression 2*f for * floating point argument f. * Both the argument and result are passed as unsigned int's, but * they are to be interpreted as the bit-level representation of * single-precision floating point values. * When argument is NaN, return argument * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ unsigned floatScale2(unsigned uf) { return 2; } /* * floatFloat2Int - Return bit-level equivalent of expression (int) f * for floating point argument f. * Argument is passed as unsigned int, but * it is to be interpreted as the bit-level representation of a * single-precision floating point value. * Anything out of range (including NaN and infinity) should return * 0x80000000u. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ int floatFloat2Int(unsigned uf) { return 2;

:/5Q 1 Introduction The purpose of this assignment is to become more familiar with bit-level representations of 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. 2 Logistics This is an individual project. All handins are electronic via Canvas. 3 Handout Instructions For all assignments in this class, you should download the handout from Canvas. Your lab materials are contained in a tar archive file called lab1-handout.tar, which you can download from Canvas. Start by copying this file to a directory on your VM in which you plan to do your work. You should use the same VM you installed in lab0 (i.e., Ubuntu 20.04 or Linux Mint 20). Remember that your submission will only be tested on an Ubuntu 20.04 or Linux Mint 20 OS (Operating System) with compiler configurations and libraries similar to those that come preinstalled or installed later via the default repository on those OSes. We advise you to implement and test your code on such machines/VMs before submitting it. This lab will require one additional library/package to be installed "gcc-multilib" in order for you to compile the code successfully. Please run the following command to install it: linux> sudo apt install gcc-multilib Now enter the following command while you are inside the VM (Remember, you must first cd to the same directory that contains the tar file before running this command) linux> tar xvf lab1-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 bits. c file contains a skeleton for each of the 13 programming puzzles. Your assignment is to complete each function skeleton using only straightline code for the integer puzzles (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: !&1+>> 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. 4 The Puzzles This section describes the puzzles that you will be solving in bits. c. Table 1 lists the puzzles in rought order of difficulty from easiest to hardest. 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. Table 1: Datalab puzzles. For the floating point puzzles, value f is the floating-point number having the same bit representation as the unsigned integer uf. For the floating-point puzzles, you will implement some common single-precision floating-point operations. For these puzzles, 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. The included program fshow helps you understand the structure of floating point numbers. To compile fshow, switch to the handout directory and type: uni x> make You can use f show to see what an arbitrary pattern represents as a floating-point number: unix>. /fshow 2080374784 Floating point value 2.658455992e+36 Bit Representation 0x7c00000, sign =0, exponent =f8, fraction =000000 Normalized. 1.00000000002(121) You can also give fshow hexadecimal and floating point values, and it will decipher their bit structure. 5 Evaluation Your score will be computed out of a maximum of 60 points based on the following distribution: 31 Correctness points. 22 Performance points. 7 Style points. Correctness points. The puzzles you must solve have been given a difficulty rating between 1 and 4 , such that their weighted sum totals to 31 . We will evaluate your functions using the btest program, which is described in the next section. You will get full credit for a puzzle if it passes all of the tests performed by btest, 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. You will receive two points for each correct function that satisfies the operator limit. Style points. Finally, we've reserved 7 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. utograding your work e have included some autograding tools in the handout directory btest, dlc, and driver.pl help you check the correctness of your work. - btest: This program checks the functional correctness of the functions in bits.c. To build and use it, type the following two commands: unix> make unix>. /btest Notice that you must rebuild btest each time you modify your bits . c file. You'll find it helpful to work through the functions one at a time, testing each one as you go. You can use the f flag to instruct btest to test only a single function: unix>. /btest f bitxor You can feed it specific function arguments using the option flags 1,2, and 3 : unix>. /btest f bitxor 1425 Check the file README for documentation on running the btest program. - dlc: This is a modified version of an ANSI C compiler from the MIT CILK group that you can use to check for compliance with the coding rules for each puzzle. The typical usage is: unix>./dlcbits.c The program runs silently unless it detects a problem, such as an illegal operator, too many operators, or non-straightline code in the integer puzzles. Running with the -e switch: unix> ./dlc -e bits.c causes dle to print counts of the number of operators used by each function. Type ./dlc -help for a list of command line options. - driver.pl: This is a driver program that uses btest and dle to compute the correctness and performance points for your solution. It takes no arguments: unix> ./driver.pl Your instructors will use driver. pl to evaluate your solution. 6 Handin Instructions To receive a score, you should upload your submission to Canvas. You may handin as often as you like until the due date but please keep the number of submissions as MINIMAL as possible. The last submission is what will be graded. Submit a tar file containing your bits.c implementation to Canvas. Running the make command will generate the tar file, lab1-handin.tar. You can upload this file to Canvas. IMPORTANT: Do not assume your submission will succeed! You should ALWAYS check to see if your submission was successful on Canvas. IMPORTANT: Do not upload files in other archive formats, such as those with extensions .zip, .gzip, or .tgz. 7 Advice - Don't include the stdio. h header file in your bits. c file, as it confuses dle and results in some non-intuitive error messages. You will still be able to use print f in your bits. c file for debugging without including the stdio. h> header, although gec will print a warning that you can ignore. - The dle program enforces a stricter form of C declarations than is the case for C++ or that is enforced by gec. In particular, any declaration must appear in a block (what you enclose in curly braces) before any statement that is not a declaration. For example, it will complain about the following code: int foo (int x) \{ int a=x; a=3; int b=a; / / / Statement that is not a declaration / \}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions

Question

What will the cost of each site link be?

Answered: 1 week ago