csapp的datalab配套实验, 要求修改bits.c源文件使所有给定函数满足功能并通过btest的所有测试用例,每个实现函数内均对使用的运算符种类和数量有所限制,可以用dlc程序进行检查。该实验主要为了强化理解整形和浮点型数据的编码形式和位运算相关知识。

实验内容

  • bitXor
    实现异或操作
    /*
    * bitXor - x^y using only ~ and &
    * Example: bitXor(4, 5) = 1
    * Legal ops: ~ &
    * Max ops: 14
    * Rating: 1
    */
    //Tip: 相当于用用非门和与门实现异或门
    int bitXor(int x, int y) {
    //int nand = ~(x&y);
    return ~(x&y)&~(~x&~y);
    }
  • tmin
    返回最小int值
    /*
    * tmin - return minimum two's complement integer
    * Legal ops: ! ~ & ^ | + << >>
    * Max ops: 4
    * Rating: 1
    */
    //Tip: 返回0x8FFFFFFF(-2147483648)即可
    int tmin(void) {
    return 1<<31;
    }
  • isTmax
    返回输入值是否为int最大值
    /*
    * isTmax - returns 1 if x is the maximum, two's complement number,
    * and 0 otherwise
    * Legal ops: ! ~ & ^ | +
    * Max ops: 10
    * Rating: 1
    */
    //Tip: 应用异或位运算符^判断二者是否相等,
    int isTmax(int x) {
    int maximum = ~(1<<31);
    return !(x^maximum);
    }
  • allOddBits
    如果输入值的所有奇数位(按0-31计数)为1则返回1, 否则返回0
    /*
    * allOddBits - return 1 if all odd-numbered bits in word set to 1
    * where bits are numbered from 0 (least significant) to 31 (most significant)
    * Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1
    * Legal ops: ! ~ & ^ | + << >>
    * Max ops: 12
    * Rating: 2
    */
    int allOddBits(int x) {
    int oddByte = 0xAA;
    int oddInt = (oddByte<<24) + (oddByte<<16) + (oddByte<<8) + oddByte; //0xAAAAAAAA
    return !((x&oddInt)^oddInt);
    }
  • negate
    返回输入值的相反数
    /*
    * negate - return -x
    * Example: negate(1) = -1.
    * Legal ops: ! ~ & ^ | + << >>
    * Max ops: 5
    * Rating: 2
    */
    //Tip:取反+1即可
    int negate(int x) {
    return ~x +1;
    }
  • isAsciiDigit
    返回输入是否为数字的ASCII值
    /*
    * 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
    */
    //Tip: x&10000000可以判断是否为负数 X[:3]
    int isAsciiDigit(int x) {
    int TMIN = 1<<31;
    return !((x+ ~0x30+1)&TMIN) & !((0x39+ ~x+1)&TMIN);
    }
  • conditional
    实现三元运算符 ?:
    /*
    * conditional - same as x ? y : z
    * Example: conditional(2,4,5) = 4
    * Legal ops: ! ~ & ^ | + << >>
    * Max ops: 16
    * Rating: 3
    */
    int conditional(int x, int y, int z) {
    int p = ~(!x)+1; //构造x的类布尔表示
    int op = ~p;
    return ((y^p)&op)|((z^op)&p);
    }
  • isLessOrEqual
    实现运算符 <=
    /*
    * isLessOrEqual - if x <= y then return 1, else return 0
    * Example: isLessOrEqual(4,5) = 1.
    * Legal ops: ! ~ & ^ | + << >>
    * Max ops: 24
    * Rating: 3
    */
    //Tip: 仅使用x-y<=0可能会溢出 所以要提取出符号位 同号再计算 异号直接返回结果
    int isLessOrEqual(int x, int y) {
    int signX = (x>>31)&1;
    int signY = (y>>31)&1;
    int YSubX = y+~x+1;
    int TMIN = 1<<31;
    return (signX & !signY) | (!(signX^signY)&!(YSubX & TMIN));
    }
  • 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
    */
    //Tip: 利用非0值和其负值的msb不同 而-0=0
    int logicalNeg(int x) {
    return ((x|(~x+1))>>31)+1;
    }

留坑待补充…