深入理解计算机系统作业

/*
* 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
*/

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)位注意左移时将原数最高位均置零 故还应加一

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
*/

 

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);

return n;


}//分组相加 先两两相加再四位四位相加 再八位八位相加 再十六位十六位相加

/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/

int bang(int x)
{
int z;
z=(~((x|(~x+1))>>31))&0x1;//除0外一个数的二进制与其补码中有一个为1 括号要写对
return z;
}

 

/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/

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

 

posted @ 2019-09-13 20:37  柠檬味呀  阅读(1016)  评论(0编辑  收藏  举报