datalab - csapp exp1
之前上课做过一次,这次在官网上下下来,好像有点区别,不过大体差不多。
环境是ubuntu32
先修改bits.c文件,按要求实现内部函数
命令行执行:
make btest
./btest
然后会看到对你修改后的函数的评判。
具体函数:
1 //1 2 /* 3 * bitXor - x^y using only ~ and & 4 * Example: bitXor(4, 5) = 1 5 * Legal ops: ~ & 6 * Max ops: 14 7 * Rating: 1 8 */ 9 int bitXor(int x, int y) { 10 return ~(~(x&(~y)) & ~((~x)&y)); 11 } 12 /* 13 * tmin - return minimum two's complement integer 14 * Legal ops: ! ~ & ^ | + << >> 15 * Max ops: 4 16 * Rating: 1 17 */ 18 int tmin(void) { 19 return 0x1<<31; 20 } 21 //2 22 /* 23 * isTmax - returns 1 if x is the maximum, two's complement number, 24 * and 0 otherwise 25 * Legal ops: ! ~ & ^ | + 26 * Max ops: 10 27 * Rating: 1 28 */ 29 int isTmax(int x) { 30 return !(x^(tmin()-1)); 31 } 32 /* 33 * allOddBits - return 1 if all odd-numbered bits in word set to 1 34 * where bits are numbered from 0 (least significant) to 31 (most significant) 35 * Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1 36 * Legal ops: ! ~ & ^ | + << >> 37 * Max ops: 12 38 * Rating: 2 39 */ 40 int allOddBits(int x) { 41 int q = 0xaaaaaaaa; 42 return !((x & q) ^ q); 43 } 44 /* 45 * negate - return -x 46 * Example: negate(1) = -1. 47 * Legal ops: ! ~ & ^ | + << >> 48 * Max ops: 5 49 * Rating: 2 50 */ 51 int negate(int x) { 52 return (~x)+1; 53 } 54 //3 55 /* 56 * isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9') 57 * Example: isAsciiDigit(0x35) = 1. 58 * isAsciiDigit(0x3a) = 0. 59 * isAsciiDigit(0x05) = 0. 60 * Legal ops: ! ~ & ^ | + << >> 61 * Max ops: 15 62 * Rating: 3 63 */ 64 int isAsciiDigit(int x) { 65 int r = 0x3a; 66 int l = 0x2f; 67 int flag1 = ~(!(x >> 6))+1; 68 int res2 = x + (~r + 1); 69 int flag2 = (res2 >> 31) & 1; 70 int res3 = l + (~x+1); 71 int flag3 = (res3 >> 31) & 1; 72 return (flag1 & flag2 & flag3); 73 } 74 /* 75 * conditional - same as x ? y : z 76 * Example: conditional(2,4,5) = 4 77 * Legal ops: ! ~ & ^ | + << >> 78 * Max ops: 16 79 * Rating: 3 80 */ 81 int conditional(int x, int y, int z) { 82 x = ~(!!x)+1; 83 return (x & y) | (~x & z); 84 } 85 /* 86 * isLessOrEqual - if x <= y then return 1, else return 0 87 * Example: isLessOrEqual(4,5) = 1. 88 * Legal ops: ! ~ & ^ | + << >> 89 * Max ops: 24 90 * Rating: 3 91 */ 92 int isLessOrEqual(int x, int y) { 93 int res = x + (~y+1); 94 int flag1 = ((x&(~y)) | (~(x^y)&res)) >> 31 & 1;//1.x<0 y>=0 2.x*y>0 x-y<0 95 int flag2 = !(x^y);//if equal 96 return (flag1 | flag2); 97 } 98 //4 99 /* 100 * logicalNeg - implement the ! operator, using all of 101 * the legal operators except ! 102 * Examples: logicalNeg(3) = 0, logicalNeg(0) = 1 103 * Legal ops: ~ & ^ | + << >> 104 * Max ops: 12 105 * Rating: 4 106 */ 107 int logicalNeg(int x) { 108 return (~x&~(~x+1)) >> 31&0x1; 109 } 110 /* howManyBits - return the minimum number of bits required to represent x in 111 * two's complement 112 * Examples: howManyBits(12) = 5 113 * howManyBits(298) = 10 114 * howManyBits(-5) = 4 115 * howManyBits(0) = 1 116 * howManyBits(-1) = 1 117 * howManyBits(0x80000000) = 32 118 * Legal ops: ! ~ & ^ | + << >> 119 * Max ops: 90 120 * Rating: 4 121 */ 122 int howManyBits(int x) { 123 int t = x ^ (x>>31); 124 int isZero = !t; 125 int notZeroMask = (!(!t)<<31) >> 31; 126 int bit_16,bit_8,bit_4,bit_2,bit_1; 127 bit_16 = !!(t>>16) << 4; 128 t = t >> bit_16; 129 bit_8 = !!(t>>8) << 3; 130 t = t >> bit_8; 131 bit_4 = !!(t>>4) << 2; 132 t = t>>bit_4; 133 bit_2 = !!(t>>2) << 1; 134 t = t >> bit_2; 135 bit_1 = !!(t>>1); 136 t = bit_16 + bit_8 + bit_4 + bit_2 + bit_1 + 2; 137 return isZero|(t¬ZeroMask); 138 } 139 //float 140 /* 141 * floatScale2 - Return bit-level equivalent of expression 2*f for 142 * floating point argument f. 143 * Both the argument and result are passed as unsigned int's, but 144 * they are to be interpreted as the bit-level representation of 145 * single-precision floating point values. 146 * When argument is NaN, return argument 147 * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while 148 * Max ops: 30 149 * Rating: 4 150 */ 151 unsigned floatScale2(unsigned uf) { 152 int round,S,E,maskE,maskM,maskS,maskEM,maskSM,tmp; 153 round = !((uf&3)^3); 154 maskS = 0x80000000; 155 maskE = 0x7F800000; 156 maskM = 0x007FFFFF; 157 maskEM = 0x7FFFFFFF; 158 maskSM = 0x807FFFFF; 159 E = uf&maskE; 160 S = uf&maskS; 161 if(E == 0x7F800000) 162 return uf; 163 if(E == 0x00800000) 164 return S | (round + ((uf&maskEM)>>1)); 165 if(E == 0x00000000){ 166 tmp = (uf&maskM) >> 1; 167 return S | (tmp+round); 168 } 169 return (((E>>23)-1)<<23)|(uf&maskSM); 170 } 171 /* 172 * floatFloat2Int - Return bit-level equivalent of expression (int) f 173 * for floating point argument f. 174 * Argument is passed as unsigned int, but 175 * it is to be interpreted as the bit-level representation of a 176 * single-precision floating point value. 177 * Anything out of range (including NaN and infinity) should return 178 * 0x80000000u. 179 * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while 180 * Max ops: 30 181 * Rating: 4 182 */ 183 int floatFloat2Int(unsigned uf) { 184 return 2; 185 } 186 /* 187 * floatPower2 - Return bit-level equivalent of the expression 2.0^x 188 * (2.0 raised to the power x) for any 32-bit integer x. 189 * 190 * The unsigned value that is returned should have the identical bit 191 * representation as the single-precision floating-point number 2.0^x. 192 * If the result is too small to be represented as a denorm, return 193 * 0. If too large, return +INF. 194 * 195 * Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while 196 * Max ops: 30 197 * Rating: 4 198 */ 199 unsigned floatPower2(int x) { 200 return 2; 201 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步