/* * 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)); }
getByte
1 2 3 4 5 6 7 8 9 10 11 12
/* * 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 */ int getByte(int x, int n) { int mask = 0xff; return (x>>(n<<3)&mask); }
位!=字节,4位一个字节
logicalShift
1 2 3 4 5 6 7 8 9 10 11 12 13
/* * logicalShift - shift x to the right by n, using a logical shift * Can assume that 0 <= n <= 31 * Examples: logicalShift(0x87654321,4) = 0x08765432 * Legal ops: ! ~ & ^ | + << >> * Max ops: 20 * Rating: 3 */
int logicalShift(int x, int n) { int mask=((0x1<<(32+~n))+~0)|(0x1<<(32+~n)); return (x>>n)&mask; }
/* * ilog2 - return floor(log base 2 of x), where x > 0 * Example: ilog2(16) = 4 * Legal ops: ! ~ & ^ | + << >> * Max ops: 90 * Rating: 4 */ int ilog2(int x) { int res = 0; res = (!!(x>>(16)))<<4; res = res+((!!(x>>(8+res)))<<3); res = res+((!!(x>>(4+res)))<<2); res = res+((!!(x>>(2+res)))<<1); res = res+((!!(x>>(1+res)))<<0); return res; }
/* * float_neg - Return bit-level equivalent of expression -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 representations of * single-precision floating point values. * When argument is NaN, return argument. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 10 * Rating: 2 */ unsigned float_neg(unsigned uf) { int c = 0x00ffffff; if ((~(uf << 1)) < c){ return uf; } else{ return uf ^ (0x80000000); } }
/* * float_i2f - Return bit-level equivalent of expression (float) x * Result is returned as unsigned int, but * it is to be interpreted as the bit-level representation of a * single-precision floating point values. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ unsigned float_i2f(int x) { int sign = (x>>31)&0x1; int exp=0,frac=0,delta=0; int frac_mask = 0x7fffff; if (!x){ return x; } else if(x==0x80000000){ exp = 158; } else{ if(sign) x = -x; int index = 30; while(!(x>>index)){ index--; } exp = index+127; x = x<<(31-index); frac = (x>>8)&frac_mask; x = x&0xff; delta = x>0x80 || ((x==0x80)&&(frac&0x1)); frac+=delta; if(frac>>23){ exp+=1; frac = frac&frac_mask; } } return (sign<<31)|(exp<<23)|frac; }
/* * float_twice - 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 */ unsigned float_twice(unsigned uf) { int sign = uf>>31&&0x01; int exp = uf>>23 & 0xff; int frac = uf & 0x7fffff; if(exp!=0xff){ if(!exp) frac=frac<<1; else{ exp += 1; if(exp==0xff) frac=0; } } return sign<<31|exp<<23|frac; }