/* * isTmax - returns 1 if x is the maximum, two's complement number, * and 0 otherwise * Legal ops: ! ~ & ^ | + * Max ops: 10 * Rating: 1 */ intisTmax(int x){ int result = x + x + 1; result = ~result; result = result + !(x + 1) //排除-1特殊 return !result; }
/* * conditional - same as x ? y : z * Example: conditional(2,4,5) = 4 * Legal ops: ! ~ & ^ | + << >> * Max ops: 16 * Rating: 3 */ intconditional(int x, int y, int z){ x = !x; x = ~x + 1; int result = (~x&y)|(x&z) return result; }
/* howManyBits - return the minimum number of bits required to represent x in * two's complement * Examples: howManyBits(12) = 5 * howManyBits(298) = 10 * howManyBits(-5) = 4 * howManyBits(0) = 1 * howManyBits(-1) = 1 * howManyBits(0x80000000) = 32 * Legal ops: ! ~ & ^ | + << >> * Max ops: 90 * Rating: 4 */ inthowManyBits(int x){ x = x^(x>>31); //得到负数补码,正数不变 int A,B,C,D,E,F; A = !!(x>>16)<<4; //看最高16位有无1,如果有,A为16; x = x>>A; //如果上面成立,则消掉后面16位(后面的数字不讨论,折半讨论有1的区域) B = !!(x>>8)<<3; //看原数据最高8位有无1,有则B为8; x = x>>B; //成立则消掉后面8位(相当于总地消掉原数据后面24位) C = !!(x>>4)<<2; //同理,直到遍历到1的准确位置,之后把记的数字加起来,就是需要表示的最小数量 x = x>>C; D = !!(x>>2)<<1; x = x>>D; E = !!(x>>1); x = x>>E; F = x; int result = A + B + C + D + E + F + 1; //+1表示符号位 return result; }
/* * floatScale2 - Return bit-level equivalent of expression 2*f for * floating point argument f. * Both the argument and result are passed as unsigned int's, but * they are to be interpreted as the bit-level representation of * single-precision floating point values. * When argument is NaN, return argument * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ unsignedfloatScale2(unsigned uf){ intexp = (uf&0x7F800000)>>23; //求指数 if(exp==0) //无穷小和0的情况,返回乘2的值 return uf<<1|(!!(uf>>31)); if(exp==255) //无穷大和NaN的情况,返回原值(它们乘2等于本身) return uf; exp = exp + 1; if(exp==255) //剩下的情况则是如果指数+1为255,那就返回原符号无穷大,否则返回指数加1后的原符号数 return0x7F800000|(!!(uf>>31)); else result = (exp<<23)|(uf&0x807FFFFF); return result; }
/* * floatFloat2Int - Return bit-level equivalent of expression (int) f * for floating point argument f. * Argument is passed as unsigned int, but * it is to be interpreted as the bit-level representation of a * single-precision floating point value. * Anything out of range (including NaN and infinity) should return * 0x80000000u. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ intfloatFloat2Int(unsigned uf){ intexp = (uf&0x7F800000)>>23; int frac = (uf&0x007FFFFF)|0x00800000; //或上0x00800000是为了加上省掉的1 if((exp<127)||(!uf)) return0; //特殊情况 if(exp>158) return0x80000000u; //指数大于31+127 if(exp>150) frac <<= (exp-150); //把小数转换成整数 else frac >>= (150-exp); if(!((frac>>31)^(uf>>31))) return frac; //符号相同 elseif(frac>>31) return0x80000000u; //原符号为正溢出 elsereturn ~frac+1; //原负数情况 }
先想想溢出的情况:指数大于了31+127,或者原来的符号是正,变换后变成了负;
另一种特殊情况是:原来的数字是小数(也就是指数小于0+127)或者0,则直接返回0;
原来的符号是负,变换后为正,就求一次补码;
其他的就是正常返回变换过后的值就行了;
13.floatPower2(x)
求浮点2的x次幂;
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* * floatPower2 - Return bit-level equivalent of the expression 2.0^x * (2.0 raised to the power x) for any 32-bit integer x. * * The unsigned value that is returned should have the identical bit * representation as the single-precision floating-point number 2.0^x. * If the result is too small to be represented as a denorm, return * 0. If too large, return +INF. * * Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while * Max ops: 30 * Rating: 4 */ unsignedfloatPower2(int x){ intexp = x + 127; if(exp<=0) return0; if(exp>=255) return0xFF<<23; returnexp<<23; }