/* * 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 */ #include<stdio.h> intfloatFloat2Int(unsigned uf) { intexp = (uf >> 23) & 0xFF; // 00...0 exp unsigned frac = (uf << 9) >> 9; // 00...0 frac
/* * 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) { /* float 可以表示的 2的幂次的范围为 [2^(-149), 2^(127)], * 其中 [2^(-149), 2^(-127)] 由 denormalized case 表示, * [2^(-126), 2^(127)] 由 normalized case 表示 */ if (x < -149) { return0; } // denormalized case elseif (x <= -127) { return1 << (x + 149); // 只处理 frac 部分 } // normalized case elseif (x <= 127) { return (x + 127) << 23; // 只处理 exp 部分 } else { return (0xFF << 23); // +INF } }