csapp datalab1实验记录
lab1试验记录
具体的实验思路已经写到每一个饰演的注释前面了,可以参考,已经通过测试。后续会整理细节的笔记发送出来

//1
/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal ops: ~ &
* Max ops: 14
* Rating: 1
*/
int bitXor(int x, int y) {
return ~(~x&~y)&~(x&y);
}
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
* solution: 32bits : 0x 8000
*/
int tmin(void) {
return 0x01<<31;
}
//2
/*
* isTmax - returns 1 if x is the maximum, two's complement number,
* and 0 otherwise
* Legal ops: ! ~ & ^ | +
* Max ops: 10
* Rating: 1
* solution: max 32bits : 7FFF
*/
int isTmax(int x) {
int y = 0x7FFFFFFF;
return !(x^y);
}
/*
* 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
* 0xAAAAAAAA相当于一个mask把奇数位都提取出来 再和mask异或,如果相同就是奇数位为1
*/
int allOddBits(int x) {
return !((x & 0xAAAAAAAA )^0xAAAAAAAA) ;
}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
return ~x+1;
}
//3
/*
* 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
* 移位运算,左移是逻辑移动,符号位会被舍弃。右移则对无符号数用0填充
* 有符号数用1补充
*/
int isAsciiDigit(int x) {
int a,b;
a=x-0x30; //是Asic应该为sign=0
b=0x39-x; //是Asic应该为sign=0
return !((a>>31)|(b>>31)); //只把符号位提取出来,去或,有1则为1,表明超出了范围。
}
/*
* conditional - same as x ? y : z
* Example: conditional(2,4,5) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
* 和数字电路不同 x≠0 -->y 否则 x=0 -->z
* 先把x转化为bool逻辑值,如果x不为0,x的bool值就是1,然后还要把bool值转化为
* 我理解的数值值,0---0,1---FFFFFFFF
* ~ !的区别,~是按位取反,!是逻辑取反。
* 我最开始的思路是int sign = x^0; 异或提取,但是这样是不对的,异或对每一位都进行了操作
*/
int conditional(int x, int y, int z) {
x = !!x;
x = ~x+1;
return (x&y)|(~x&z);
// int sign = x^0;//x=0 -->0 否则-->1
// sign = ~sign+1;
// return (~sign&z)|(sign&y);
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
* 最开始的思路只是用x-y判断符号位,这样是错误的,如果xy异号,那么很可能输出的值就会溢出
* 所以这里通过判断xy符号的方法
*/
int isLessOrEqual(int x, int y) {
int signx = (x >> 31) & 1;
int signy = (y >> 31) & 1;
int flag1 = signx & (!signy);
int e = signx ^ signy; //同号
int flag2 = ((!e) & ((x + ~y) >> 31) & 1);
return flag1 | flag2;
}
//4
/*
* 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
* 这道题需要求出逻辑反,我认为基本舍弃了,通过逻辑运算,异或等等来完成,因为这些都只能按位算
* 没办法实现逻辑上的操作。
* 对于逻辑为0的数而言,只有0本身。所以从0这个数找一些特点
* 0的补码依然为0 ,不论是用10000,还是0000来表示,补码仍等于本身。
* 用x -x+1符号就进行符号比较,符号位相同则为0,我最开始实现的是((x>>31)^((~x+1)>>31))+1
* 但是这个是有问题的,右移过程,如果符号位是1,直接就补满1,这是算术右移的特性,
* 所以需要想办法先把符号位提取出来
*/
int logicalNeg(int x) {
return (((~ x + 1) | x) >> 31) + 1;
}
/* howManyBits - return the minimum number of bits required to represent x in
* two's complement
* Examples: howManyBits(12) = 5
* howManyBits(298) = 10
* howManyBits(-5) = 4
* howManyBits(0) = 1
* howManyBits(-1) = 1
* howManyBits(0x80000000) = 32
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
* 这个题是二分法,当时没想出来,看的网友的答案
*/
int howManyBits(int x) {
int b16, b8, b4, b2, b1;
int sign = x >> 31;
// 若为负数,则按位取反;若为正数,则不变
x = (sign & ~x) | (~sign & x);
// 1.若高16位有1,则至少需要16位
b16 = (!!(x >> 16)) << 4;
x >>= b16;
// 2.若高8位有1,则还需要8位
b8 = (!!(x >> 8)) << 3;
x >>= b8;
// 3.若高4位有1,则还需要4位
b4 = (!!(x >> 4)) << 2;
x >>= b4;
// 4.若高2位有1,则还需要2位
b2 = (!!(x >> 2)) << 1;
x >>= b2;
// 5.若高1位有1,则还需要1位
b1 = (!!(x >> 1));
x >>= b1;
// 6.加上符号位
return b16 + b8 + b4 + b2 + b1 + x + 1;
}
//float
/*
* floatScale2 - Return bit-level equivalent of expression 2*f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representation of
* single-precision floating point values.
* When argument is NaN, return argument
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
* 单精度浮点数 4bytes 1符号位,8阶码,23尾数
* 逻辑运算符&&”表示 与,意为同时都要满足 “||”表示 或,意为二者或多着只要满足其中一个
* 先把指数,符号部分都提取出来
*/
unsigned floatScale2(unsigned uf) {
int exp=(uf&0x7f800000)>>23;//相当于把指数的8位提取出来了
int sign = uf&(1<<31);
if(exp==0) return uf<<1|sign; //指数部分=0 原数乘2,再加上符号位,符号位目前是32bit是符号,其余的全都是1
if(exp==255) return uf; //如果指数已经是255,乘以2后没有变化
exp++;//指数+1
if(exp==255) return 0x7f800000|sign;//如果指数满了,就返回无穷大
return (exp<<23)|(uf&0x807fffff);
}
/*
* floatFloat2Int - Return bit-level equivalent of expression (int) f
* for floating point argument f.
* Argument is passed as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point value.
* Anything out of range (including NaN and infinity) should return
* 0x80000000u.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
* 把浮点数转化为定点数,首先要把符号位,指数位,小数位全都单独表示出来
* 单精度浮点数,存在一个偏移量,8位指数部分,偏移量127.8bit本来可以表示0-255,但是
* 0 和 255被表示为无穷和0,非数。所以只能从1-254表示,减去偏移指数 -126到127。
*/
int floatFloat2Int(unsigned uf) {
int exp=(uf&0x7f800000)>>23;//相当于把指数的8位提取出来了
int sign = uf >> 31; //把符号位提取出来
int frac = uf&0x007fffff; //把frac提取出来了
// 指数是0 就返回0
if (exp == 0) return 0;
// 指数都是1 返回无穷
if (exp == 0xFF) return 0x80000000u;
//单精度浮点数的偏移指数,减去
exp -= 127;
// If the exponent is negative, the float is less than one
if (exp < 0) return 0;
// If the exponent is greater than or equal to 31, the float is too large
if (exp >= 31) return 0x80000000u;
// Add the implicit leading one to the fraction
frac |= (1 << 23);
// Shift the fraction left or right according to the exponent
if (exp > 23) {
frac <<= (exp - 23);
} else {
frac >>= (23 - exp);
}
// If the sign is negative, negate the fraction
if (sign) frac = -frac;
return frac;
}
/*
* floatPower2 - Return bit-level equivalent of the expression 2.0^x
* (2.0 raised to the power x) for any 32-bit integer x.
*
* The unsigned value that is returned should have the identical bit
* representation as the single-precision floating-point number 2.0^x.
* If the result is too small to be represented as a denorm, return
* 0. If too large, return +INF.
*
* Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while
* Max ops: 30
* Rating: 4
*/
unsigned floatPower2(int x) {
// 如果x小于-149,返回0
if (x < -126) {
return 0;
}
// 如果x大于127,返回正无穷
if (x > 127) {
return 0x7f800000;
}
// 计算指数部分,加上偏置指数127,左移23位
unsigned exp = (x + 127) << 23;
// 尾数部分为0
unsigned frac = 0;
// 返回指数部分和尾数部分的组合
return exp | frac;
}
1万+




被折叠的 条评论
为什么被折叠?



