计算机基础实验_lab1(CSAPPdatalab)
NPU_CS_DataLab
计算机系统基础实验_数据表⽰
使⽤限制的操作符,来完成⼀些任务,主要考察了移位运算等。如果能⾃⼰认真完成,⼀定会⾮常有收获,会对计算机底层⼀些东西实现和编码的概念有着⼀个感性的认识,尤其是浮点数那三道题,会让你对IEEE754标准定义的浮点数有着极为深刻的认识。
这个实验为⼀个⼯程,但我们只需要写bits.c⾥⾯的⼀些函数,整个框架是搭好的。然后整个⼯程智能化程度挺⾼的,不满⾜条件都没法make。
这个⼯程要在ubuntu32位机器上运⾏,可以安装虚拟机,也可以在ubuntu64位上安装32位的包。
下⾯时bits.c⾥⾯的⼀些函数。
1. bitAnd
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x,int y){
int ans =~((~x)|(~y));
return ans;
}
有点离散数学基础知识应该就可以完成。
2. upperBits
/*
* upperBits - pads n upper bits with 1's
* You may assume 0 <= n <= 32
* Example: upperBits(4) = 0xF0000000
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 10
* Rating: 1
*/
int upperBits(int n){
int a =1<<31;
int b = n +(~0);
李宗仁简历
int k =((!!n)<<31)>>31;
return k &(a >> b);
}
重点在于处理n为32的情况
3. anyEvenBit
/*
* anyEvenBit - return 1 if any even-numbered bit in word t to 1
* Examples anyEvenBit(0xA) = 0, anyEvenBit(0xE) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
秋天的公园
* Rating: 2
*/
int anyEvenBit(int x){
return!!((x |(x >>8)|(x >>16)|(x >>24))&0x55);
}
4. leastBitPos
* leastBitPos - return a mask that marks the position of the
* least significant 1 bit. If x == 0, return 0
* Example: leastBitPos(96) = 0x20
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int leastBitPos(int x){
return(~x +1)& x;
}
题意为⽤⼀个1标记出最低有效位的位置。
如:96:0110 0000(int为32位,现为简写)
则返回:0010 0000
精髓在于取反加⼀。然后进⾏简单的掩码操作便可。
5. byteSwap
奶瓶有保质期吗/*
* byteSwap - swaps the nth byte and the mth byte
* Examples: byteSwap(0x12345678, 1, 3) = 0x56341278
* byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD
* You may assume that 0 <= n <= 3, 0 <= m <= 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 25
* Rating: 2
*/
int byteSwap(int x,int n,int m){
int xn = n <<3;
int xm = m <<3;
int x1 =(x >> xn)<< xm;
int x2 =(x >> xm)<< xn;
int ans =(x &(~(0xff<< xn))&(~(0xff<< xm)))|(x1 &(0xff<< xm))|(x2 &(0xff<< xn)); return ans;
}
⾼低字节交换,然后进⾏掩码。
6. isNotEqual
/*
* isNotEqual - return 0 if x == y, and 1 otherwi
* Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1
* Legal ops: ! ~ & ^ | + << >>怎么洗
* Max ops: 6
* Rating: 2
*/
int isNotEqual(int x,int y){
return!!(x^y);
}
利⽤异或的特性,再⽤逻辑操作符将结果转化为0或1的逻辑值。
7. float_neg
* float_neg - Return bit-level equivalent of expression -f for
* floating point argument f.
* Both the argument and result are pasd as unsigned int's, but
xx中国
* they are to be interpreted as the bit-level reprentations of
* single-precision floating point values.
* When argument is NaN, return argument.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 10
* Rating: 2
*/
unsigned float_neg(unsigned uf){
unsigned result= uf ^0x80000000;//使⽤按位异或和掩码对符号位取反其余为不变unsigned tmp = uf &0x7fffffff;//为下⼀步看是不是NaN数准备
if(tmp >0x7f800000)//若满⾜条件则尾数⾮零,全1阶码
result = uf;//是NaN数,返回原值,否则返回原数符号位取反对应的数
return result;
}
解释看注释。
8. implication
/*
* implication - return x -> y in propositional logic - 0 for fal, 1
* for true
* Example: implication(1,1) = 1
* implication(1,0) = 0
* Legal ops: ! ~ ^ |
* Max ops: 5
* Rating: 2
*/
int implication(int x,int y){
return(!x)|(!!y);
}
离散数学基础知识。
9. bitMask
/*
* bitMask - Generate a mask consisting of all 1's
* lowbit and highbit
* Examples: bitMask(5,3) = 0x38
* Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31
* If lowbit > highbit, then mask should be all 0's
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/
int bitMask(int highbit,int lowbit){
int i =~0;
return(((i << highbit)<<1)^(i << lowbit))&(i << lowbit);
}
题意为lowbit道highbit之间全为1。
例bitmask(5,3)应返回第三位到第五位为1其余为为0的⼀个int型变量。即0x38 : 0011 1000
没啥说的,⾃⼰看代码,巧⽤移位吧。
10. 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 m =~!x +1;
return(y &~m)|(z & m);
}
类似于数字逻辑⾥的多路选择器,学了数字逻辑应该就简单了。
11. isLessOrEqual
/*
* isLessOrEqual - if x <= y then return 1, el return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x,int y){
int signx=x>>31;
int signy=y>>31;
int sameSign=(!(signx^signy));
return(sameSign &((x+(~y))>>31))|((!sameSign)& signx);
}
学了数电也应该感觉⽐较简单。
12. isPositive
/*
* isPositive - return 1 if x > 0, return 0 otherwi
* Example: isPositive(-1) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 3
*/
int isPositive(int x){
return!((x>>31)|(!x));
}
⽐较简单,主要是处理符号位,但对0要特殊处理。
13. satMul3
* satMul3 - multiplies by 3, saturating to Tmin or Tmax if overflow
* Examples: satMul3(0x10000000) = 0x30000000
* satMul3(0x30000000) = 0x7FFFFFFF (Saturate to TMax)
* satMul3(0x70000000) = 0x7FFFFFFF (Saturate to TMax)
火星有大气层吗
* satMul3(0xD0000000) = 0x80000000 (Saturate to TMin)
* satMul3(0xA0000000) = 0x80000000 (Saturate to TMin)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 25
* Rating: 3
*/
int satMul3(int x){
int two = x <<1;
int three = two + x;
int xSign = x &(0x80<<24);
int twoSign = two &(0x80<<24);
int threeSign = three &(0x80<<24);
int mask =((xSign ^ twoSign)|(xSign ^ threeSign))>>31;
int smask = xSign >>31;
return(~mask & three)|(mask &((~smask &~(0x1<<31))|(smask &(0x80<<24))));
}
注意溢出的判断与处理,我好像也⽤了数电⾥⾯多路选择器的思想。
14. float_half
/*
* float_half - Return bit-level equivalent of expression 0.5*f for
* floating point argument f.
* Both the argument and result are pasd as unsigned int's, but
* they are to be interpreted as the bit-level reprentation 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
*/
unsigned float_half(unsigned uf){
int round, S, E, maskE, maskM, maskS, maskEM, maskSM, tmp;
round =!((uf &3)^3);//处理精度问题,进⾏舍⼊时需要的参数
maskS =0x80000000;
maskE =0x7F800000;
maskM =0x007FFFFF;
maskEM=0x7FFFFFFF;
maskSM=0x807FFFFF;
申请入党流程
E = uf & maskE;//⽤于判断是否为NaN数
S = uf & maskS;//去符号位
if(E >0x7F800000)return uf;//阶码全1,⾮0尾数,NaN数直接返回
if(E ==0x00800000){//阶码只有最后⼀位为1,除2之后要变为⾮规格化数,按⾮//规格化数处理,即处理阶码下溢情况
return S |(round +((uf & maskEM)>>1));
}
if(E ==0x00000000){//阶码全0,那要么是0,要么是⾮规格化数,直接右移⼀位同时保留符号位
tmp =(uf & maskM)>>1;
return S |(tmp + round);
}
return(((E>>23)-1)<<23)|(uf & maskSM);//规格化数,正常处理,阶码减⼀
}
这个考察了相当细致了,要对IEEE754标准定义的浮点数有着深刻的理解,对阶码的各种情况对应的不同意义要了解。参考了某位⼤神的博客才过的这道题。
15. float_i2f