这些是我们《系统级编程》课程的第一次作业。就是用指定的运算符实现某些函数功能。 /* * 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 ~(~x|~y); } /* * bitOr - x|y using only ~ and & * Example: bitOr(6, 5) = 7 * Legal ops: ~ & * Max ops: 8 * Rating: 1 */ int bitOr(int x, int y) { return ~(~x&~y); } /* * isZero - returns 1 if x == 0, and 0 otherwise * Examples: isZero(5) = 0, isZero(0) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 2 * Rating: 1 */ int isZero(int x) { return !x; } /* * minusOne - return a value of -1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 2 * Rating: 1 */ int minusOne(void) { return ~0; } /* * TMax - return maximum two's complement integer * Legal ops: ! ~ & ^ | + << >> * Max ops: 4 * Rating: 1 */ int tmax(void) { return (~0)^(1<<31); } /* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Legal ops: ~ & * Max ops: 14 * Rating: 2 */ int bitXor(int x, int y) { return ~(x&y)&(x|y); } /* * 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 (x&(0xff<<t))>>t&0xff */ int getByte(int x, int n) { return (x>>(n<<3))&0xff; } /* * isEqual - return 1 if x == y, and 0 otherwise * Examples: isEqual(5,5) = 1, isEqual(4,5) = 0 * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */ int isEqual(int x, int y) { return !(x^y); } /* * negate - return -x * Example: negate(1) = -1. * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */ int negate(int x) { return ~x+1; } /* * isPositive - return 1 if x > 0, return 0 otherwise * Example: isPositive(-1) = 0. * Legal ops: ! ~ & ^ | + << >> * Max ops: 8 * Rating: 3 */ int isPositive(int x) { return !(x&(1<<31))^!x; } /* * tmin - return minimum two's complement integer * Legal ops: ! ~ & ^ | + << >> * Max ops: 4 * Rating: 1 */ int tmin(void) { return 1<<31; } /* * minusFive - return a value of -5 * Legal ops: ! ~ & ^ | + << >> * Max ops: 4 * Rating: 1 */ int minusFive(void) { return ~5+1; } /* * copyLSB - set all bits of result to least significant bit of x * Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000 * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */ int copyLSB(int x) { return x<<31>>31; } /* * getBit - Extract bit n from word x * Bits numbered from 0 (LSB) to 31 (MSB) * Examples: * getBit(0x12345678, 0) = 0 * getBit(0x12345678, 3) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 4 * Rating: 2 */ int getBit(int x, int n) { return x>>n&1; }