深入理解计算机系统作业

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

 

 

 

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

 

转载于:https://www.cnblogs.com/wwqdata/p/11517592.html

  • 0
    点赞
  • 1
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值