系统编程实验lab2--位操作 bit manipulate

没有那个限制!!  

直接上代码:

  

  1 /* 
  2  * bits.c - Source file with your solutions to the Lab.
  3  *          This is the file you will hand in to your instructor.
  4  */
  5 
  6 #include "btest.h"
  7 #include <limits.h>
  8 
  9 /*
 10  * Instructions to Students:
 11  *
 12  * STEP 1: Fill in the following struct with your identifying info.
 13  */
 14 info_struct info =
 15 {
 16    /* Replace with your full name */
 17    "huangmenglin",
 18    /* Replace with your login ID */
 19    "2016141463199",
 20 };
 21 
 22 #if 0
 23 /*
 24  * STEP 2: Read the following instructions carefully.
 25  */
 26 
 27 You will provide your solution to the Data Lab by
 28 editing the collection of functions in this source file.
 29 
 30 CODING RULES:
 31  
 32   Replace the "return" statement in each function with one
 33   or more lines of C code that implements the function. Your code 
 34   must conform to the following style:
 35  
 36   int Funct(arg1, arg2, ...) {
 37       /* brief description of how your implementation works */
 38       int var1 = Expr1;
 39       ...
 40       int varM = ExprM;
 41 
 42       varJ = ExprJ;
 43       ...
 44       varN = ExprN;
 45       return ExprR;
 46   }
 47 
 48   Each "Expr" is an expression using ONLY the following:
 49   1. Integer constants 0 through 255 (0xFF), inclusive. 
 50      ***You are not allowed to use big constants such as 0xffffffff***
 51   2. Function arguments and local variables (no global variables).
 52   3. Unary integer operations ! ~
 53   4. Binary integer operations & ^ | + << >>
 54     
 55   Some of the problems restrict the set of allowed operators even further.
 56   Each "Expr" may consist of multiple operators. You are not restricted to
 57   one operator per line.
 58 
 59   You are expressly forbidden to:
 60   1. Use any control constructs such as if, do, while, for, switch, etc.
 61   2. Define or use any macros.
 62   3. Define any additional functions in this file.
 63   4. Call any functions.
 64   5. Use any other operations, such as &&, ||, -, ?, or []:
 65   6. Use any form of casting.
 66  
 67   You may assume that your machine:
 68   1. Uses 2s complement, 32-bit representations of integers.
 69   2. Performs right shifts arithmetically.
 70   3. Has unpredictable behavior when shifting an integer by more
 71      than the word size.
 72 
 73 EXAMPLES OF ACCEPTABLE CODING STYLE:
 74   /*
 75    * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
 76    */
 77   int pow2plus1(int x) {
 78      /* exploit ability of shifts to compute powers of 2 */
 79      return (1 << x) + 1;
 80   }
 81 
 82   /*
 83    * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
 84    */
 85   int pow2plus4(int x) {
 86      /* exploit ability of shifts to compute powers of 2 */
 87      int result = (1 << x);
 88      result += 4;
 89      return result;
 90   }
 91 
 92 
 93 NOTES AND HINTS:
 94   1. Each function has a maximum number of operators (! ~ & ^ | + << >>)
 95      that you are allowed to use for your implementation of the function. 
 96      The max operator count will be checked by your instructor. 
 97      Note that '=' is not counted; you may use as many of these as you 
 98      want without penalty.
 99   2. Use the btest test harness to check your functions for correctness.
100 #endif
101 
102 /*
103  * STEP 3: Modify the following functions according the coding rules.
104  */
105 
106 
107 
108 /* 
109  * bitAnd - x&y using only ~ and | 
110  *   Example: bitAnd(6, 5) = 4
111  *   Legal ops: ~ |
112  *   Max ops: 8
113  *   Rating: 1
114  */
115 int bitAnd(int x, int y) {
116 
117     int x1 = ~x;
118     int y1 = ~y;
119     int res = ~(x1|y1);
120     return res;
121 
122 }
123 
124 
125 
126 
127 
128 
129 /* 
130  * bitOr - x|y using only ~ and & 
131  *   Example: bitOr(6, 5) = 7
132  *   Legal ops: ~ &
133  *   Max ops: 8
134  *   Rating: 1
135  */
136 int bitOr(int x, int y) {
137     int x1 = ~x;
138     int y1 = ~y;
139     int res = ~(x1&y1);
140     return res;
141 }
142 
143 
144 
145 
146 
147 
148 /*
149  * isZero - returns 1 if x == 0, and 0 otherwise 
150  *   Examples: isZero(5) = 0, isZero(0) = 1
151  *   Legal ops: ! ~ & ^ | + << >>
152  *   Max ops: 2
153  *   Rating: 1
154  */
155 int isZero(int x) {
156   return !x;    
157 
158 }
159 
160 
161 
162 
163 
164 
165 /* 
166  * minusOne - return a value of -1 
167  *   Legal ops: ! ~ & ^ | + << >>
168  *   Max ops: 2
169  *   Rating: 1
170  */
171 int minusOne(void) {
172     char c=255;
173     return c;
174 }
175 
176 
177 
178 
179 
180 
181 /* 
182  * TMax - return maximum two's complement integer 
183  *   Legal ops: ! ~ & ^ | + << >>
184  *   Max ops: 4
185  *   Rating: 1   
186  */
187 int tmax(void) {
188     int a = 1;
189     int b = minusOne();
190 
191     return b^(a<<31);
192 }
193 
194 
195 
196 
197 
198 
199 
200 /* 
201  * bitXor - x^y using only ~ and & 
202  *   Example: bitXor(4, 5) = 1
203  *   Legal ops: ~ &
204  *   Max ops: 14
205  *   Rating: 2
206  */
207 int bitXor(int x, int y) {
208 
209   int a = ~x;
210   int b = ~y;
211   return (a&y)|(x&b);
212 
213 }
214 
215 
216 
217 
218 
219 
220 /* 
221  * getByte - Extract byte n from word x
222  *   Bytes numbered from 0 (LSB) to 3 (MSB)
223  *   Examples: getByte(0x12345678,1) = 0x56
224  *   Legal ops: ! ~ & ^ | + << >>
225  *   Max ops: 6
226  *   Rating: 2
227  */
228 int getByte(int x, int n) {
229     int a = 255;
230     x = x>>(n*8);
231     return a&x;
232 }
233 
234 
235 
236 
237 
238 
239 /* 
240  * isEqual - return 1 if x == y, and 0 otherwise 
241  *   Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
242  *   Legal ops: ! ~ & ^ | + << >>
243  *   Max ops: 5
244  *   Rating: 2
245  */
246 int isEqual(int x, int y) {
247 
248   return !(x^y);
249 
250 }
251 
252 
253 
254 
255 
256 
257 /* 
258  * negate - return -x 
259  *   Example: negate(1) = -1.
260  *   Legal ops: ! ~ & ^ | + << >>
261  *   Max ops: 5
262  *   Rating: 2
263  */
264 int negate(int x) {
265     int res = (~x)+1;
266     return res;
267 }
268 
269 
270 
271 
272 /* 
273  * isPositive - return 1 if x > 0, return 0 otherwise 
274  *   Example: isPositive(-1) = 0.
275  *   Legal ops: ! ~ & ^ | + << >>
276  *   Max ops: 8
277  *   Rating: 3
278  */
279 int isPositive(int x) {
280    
281     return !((!x)|(x>>31));
282 }


posted @ 2018-09-16 10:48  学习丶笔记  Views(592)  Comments(0Edit  收藏  举报