/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
z=~(~x|~y);
return z;
}//demorgan 律
/*
* 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 y,z;
y=x>>(8*n);
z=y&0xff;
return z;
}
/* 將要移的兩位挪到最右 用掩碼0xff將其合取出來
* 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
*/spa
int logicalShift(int x, int n) {
int y,z;
y=x>>n;
z=y&((~(0x1<<31)>>n<<1)+1)
return z;
}//向右移n位 保證按照邏輯右移前面補0 將0向左移31位再向右移(n-1)位注意左移時將原數最高位均置零 故還應加一get
1&x爲x 0&x爲0
/*
* bitCount - returns count of number of 1's in word
* Examples: bitCount(5) = 2, bitCount(7) = 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 40
* Rating: 4
*/it
int bitCount(int x) {
int a = (0x55) | (0x55 << 8);
int mask0 = a | (a << 16);
int b = (0x33) | (0x33 << 8);
int mask1 = b | (b << 16);
int c = (0x0F) | (0x0F << 8);
int mask2 = c | (c << 16);
int mask3 = (0xFF) | (0xFF << 16);
int mask4 = (0xFF) | (0xFF << 8);二進制
//unsigned int n = (unsigned int)x;
int n = x;
n = (n & mask0) + (n >> 0x01 & mask0);
n = (n & mask1) + (n >> 0x02 & mask1);
n = (n & mask2) + (n >> 0x04 & mask2);
n = (n & mask3) + (n >> 0x08 & mask3);
n = (n & mask4) + (n >> 0x10 & mask4);im
return n;demo
}//分組相加 先兩兩相加再四位四位相加 再八位八位相加 再十六位十六位相加word
/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/di
int bang(int x)
{
int z;
z=(~((x|(~x+1))>>31))&0x1;//除0外一個數的二進制與其補碼中有一個爲1 括號要寫對
return z;
}co
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/return
int tmin(void)
{
return 1<<31;
}
/*
* fitsBits - return 1 if x can be represented as an
* n-bit, two's complement integer.
* 1 <= n <= 32
* Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int fitsBits(int x, int n)
{
int y,t;
t=32+(~n+1);
y=(x<<t)>>t;
int z=!(x^y);
return z;
}/思路將x左移(32-n)位再右移(32-n)位 獲得的結果與原x相同則代表知足fitsBits
/*
* divpwr2 - Compute x/(2^n), for 0 <= n <= 30
* Round toward zero
* Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int divpwr2(int x, int n) {
int z;
int k;
k=(x>>31)&0x1;
z=(x+((k<<n)+(~k+1)))>>n;
return z;
}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
z=~x+1;
return z;
}
/*
* isPositive - return 1 if x > 0, return 0 otherwise
* Example: isPositive(-1) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 3
*/
int isPositive(int x) {
int z;
z=!(x>>31)&!(!x);
return z;
}//首位爲0且x自己不爲0 是正數
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
int sign,ret1,ret2;
sign = (x^y) >> 31;
ret1 = ((~x+1+y)>>31)&(~sign);//若是x和y同號
ret2 = (y>>31) & (sign);
return !(ret1|ret2);
}//分狀況討論 當x<=y且x y同號時 sign=0 ret1=0 ret2=0 return 1
當y>0且 x y異號時 sign=1 ret2=0 ret1=0 return 1
當x>y且 x y 同號時 ret1=1 return 0
當y<0且 x y異號時 ret2=1 return 0