/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal ops: ~ &
* Max ops: 14
* Rating: 1
*/
int bitXor(int x, int y)
{
int a,b;
a=~x&y;
b=x&~y;
a=~(~a&~b);
return a;
}
minusOne
/*
* minusOne - return a value of -1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 2
* Rating: 1
*/
int minusOne(void) {
int a=0;
a=~a;
return a;
}
leastBitPos
/*
* leastBitPos - return a mask that marks the position of the
* least significant 1 bit. If x == 0, return 0
* Example: leastBitPos(96) = 0x20
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int leastBitPos(int x) {
int y=~x;
y=y+0x1;
y=x&y;
return y;
}
allEvenBits
/*
* allEvenBits - return 1 if all even-numbered bits in word set to 1
* Examples allEvenBits(0xFFFFFFFE) = 0, allEvenBits(0x55555555) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 2
*/
int allEvenBits(int x) {
int a=0x55;
a+=a<<8;
a+=a<<16;
a=a+~(a&x)+1;
return !a;
}
byteSwap
/*
* byteSwap - swaps the nth byte and the mth byte
* Examples: byteSwap(0x12345678, 1, 3) = 0x56341278
* byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD
* You may assume that 0 <= n <= 3, 0 <= m <= 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 25
* Rating: 2
*/
int byteSwap(int x, int n, int m) {
int a=n<<3;
int b=m<<3;
int c=((((0xff<>a)&0xff)<>b)&0xff)<
isAsciiDigit
/*
* isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')
* Example: isAsciiDigit(0x35) = 1.
* isAsciiDigit(0x3a) = 0.
* isAsciiDigit(0x05) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 3
*/
int isAsciiDigit(int x) {
return (!((x+~48+1)>>31))&!!((x+~58+1)>>31);
}
satMul2
/*
* satMul2 - multiplies by 2, saturating to Tmin or Tmax if overflow
* Examples: satMul2(0x30000000) = 0x60000000
* satMul2(0x40000000) = 0x7FFFFFFF (saturate to TMax)
* satMul2(0x60000000) = 0x80000000 (saturate to TMin)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int satMul2(int x) {
int a=x<<1;
int b=x>>31;
int c=(x^a)>>31;
int tmin=1<<31;
return ((~c)&a)+(c&(~b+tmin));
}
subOK
/*
* subOK - Determine if can compute x-y without overflow
* Example: subOK(0x80000000,0x80000000) = 1,
* subOK(0x80000000,0x70000000) = 0,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int subOK(int x, int y) {
int a=x>>31;
int b=y>>31;
int z=x+(~y)+1;
int c=z>>31;
return !((a^b)&(~(b^c))&(a^c));
}
isLess
/*
* isLess - if x < y then return 1, else return 0
* Example: isLess(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLess(int x, int y) {
int a=((x>>31)^(y>>31))&0x1;
int z=x+(~y)+1;
return (a&(x>>31))|((!(~(z>>31)))&!a);
}
bitMask
/*
* bitMask - Generate a mask consisting of all 1's
* lowbit and highbit
* Examples: bitMask(5,3) = 0x38
* Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31
* If lowbit > highbit, then mask should be all 0's
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/
int bitMask(int highbit, int lowbit) {
int i=~0;
int a=(i<>31));
}
leftBitCount 这个写了好久,然后我推了好多次,总算好了,感觉自己很棒棒啦。
/*
* leftBitCount - returns count of number of consective 1's in
* left-hand (most significant) end of word.
* Examples: leftBitCount(-1) = 32, leftBitCount(0xFFF0F0F0) = 12
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 50
* Rating: 4
*/
int leftBitCount(int x) {
int count;
int tmpMask1 = (0x55)|(0x55<<8);
int mask1 = (tmpMask1)|(tmpMask1<<16);
int tmpMask2 = (0x33)|(0x33<<8);
int mask2 = (tmpMask2)|(tmpMask2<<16);
int tmpMask3 = (0x0f)|(0x0f<<8);
int mask3 = (tmpMask3)|(tmpMask3<<16);
int mask4 = (0xff)|(0xff<<16);
int mask5 = (0xff)|(0xff<<8);
x=~x;
x=((x)>>1)|x;
x=((x)>>2)|x;
x=((x)>>4)|x;
x=((x)>>8)|x;
x=((x)>>16)|x;
count = (x&mask1)+((x>>1)&mask1);
count = (count&mask2)+((count>>2)&mask2);
count = (count + (count >> 4)) & mask3;
count = (count + (count >> 8)) & mask4;
count = (count + (count >> 16)) & mask5;
return 32+~count+1;
}
logicalNeg
/*
* logicalNeg - implement the ! operator, using all of
* the legal operators except !
* Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int logicalNeg(int x) {
return ~(((~x+1)|x)>>31)&1;
}
float_abs
/*
* float_abs - Return bit-level equivalent of absolute value of 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_abs(unsigned uf) {
unsigned a=~(0x80<<24);
int b=(0xff<<23)+1;
unsigned c=uf&a;
if(((c+(~b+1))>>31)&0x1)
return c;
else
return uf;
}
float_f2i
/*
* float_f2i - 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
*/
int float_f2i(unsigned uf) {
int a=0x80<<24;
int b=(((0xff<<8)+0xff)<<7)+0x7f;
int exp=(uf>>23)&0xFF;
int frac=uf&b;
int sign=uf&a;
int bias=exp-127;
if (exp==255||bias>30)
return a;
else if(!exp||(bias<0))
return 0;
frac=frac|(1<<23);
if (bias>23)
frac=frac<<(bias-23);
else
frac=frac>>(23-bias);
if (sign)
frac=~(frac)+1;
return frac;
}
float_half
/*
* float_half - Return bit-level equivalent of expression 0.5*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_half(unsigned uf) {
int a=0xff<<23;
int b=!((uf&3)^3);
int sign=uf&(0x80<<24);
int exp=uf&a;
int frac=(uf&((((0xff<<8)+0xff)<<7)+0x7f));
if(exp==a)
return uf;
if(exp==(0x80<<16))
return sign|(b+((uf&(~(0x80<<24)))>>1));
if(exp==0x0)
return sign|((frac>>1)+b);
return (((exp>>23)-1)<<23)|sign|frac;
}
通过