SSD6 Exercise 2: Data Lab(Manipulating Bits)

解题思路:

  1. bitAnd(x,y)
    根据逻辑学基本知识易得

  2. bitOr(x,y)
    根据逻辑学基本知识易得

  3. isZero(x)
    !x操作符只会返回0(x不为0时)或1(x=0时),符合题目要求

  4. minusOne(void)
    -1的补码表示是:0xFFFFFFFF,其对应的取反数为0x00000000,刚好为0,故为~0

  5. tmax(void)
    0x80二进制表示即1000 0000,又因为32位的2的补码表示的最大数为 0111 1111 1111 1111 1111 1111 1111 1111,故将其左移24位(得1000 0000 0000 0000 0000 0000 0000 0000),再取反即满足题意

  6. bitXor(x,y)
    推理过程如下,
    bitXor

  7. getByte(x,n)
    将数据右移n*(2^3)位,即使得其高地址起的24位都为0,再与0xFF进行&运算,即可在最低字节截取所需的数据

  8. isEqual(x,y)
    根据异或的定义,易得正解

  9. negate(x)
    分为x为正数,0,负数三种情况来讨论,
    a.根据2的补码运算的定义,易得正数取反加1即为其相反数;
    b.0000 0000 执行2的补码运算(各位取反+1)得到 1 0000 0000 ,忽略溢位(只有八个位),则得 0000 0000
    c.根据负数在计算机中的表示方法,可知,除了第一位后,其余所有位进行2的补码运算(各位取反+1),即可求其正值,而第一位取反后为0,无影响
    综上,~x+1即满足题意

  10. isPositive(x)
    a.正负之分根据符号位来判断即可,(1<<31)得1000 0000 0000 0000 0000 0000 0000 0000,当x为负数时,out=!((1 << 31) & x)返回0;否则返回1。
    b.再来去除x为0的情况,out^!x返回值的情况如下
    x为正数时,1^0=1;
    x=0,1^1=0;
    x为负数时,0^0=0;
    显然,满足题意


修改bits.c后,程序运行效果如下:
运行结果


bit.c文件部分:

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

}


/*
 * bitOr - x|y using only ~ and &
 *   Example: bitOr(6, 5) = 7
 *   Legal ops: ~ &
 *   Max ops: 8
 *   Rating: 1
 */
int bitOr(int x, int y) {

  return ~((~x)&(~y));

}


/*
 * isZero - returns 1 if x == 0, and 0 otherwise
 *   Examples: isZero(5) = 0, isZero(0) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 2
 *   Rating: 1
 */
int isZero(int x) {

  return !x;
}


/*
 * minusOne - return a value of -1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 2
 *   Rating: 1
 */
int minusOne(void) {

  return ~0;

}


/*
 * TMax - return maximum two's complement integer
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 4
 *   Rating: 1
 */
int tmax(void) {

    int x = 0x80;// 二进制表示即1000 0000
    return(~(x << 24));

}


/*
 * bitXor - x^y using only ~ and &
 *   Example: bitXor(4, 5) = 1
 *   Legal ops: ~ &
 *   Max ops: 14
 *   Rating: 2
 */
int bitXor(int x, int y) {

  int a = ~(x&(~y));
  int b = ~((~x)&y);
  return ~(a&b);

}


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

  return (0xFF & (x >> (n << 3)));

}


/*
 * isEqual - return 1 if x == y, and 0 otherwise
 *   Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int isEqual(int x, int y) {

  return !(x^y);

}


/*
 * negate - return -x
 *   Example: negate(1) = -1.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int negate(int x) {

  return ~x+1;

}


/*
 * 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 out = !((1 << 31) & x);
  int iszero = !x;

  return out ^ iszero;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值