Advertisement
Guest User

Bits

a guest
Oct 16th, 2012
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.63 KB | None | 0 0
  1. /*
  2.  * CS:APP Data Lab
  3.  *
  4.  * <Please put your name and userid here>
  5.  *
  6.  * bits.c - Source file with your solutions to the Lab.
  7.  *          This is the file you will hand in to your instructor.
  8.  *
  9.  * WARNING: Do not include the <stdio.h> header; it confuses the dlc
  10.  * compiler. You can still use printf for debugging without including
  11.  * <stdio.h>, although you might get a compiler warning. In general,
  12.  * it's not good practice to ignore compiler warnings, but in this
  13.  * case it's OK.  
  14.  */
  15.  
  16. #if 0
  17. /*
  18.  * Instructions to Students:
  19.  *
  20.  * STEP 1: Read the following instructions carefully.
  21.  */
  22.  
  23. You will provide your solution to the Data Lab by
  24. editing the collection of functions in this source file.
  25.  
  26. INTEGER CODING RULES:
  27.  
  28.   Replace the "return" statement in each function with one
  29.   or more lines of C code that implements the function. Your code
  30.   must conform to the following style:
  31.  
  32.   int Funct(arg1, arg2, ...) {
  33.       /* brief description of how your implementation works */
  34.       int var1 = Expr1;
  35.       ...
  36.       int varM = ExprM;
  37.  
  38.       varJ = ExprJ;
  39.       ...
  40.       varN = ExprN;
  41.       return ExprR;
  42.   }
  43.  
  44.   Each "Expr" is an expression using ONLY the following:
  45.   1. Integer constants 0 through 255 (0xFF), inclusive. You are
  46.       not allowed to use big constants such as 0xffffffff.
  47.   2. Function arguments and local variables (no global variables).
  48.   3. Unary integer operations ! ~
  49.   4. Binary integer operations & ^ | + << >>
  50.    
  51.   Some of the problems restrict the set of allowed operators even further.
  52.   Each "Expr" may consist of multiple operators. You are not restricted to
  53.   one operator per line.
  54.  
  55.   You are expressly forbidden to:
  56.   1. Use any control constructs such as if, do, while, for, switch, etc.
  57.   2. Define or use any macros.
  58.   3. Define any additional functions in this file.
  59.   4. Call any functions.
  60.   5. Use any other operations, such as &&, ||, -, or ?:
  61.   6. Use any form of casting.
  62.   7. Use any data type other than int.  This implies that you
  63.      cannot use arrays, structs, or unions.
  64.  
  65.  
  66.   You may assume that your machine:
  67.   1. Uses 2s complement, 32-bit representations of integers.
  68.   2. Performs right shifts arithmetically.
  69.   3. Has unpredictable behavior when shifting an integer by more
  70.      than the word size.
  71.  
  72. EXAMPLES OF ACCEPTABLE CODING STYLE:
  73.   /*
  74.    * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
  75.    */
  76.   int pow2plus1(int x) {
  77.      /* exploit ability of shifts to compute powers of 2 */
  78.      return (1 << x) + 1;
  79.   }
  80.  
  81.   /*
  82.    * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
  83.    */
  84.   int pow2plus4(int x) {
  85.      /* exploit ability of shifts to compute powers of 2 */
  86.      int result = (1 << x);
  87.      result += 4;
  88.      return result;
  89.   }
  90.  
  91. FLOATING POINT CODING RULES
  92.  
  93. For the problems that require you to implent floating-point operations,
  94. the coding rules are less strict.  You are allowed to use looping and
  95. conditional control.  You are allowed to use both ints and unsigneds.
  96. You can use arbitrary integer and unsigned constants.
  97.  
  98. You are expressly forbidden to:
  99.   1. Define or use any macros.
  100.   2. Define any additional functions in this file.
  101.   3. Call any functions.
  102.   4. Use any form of casting.
  103.   5. Use any data type other than int or unsigned.  This means that you
  104.      cannot use arrays, structs, or unions.
  105.   6. Use any floating point data types, operations, or constants.
  106.  
  107.  
  108. NOTES:
  109.   1. Use the dlc (data lab checker) compiler (described in the handout) to
  110.      check the legality of your solutions.
  111.   2. Each function has a maximum number of operators (! ~ & ^ | + << >>)
  112.      that you are allowed to use for your implementation of the function.
  113.      The max operator count is checked by dlc. Note that '=' is not
  114.      counted; you may use as many of these as you want without penalty.
  115.   3. Use the btest test harness to check your functions for correctness.
  116.   4. Use the BDD checker to formally verify your functions
  117.   5. The maximum number of ops for each function is given in the
  118.      header comment for each function. If there are any inconsistencies
  119.      between the maximum ops in the writeup and in this file, consider
  120.      this file the authoritative source.
  121.  
  122. /*
  123.  * STEP 2: Modify the following functions according the coding rules.
  124.  *
  125.  *   IMPORTANT. TO AVOID GRADING SURPRISES:
  126.  *   1. Use the dlc compiler to check that your solutions conform
  127.  *      to the coding rules.
  128.  *   2. Use the BDD checker to formally verify that your solutions produce
  129.  *      the correct answers.
  130.  */
  131.  
  132.  
  133. #endif
  134. /*
  135.  * bitAnd - x&y using only ~ and |
  136.  *   Example: bitAnd(6, 5) = 4
  137.  *   Legal ops: ~ |
  138.  *   Max ops: 8
  139.  *   Rating: 1
  140.  */
  141. int bitAnd(int x, int y){
  142.        /*by taking the one compliment of x and y, then then taking the one's compliment of the union of the two bit sequence, it produces the bit operator &*/
  143.        int result = ~(~x|~y);
  144.        return result;
  145. }
  146. /*
  147.  * getByte - Extract byte n from word x
  148.  *   Bytes numbered from 0 (LSB) to 3 (MSB)
  149.  *   Examples: getByte(0x12345678,1) = 0x56
  150.  *   Legal ops: ! ~ & ^ | + << >>
  151.  *   Max ops: 6
  152.  *   Rating: 2
  153.  */
  154. int getByte(int x, int n) {
  155.   /*shifts x over to get the right byte, then takes the exclusive or of the same sequence that doesn't include the right byte, thus producing the only right byte*/
  156.   int rightshift = (x >> (n << 3));
  157.   int temp = rightshift >> 8;
  158.   int leftshift = temp << 8;
  159.   int result = leftshift ^ rightshift;
  160.   return result;
  161.  
  162. }
  163. /*
  164.  * logicalShift - shift x to the right by n, using a logical shift
  165.  *   Can assume that 0 <= n <= 31
  166.  *   Examples: logicalShift(0x87654321,4) = 0x08765432
  167.  *   Legal ops: ~ & ^ | + << >>
  168.  *   Max ops: 20
  169.  *   Rating: 3
  170.  */
  171. int logicalShift(int x, int n) {
  172.  /*by making a bit string of N 1's then zeros, you can then shift x and remove any one's that come from the left*/
  173.   int temp = ~(~0 << n) << (~(~32 + n));
  174.   int result = ~temp & ((x >> n) | temp);
  175.   return result;
  176. }
  177. /*
  178.  * bitCount - returns count of number of 1's in word
  179.  *   Examples: bitCount(5) = 2, bitCount(7) = 3
  180.  *   Legal ops: ! ~ & ^ | + << >>
  181.  *   Max ops: 40
  182.  *   Rating: 4
  183.  */
  184.  
  185. int bitCount(int x) {
  186. /* we take our sequence and then combine with a 0101.. sequence and again with x shifted right one, then add them together.  This is taking each two */
  187.   int result = (x & 0x55555555) + ((x >> 1) & 0x55555555);
  188.   result = (result & 0x33333333) + ((result >> 2) & 0x33333333);
  189.   result = (result & 0x0F0F0F0F) + ((result >> 4) & 0x0F0F0F0F);
  190.   result = (result & 0x00FF00FF) + ((result >> 8) & 0x00FF00FF);
  191.   result = (result & 0x0000FFFF) + ((result >> 16) & 0x0000FFFF);
  192.   return result;
  193. }
  194. /*
  195.  * bang - Compute !x without using !
  196.  *   Examples: bang(3) = 0, bang(0) = 1
  197.  *   Legal ops: ~ & ^ | + << >>
  198.  *   Max ops: 12
  199.  *   Rating: 4
  200.  */
  201. int bang(int x) {
  202.   return 2;
  203. }
  204. /*
  205.  * tmin - return minimum two's complement integer
  206.  *   Legal ops: ! ~ & ^ | + << >>
  207.  *   Max ops: 4
  208.  *   Rating: 1
  209.  */
  210. int tmin(void) {
  211.   return 2;
  212. }
  213. /*
  214.  * fitsBits - return 1 if x can be represented as an
  215.  *  n-bit, two's complement integer.
  216.  *   1 <= n <= 32
  217.  *   Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
  218.  *   Legal ops: ! ~ & ^ | + << >>
  219.  *   Max ops: 15
  220.  *   Rating: 2
  221.  */
  222. int fitsBits(int x, int n) {
  223.   return 2;
  224. }
  225. /*
  226.  * divpwr2 - Compute x/(2^n), for 0 <= n <= 30
  227.  *  Round toward zero
  228.  *   Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
  229.  *   Legal ops: ! ~ & ^ | + << >>
  230.  *   Max ops: 15
  231.  *   Rating: 2
  232.  */
  233. int divpwr2(int x, int n) {
  234.     return 2;
  235. }
  236. /*
  237.  * negate - return -x
  238.  *   Example: negate(1) = -1.
  239.  *   Legal ops: ! ~ & ^ | + << >>
  240.  *   Max ops: 5
  241.  *   Rating: 2
  242.  */
  243. int negate(int x) {
  244.   return 2;
  245. }
  246. /*
  247.  * isPositive - return 1 if x > 0, return 0 otherwise
  248.  *   Example: isPositive(-1) = 0.
  249.  *   Legal ops: ! ~ & ^ | + << >>
  250.  *   Max ops: 8
  251.  *   Rating: 3
  252.  */
  253. int isPositive(int x) {
  254.   return 2;
  255. }
  256. /*
  257.  * isLessOrEqual - if x <= y  then return 1, else return 0
  258.  *   Example: isLessOrEqual(4,5) = 1.
  259.  *   Legal ops: ! ~ & ^ | + << >>
  260.  *   Max ops: 24
  261.  *   Rating: 3
  262.  */
  263. int isLessOrEqual(int x, int y) {
  264.   return 2;
  265. }
  266. /*
  267.  * ilog2 - return floor(log base 2 of x), where x > 0
  268.  *   Example: ilog2(16) = 4
  269.  *   Legal ops: ! ~ & ^ | + << >>
  270.  *   Max ops: 90
  271.  *   Rating: 4
  272.  */
  273. int ilog2(int x) {
  274.   return 2;
  275. }
  276. /*
  277.  * float_neg - Return bit-level equivalent of expression -f for
  278.  *   floating point argument f.
  279.  *   Both the argument and result are passed as unsigned int's, but
  280.  *   they are to be interpreted as the bit-level representations of
  281.  *   single-precision floating point values.
  282.  *   When argument is NaN, return argument.
  283.  *   Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  284.  *   Max ops: 10
  285.  *   Rating: 2
  286.  */
  287. unsigned float_neg(unsigned uf) {
  288.  return 2;
  289. }
  290. /*
  291.  * float_i2f - Return bit-level equivalent of expression (float) x
  292.  *   Result is returned as unsigned int, but
  293.  *   it is to be interpreted as the bit-level representation of a
  294.  *   single-precision floating point values.
  295.  *   Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  296.  *   Max ops: 30
  297.  *   Rating: 4
  298.  */
  299. unsigned float_i2f(int x) {
  300.   return 2;
  301. }
  302. /*
  303.  * float_twice - Return bit-level equivalent of expression 2*f for
  304.  *   floating point argument f.
  305.  *   Both the argument and result are passed as unsigned int's, but
  306.  *   they are to be interpreted as the bit-level representation of
  307.  *   single-precision floating point values.
  308.  *   When argument is NaN, return argument
  309.  *   Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  310.  *   Max ops: 30
  311.  *   Rating: 4
  312.  */
  313. unsigned float_twice(unsigned uf) {
  314.   return 2;
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement