Six important experimental LAB1 of Csapp

Source: Internet
Author: User

Csapp && lab1



------------------------------------------ --------------------------The experimental requirements-------------------------- ------------------------------------------

The Bit Puzzles


This section describes the puzzles so you'll be a solving in bits.c. More complete (and definitive, should there are any inconsistencies) documentation are found in the BITS.C file itself.

Bit manipulations
The table below describes a set of functions that manipulate and test sets of bits. The Rating column gives the difficulty Rating (the number of points) for each puzzle and the Description column states the Desired output for each puzzle along with the constraints. See the comments in BITS.C for more details on the desired behavior of the functions. Also refer to the test functions in TESTS.C. These is used as reference functions to express the correct behavior of your functions, although they don ' t satisfy the C Oding rules for your functions.


Both ' s complement arithmetic


The following table describes a set of functions that make use of the of the and of the "s complement representation of integers. Again, refer to the comments in BITS.C and the reference versions in TESTS.C for more information.


------------------------------------------ -------------------------above are the requirements of the experiment-------------------------- -----------------------------------




Lab Material: BITS.C

/* * CSE 351 HW1 (Data Lab) * <please put your name and userid here> * * bits.c-source file with your Soluti ONS to the Lab. * This is the file you'll hand in to your instructor. * * Warning:do not include the <stdio.h> header; It confuses the DLC * compiler. You can still the use of printf for debugging without including * <stdio.h&gt, and although you might get a compiler warning.   In general, * It's not good the practice to ignore compiler warnings, but the this * case it's OK. */#if 0/* * instructions to Students: * * STEP 1:read The following instructions carefully. */you would provide your solution to this homework byediting the collection of functions in this source file. 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 of the "Expr" is a expression using only the following:1. Integer Constants 0 through 255 (0xFF), inclusive.  You aren't 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 of the "Expr" may consist of multiple operators.  You is not a restricted to one operator per line.  You is expressly forbidden to:1. Use any control constructs such as if, does, while, for, switch, etc.  2. Define or use any macros.  3. Define any additional functions in the 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, cannot use ARRays, structs, or unions.  Assume that your machine:1. Uses 2s complement, 32-bit representations of integers.  2. Performs right shifts arithmetically. 3. Have unpredictable behavior when shifting a integer by more than the word size.  EXAMPLES of acceptable CODING STYLE:/* * Pow2plus1-returns 2^x + 1, where 0 <= x <= */int Pow2plus1 (int  x) {/* Exploit ability of shifts to compute powers of 2 */return (1 << x) + 1; }/* * Pow2plus4-returns 2^x + 4, where 0 <= x <= */int POW2PLUS4 (int x) {/* Exploit ability of Shi     FTS to compute powers of 2 */int result = (1 << x);     result + = 4;  return result; }floating Point CODING rulesfor The problems this require you to implent floating-point operations,the CODING rules is le  SS Strict.  You is allowed to use looping andconditional control. You is allowed to use both ints and unsigneds. You can use arbitrary integer and unsigned constants. You are expresslyForbidden To:1. Define or use any macros.  2. Define any additional functions in the this file.  3. Call any functions.  4. Use any form of casting.  5. Use any data type, other than int or unsigned.  This means the 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 have a maximum number of operators (! ~ & ^ | + << >>) that's allowed to      Your implementation of the function. The max operator count is checked by DLC. Note that ' = ' was not counted;  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 was given in the header comment for each function. If there is any INconsistencies between the maximum ops in the writeup and in this file, consider this file the authoritative sour ce./* * STEP 2:modify The following functions according the coding rules. * * IMPORTANT. To AVOID GRADING surprises: * 1. Use the DLC compiler to check for that your solutions conform * to the coding rules. * 2. Use the BDD Checker to formally verify that your solutions produce * the correct answers.  */#endif//rating:1/* * bitand-x&y using only ~ and | * Example:bitand (6, 5) = 4 * Legal Ops: ~ | * Max ops:8 * rating:1 */int bitand (int x, int y) {return 2;} /* * 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;} /* * Thirdbits-return Word with every third bit (starting from the LSB) set to 1 * Legal OPS:! ~ & ^ | + << >> * Max ops:8 * rating:1 */int thirdbits (void) {return 2;} rating:2/* * Fitsbits-return 1 if X can is represented as an * n-bit, and both ' s complement integer. * 1 <= N <= * examples:fitsbits (5,3) = 0, fitsbits ( -4,3) = 1 * Legal OPS:! ~ & ^ | + << >> * Max ops:15 * rating:2 */int fitsbits (int x, int n) {return 2;}  /* * Sign-return 1 if positive, 0 if zero, and-1 if negative * examples:sign () = 1 * SIGN (-23) =-1 * Legal OPS:! ~ & ^ | + << >> * Max ops:10 * rating:2 */int sign (int x) {return 2;} /* * Getbyte-extract byte n from Word X * Bytes numbered from 0 (LSB) to 3 (MSB) * Examples:getbyte (0x12345678,1) = 0x56 * Legal OPS:! ~ & ^ | + << >> * Max ops:6 * rating:2 */int getbyte (int x, int n) {return 2;}    rating:3/* * Logicalshift-shift x to the right by N, using a logical shift * Can assume that 0 <= n <= 31 * Examples:logicalshift (0x87654321,4) = 0x08765432 * Legal Ops: ~ & ^ | + << >> * Max ops:20 *   Rating:3 */int logicalshift (int x, int n) {return 2;} /* * Addok-determine if can compute x+y without overflow * EXAMPLE:ADDOK (0x80000000,0x80000000) = 0, * ad DOK (0x80000000,0x70000000) = 1, * Legal OPS:! ~ & ^ | + << >> * Max ops:20 * rating:3 */int addok (int x, int y) {return 2;} /* Invert-return x with the n bits, this begin at position p inverted * (i.e., turn 0 to 1 and vice versa) an D the rest left * unchanged. Consider the indices of X to begin with the Low-order * bit numbered as 0. * Example:invert (0x80000000, 0, 1) = 0x80000001, * Invert (0x0000008e, 3, 3) = 0x000000b6, * Legal OPS:! ~ & ^ | + << >> * Max ops:20 * rating:3 */int Invert (int x, int p, int n) {return 2;} rating:4/* * Bang-compute!x without using! * Examples:bang (3) = 0, bang (0) = 1 * Legal Ops: ~ & ^ |  + << >> * Max ops:12 * rating:4 */int Bang (int x) {return 2;} Extra credit:rating:3/* * 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;} Extra credit:rating:4/* * Ispower2-returns 1 if x is a power of 2, and 0 otherwise * EXAMPLES:ISPOWER2 (5) = 0, I SPower2 (8) = 1, isPower2 (0) = 0 * Note that the no negative number is a power of 2. * Legal OPS:! ~ & ^ | + << >> * Max ops:20 * rating:4 */int isPower2 (int x) {return 2;}







For this divine bit operation can not hold, some functions are not implemented, welcome to Exchange discussion: D

My solution:

/* * CSE 351 HW1 (Data Lab) * Code Writer:jason Leaster * e-mail: [email protected] * *///rating:1/* * Bitan  D-x&y using only ~ and | * Example:bitand (6, 5) = 4 * Legal Ops: ~ | * Max ops:8 * rating:1 */int bitand (int x, int y) {return ~ ((~x) | (~y));} /* * 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 ~ ((~ (x& (~y))) & (~ ((~x) &y));} /* TODO * Thirdbits-return Word with every third bit (starting from the LSB) set to 1 * Legal OPS:! ~ & ^ |  + << >> * Max ops:8 * rating:1 */int thirdbits (void) {int ret = 0; ret = Ret<<3 |  1<<2; ret = Ret<<3 |  1<<2; return 1<<2;//:) I not very sure on what the author want me ...} rating:2/* TODO * Fitsbits-return 1 if X can is represented as an * n-bit, and both ' s complement integer. * 1 <= N <= * examples:fitsbits (5, 3) = 0, fitsbits ( -4,3) = 1 * Legal OPS:! ~ & ^ | + << >> * Max ops:15 * rating:2 */int fitsbits (int x, int n) {int num = (x << (32+ (~n + 1))) >& Gt (+ + (~n + 1)); int result =! (num ^ x) return result;  /* * Sign-return 1 if positive, 0 if zero, and-1 if negative * examples:sign () = 1 * SIGN (-23) =-1 * Legal OPS:! ~ & ^ | + << >> * Max ops:10 * rating:2 */int sign (int x) {return (x>>63) | (!! x);} /* * Getbyte-extract byte n from Word X * Bytes numbered from 0 (LSB) to 3 (MSB) * Examples:getbyte (0x12345678,1) = 0x56 * Legal OPS:! ~ & ^ | + << >> * Max ops:6 * rating:2 */int getbyte (int x, int n) {return (x>> (n<<3)) & 0xFF; }//rating:3/* * Logicalshift-shift x to the right by N, using a logical shift * Can assume that 0 <= N <= 31 * Examples:logicalshift (0x87654321,4) = 0x08765432 * Legal Ops: ~ & ^ | + << >> * Max Ops: * Rating:3 */int logicalshift (int x, int n) {return (X & (~ (1<<31))) >>n;} /* * Addok-determine if can compute x+y without overflow * EXAMPLE:ADDOK (0x80000000,0x80000000) = 0, * ad DOK (0x80000000,0x70000000) = 1, * Legal OPS:! ~ & ^ |  + << >> * Max ops:20 * rating:3 */int addok (int x, int y) {int sum = x+y; int neg_over = (x>>31 & 0x1) & (Y>>31 & 0x1) & (! (  Sum>>31 & 0x1)); int pos_over = (! ( X>>31 & 0x1) & (!) (  Y>>31 & 0x1)) & ((Sum>>31 & 0x1)); Return (!neg_over) & (!pos_over);} /* Invert-return x with the n bits, this begin at position p inverted * (i.e., turn 0 to 1 and vice versa) an D the rest left * unchanged. Consider the indices of X to begin with the Low-order * bit numbered as 0. * Example:invert (0x80000000, 0, 1) = 0x80000001, * Invert (0x0000008e, 3, 3) = 0x000000b6, * Legal oPs:! ~ & ^ | + << >> * Max ops:20 * rating:3 */int Invert (int x, int p, int n) {return;} rating:4/* * Bang-compute!x without using! * Examples:bang (3) = 0, bang (0) = 1 * Legal Ops: ~ & ^ | + << >> * Max ops:12 * rating:4 */int Bang (int x) {int X1=x | ( X&GT;&GT;16); int x2=x1| (x1>>8); int x3=x2| (x2>>4); int x4=x3| (x3>>2); int x5=x4| (x4>>1); return (X5 & 0x1);} Extra credit:rating:3/* * 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) {int ans = (!! x) <<31) &y + ((!x) <<31) &z;return ans;} Extra credit:rating:4/* * Ispower2-returns 1 if x is a power of 2, and 0 otherwise * EXAMPLES:ISPOWER2 (5) = 0, I SPower2 (8) = 1, isPower2 (0) = 0 * Note that the no negative number is a power of 2. * Legal OPS:! ~ & ^ | + << >> * Max oPS:20 * rating:4 */int isPower2 (int x) {/*todo*/return-1;//} 



Kissing Hayez French oil on canvas 1859 121x97cm private collection
This painting is a scene of a couple's deep kissing in the hallway. The picture composition is simple, the painter arranges the character in the picture is central, highlights the theme. With a delicate pen, the blue dress of the woman is shining brightly, and the cuffs of the lace are graceful and tactful. The hem of the skirt is very strong under the light, and the woman's graceful appearance is vividly. Men's dark red robes and red trousers contrast harmoniously with color. Dark yellow light through the couple projected on the wall, for the screen to spread a layer of affectionate warm atmosphere. Although the work is not a nude painting, it has been considered by critics to be one of his masterpieces. The work was so compelling at the time that it aroused wide respect among the people of the time. The content of the work is anonymous, and it does not require any esoteric literary interpretation to be able to read it. the most taste of the painting place, but also the best picture of the place, is the man's left leg pedal on a step, all the feeling is from here.  Zheng Yi ArtThat this work has a shocking aesthetic and a strong sense of modernity.



Six important experimental LAB1 of Csapp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.