Each "Expr" is an expression using ONLY the following: 1. Integer constants 0 through 255 (0xFF), inclusive. You are not allowed to use big constants such as 0xffffffff. 2. Function arguments and local variables (no global variables). 3. Unary integer operations ! ~ 4. Binary integer operations & ^ | + << >> Some of the problems restrict the set of allowed operators even further. Each "Expr" may consist of multiple operators. You are not restricted to one operator per line.
You are expressly forbidden to: 1. Use any control constructs such as if, do, while, for, switch, etc. 2. Define or use any macros. 3. Define any additional functions in this file. 4. Call any functions. 5. Use any other operations, such as &&, ||, -, or ?: 6. Use any form of casting. 7. Use any data type other than int. This implies that you cannot use arrays, structs, or unions.
bitXor
1 2 3 4 5 6 7
/* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Legal ops: ~ & * Max ops: 14 * Rating: 1 */
/* * allOddBits - return 1 if all odd-numbered bits in word set to 1 * where bits are numbered from 0 (least significant) to 31 (most significant) * Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 12 * Rating: 2 */
inthowManyBits(int x){ int s16,s8,s4,s2,s1,s; int t1 = ((!x)<<31)>>31; int t2 = ((!~x)<<31)>>31; int op = x^(x>>31); s16 = (!!(op>>16))<<4; op = op>>s16; s8 = (!!(op>>8))<<3; op = op>>s8; s4 = (!!(op>>4))<<2; op = op>>s4; s2 = (!!(op>>2))<<1; op = op>>s2; s1 = (!!(op>>1)); op = op>>s1; s = 2 + s16 + s8 + s4 + s2 + s1; return (t2&1)|((~t2)&((t1&1)|((~t1)&s))); return0; }
FLOAT PART
考察浮点型,条件有放宽:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
For the problems that require you to implement floating-point operations, the coding rules are less strict. You are allowed to use looping and conditional control. You are allowed to use both ints and unsigneds. You can use arbitrary integer and unsigned constants. You can use any arithmetic, logical, or comparison operations on int or unsigned data.
You are expressly forbidden to: 1. Define or use any macros. 2. Define any additional functions in this file. 3. Call any functions. 4. Use any form of casting. 5. Use any data type other than int or unsigned. This means that you cannot use arrays, structs, or unions. 6. Use any floating point data types, operations, or constants.
floatFloat2Int
1 2 3 4 5 6 7 8 9 10 11 12
/* * 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 */
分类讨论即可。细节比较多。
1 2 3 4 5 6 7 8 9 10 11 12
intfloatFloat2Int(unsigned uf){ unsigned s = uf & 0x80000000; unsigned e = uf & 0x7f800000; unsigned f = (uf & 0x7fffff) | 0x800000; unsigned ee = e >> 23; if(ee <= 126) return0; if(ee > 157) return0x80000000u; if(ee >= 150) f <<= (ee - 150); else f >>= (150 - ee); if(s) return -f; return f; }
floatPower2
1 2 3 4 5 6 7 8 9 10 11 12 13
/* * 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 */