Advertisement
Guest User

Untitled

a guest
Feb 20th, 2011
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. *
  2. * CS:APP Data Lab
  3. *
  4. * bits.c - Source file with your solutions to the Lab.
  5. * This is the file you will hand in to your instructor.
  6. *
  7. * WARNING: Do not include the <stdio.h> header; it confuses the dlc
  8. * compiler. You can still use printf for debugging without including
  9. * <stdio.h>, although you might get a compiler warning. In general,
  10. * it's not good practice to ignore compiler warnings, but in this
  11. * case it's OK.
  12. */
  13.  
  14. #include "btest.h"
  15. #include <limits.h>
  16.  
  17. /*
  18. * Instructions to Students:
  19. *
  20. * STEP 1: Fill in the following struct with your identifying info.
  21. */
  22. team_struct team =
  23. {
  24. /* Team name: Replace with either:
  25. Your login ID if working as a one person team
  26. or, ID1+ID2 where ID1 is the login ID of the first team member
  27. and ID2 is the login ID of the second team member */
  28. "sreisman@luc.edu+mailpbehrens@gmail.com",
  29. /* Student name 1: Replace with the full name of first team member */
  30. "Steve Reisman",
  31. /* Login ID 1: Replace with the login ID of first team member */
  32. "sreisman@luc.edu",
  33.  
  34. /* The following should only be changed if there are two team members */
  35. /* Student name 2: Full name of the second team member */
  36. "Patrick Behrens",
  37. /* Login ID 2: Login ID of the second team member */
  38. "mailpbehrens@gmail.com"
  39. };
  40.  
  41. #if 0
  42. /*
  43. * STEP 2: Read the following instructions carefully.
  44. */
  45.  
  46. You will provide your solution to the Data Lab by
  47. editing the collection of functions in this source file.
  48.  
  49. CODING RULES:
  50.  
  51. Replace the "return" statement in each function with one
  52. or more lines of C code that implements the function. Your code
  53. must conform to the following style:
  54.  
  55. int Funct(arg1, arg2, ...) {
  56. /* brief description of how your implementation works */
  57. int var1 = Expr1;
  58. ...
  59. int varM = ExprM;
  60.  
  61. varJ = ExprJ;
  62. ...
  63. varN = ExprN;
  64. return ExprR;
  65. }
  66.  
  67. Each "Expr" is an expression using ONLY the following:
  68. 1. Integer constants 0 through 255 (0xFF), inclusive. You are
  69. not allowed to use big constants such as 0xffffffff.
  70. 2. Function arguments and local variables (no global variables).
  71. 3. Unary integer operations ! ~
  72. 4. Binary integer operations & ^ | + << >>
  73.  
  74. Some of the problems restrict the set of allowed operators even further.
  75. Each "Expr" may consist of multiple operators. You are not restricted to
  76. one operator per line.
  77.  
  78. You are expressly forbidden to:
  79. 1. Use any control constructs such as if, do, while, for, switch, etc.
  80. 2. Define or use any macros.
  81. 3. Define any additional functions in this file.
  82. 4. Call any functions.
  83. 5. Use any other operations, such as &&, ||, -, or ?:
  84. 6. Use any form of casting.
  85.  
  86. You may assume that your machine:
  87. 1. Uses 2s complement, 32-bit representations of integers.
  88. 2. Performs right shifts arithmetically.
  89. 3. Has unpredictable behavior when shifting an integer by more
  90. than the word size.
  91.  
  92. EXAMPLES OF ACCEPTABLE CODING STYLE:
  93. /*
  94. * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
  95. */
  96. int pow2plus1(int x) {
  97. /* exploit ability of shifts to compute powers of 2 */
  98. return (1 << x) + 1;
  99. }
  100.  
  101. /*
  102. * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
  103. */
  104. int pow2plus4(int x) {
  105. /* exploit ability of shifts to compute powers of 2 */
  106. int result = (1 << x);
  107. result += 4;
  108. return result;
  109. }
  110.  
  111.  
  112. NOTES:
  113. 1. Use the dlc (data lab checker) compiler (described in the handout) to
  114. check the legality of your solutions.
  115. 2. Each function has a maximum number of operators (! ~ & ^ | + << >>)
  116. that you are allowed to use for your implementation of the function.
  117. The max operator count is checked by dlc. Note that '=' is not
  118. counted; you may use as many of these as you want without penalty.
  119. 3. Use the btest test harness to check your functions for correctness.
  120. 4. The maximum number of ops for each function is given in the
  121. header comment for each function. If there are any inconsistencies
  122. between the maximum ops in the writeup and in this file, consider
  123. this file the authoritative source.
  124. #endif
  125.  
  126. /*
  127. * STEP 3: Modify the following functions according the coding rules.
  128. *
  129. * IMPORTANT. TO AVOID GRADING SURPRISES:
  130. * 1. Use the dlc compiler to check that your solutions conform
  131. * to the coding rules.
  132. * 2. Use the btest test harness to check that your solutions produce
  133. * the correct answers. Watch out for corner cases around Tmin and Tmax.
  134. */
  135. /*
  136. * bitNor - ~(x|y) using only ~ and &
  137. * Example: bitNor(0x6, 0x5) = 0xFFFFFFF8
  138. * Legal ops: ~ &
  139. * Max ops: 8
  140. * Rating: 1
  141. */
  142. int bitNor(int x, int y) {
  143. /* to get nor you need to flip the bits of each value and then return the result of and*/
  144. int and = ~x & ~y;
  145.  
  146.  
  147. return and;
  148. }
  149.  
  150. /*
  151. * bitXor - x^y using only ~ and &
  152. * Example: bitXor(4, 5) = 1
  153. * Legal ops: ~ &
  154. * Max ops: 14
  155. * Rating: 2
  156. */
  157. int bitXor(int x, int y) {
  158.  
  159. int first = x&y;
  160. int flip = ~first;
  161.  
  162. int or = ~x&~y;
  163. int or2 = ~or;
  164.  
  165. int result = flip & or2;
  166.  
  167. return result;
  168.  
  169. }
  170. /*
  171. * isNotEqual - return 0 if x == y, and 1 otherwise
  172. * Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1
  173. * Legal ops: ! ~ & ^ | + << >>
  174. * Max ops: 6
  175. * Rating: 2
  176. */
  177. int isNotEqual(int x, int y) {
  178.  
  179. int xor = x^y;
  180. int not1 = !xor;
  181. int notnot1 = !not1;
  182. return notnot1;
  183. }
  184. /*
  185. * getByte - Extract byte n from word x
  186. * Bytes numbered from 0 (LSB) to 3 (MSB)
  187. * Examples: getByte(0x12345678,1) = 0x56
  188. * Legal ops: ! ~ & ^ | + << >>
  189. * Max ops: 6
  190. * Rating: 2
  191. */
  192. int getByte(int x, int n) {
  193. /*shift the bits of n over to the left by three because there are 4 bytes to deal with*/
  194. /*take n to the 2^3 and then shift x by n take the two most signifigant bits of x*/
  195. int temp = n << 3;
  196. x = x >> temp;
  197. return x & 0xff;
  198.  
  199. }
  200. /*
  201. * copyLSB - set all bits of result to least significant bit of x
  202. * Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000
  203. * Legal ops: ! ~ & ^ | + << >>
  204. * Max ops: 5
  205. * Rating: 2
  206. */
  207. int copyLSB(int x) {
  208. /*mask x so you get the least signifigant bit*/
  209.  
  210. return ((x << 31) >> 31);
  211. }
  212. /*
  213. * TMax - return maximum two's complement integer
  214. * Legal ops: ! ~ & ^ | + << >>
  215. * Max ops: 4
  216. * Rating: 1
  217. */
  218. int tmax(void) {
  219. /*the max two's complement integer is 0 followed by 31 1's*/
  220. return 0x7FFFFFFF;
  221. }
  222. /*
  223. * isNonNegative - return 1 if x >= 0, return 0 otherwise
  224. * Example: isNonNegative(-1) = 0. isNonNegative(0) = 1.
  225. * Legal ops: ! ~ & ^ | + << >>
  226. * Max ops: 6
  227. * Rating: 3
  228. */
  229. int isNonNegative(int x) {
  230. // Boolean value indicating sign of x
  231. // 1 = Negative
  232. // 0 = Non-Negative
  233. int sign_x = x >> 31;
  234.  
  235. // The negation of the sign bit of value x computes the appropriate boolean return value.
  236. return (!(sign_x));
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement