标题一、实验目的:
1更好地熟悉和掌握计算机中整数和浮点数的二进制编码表示。
2.实验中使用有限类型和数量的运算操作实现一组给定功能的函数,在此过程中加深对数据二进制编码表示的了解
- 熟悉linux基本操作命令,其中常用工具和程序开发环境
4.完善bits.c的各个函数,实现其功能,并通过.btest的测试
标题二、实验要求
-
尽快熟悉linux基本操作命令,还有其中常用工具和程序开发环境
-
除浮点数函数实现外,只能使用顺序程序结构,禁用if, do,
while, for, switch等。
[if !supportLists]n [endif]有限操作类型,!~ & ^ | + << >> 各函数不一样
[if !supportLists]n [endif]禁用(!=、==、&&、|| 等组合操作符)
[if !supportLists]n [endif]常量值范围 0~255
[if !supportLists]n [endif]禁用强制类型转换
[if !supportLists]n [endif]禁用整型外的任何其它数据类型
[if !supportLists]n [endif]禁用定义和宏
[if !supportLists]n [endif]不得使用函数
[if !supportLists]n [endif]具体要求可参看bits.c各函数框架的注释
[if !supportLists]n [endif]可以使用循环和条件控制;
[if !supportLists]n [endif]可以使用整型和无符号整型常量及变量(取值不受[0,255]限制);
[if !supportLists]n [endif]不使用任何浮点数据类型、操作及常量。
[if !supportLists]n [endif]可以使用int和unsigned两种整型数据
[if !supportLists]n [endif]禁用浮点数据类型、struct、union或数组结构。
[if !supportLists]n [endif]浮点数函数均使用unsigned型数据表示浮点数据。
[if !supportLists]n [endif]float_abs等函数必须能处理全范围的变量值,包括(NaN)和infinity。
标题三、实验内容(所修改函数代码,功能以及重要代码的解释):
主要操作内容:位操作函数; 补码运算函数; 浮点数表示函数
例:
/*
功能:将整形x最后一位置零
*/
/*
-
lsbZero - set 0 to the least significant bit of x
-
Example: lsbZero(0x87654321) = 0x87654320
-
Legal ops: ! ~ & ^ | + << >>
-
Maxops: 5
-
Rating: 1
*/
int lsbZero(int x) {
return x>>1<<1; //通过先向右移一位将最低位舍弃,再向左移动,最低位补零
}
或者
int lsbZero(int x) {
return x&(0xfffffffe);
}
/*
功能:把第n(0~3)个字节取反
//取反 1)~ 2)^1
*/
/*
-
byteNot - bit-inversion to byte n from wordx
-
Bytes numbered from 0 (LSB) to 3 (MSB)
-
Examples: getByteNot(0x12345678,1) = 0x1234A978
-
Legalops: ! ~ & ^ | + << >>
-
Maxops: 6
-
Rating: 2
*/
int byteNot(int x, int n){
//让原本的数与第n个字节的位为1,其他位都为0的数取^
int m=(n<<3);//n*8
int a=0xff<<m;
return x^a;
}
/*
功能:比较x,y的第n个字节,相同输出0,不同输出1
*/
-
byteXor - compare the nth byte of x and y,if it is same, return 0, if not, return 1
-
example: byteXor(0x12345678, 0x87654321, 1) = 1
-
byteXor(0x12345678, 0x87344321, 2) = 0
-
Legal ops: ! ~ & ^ | + << >>
-
Maxops: 20
-
Rating: 2
*/
int byteXor(int x, int y,int n) {
x=x>>(n<<3)&0xff;//把第n位字节移到最左边,其他化为0
y=y>>(n<<3)&0xff;
return !!(x^y);//比较两个数
}
/*
功能:实现逻辑与
*/
/*
-
logicalAnd - x && y
-
Legal ops: ! ~ & ^ | + << >>
-
Maxops: 20
-
Rating: 3
*/
int logicalAnd(int x, inty) {
x=!!x;//把数字为0的化成0,其他化为1
y=!!y;
return (x&y);
}
/*
功能:实现逻辑或
*/
/*
-
logicalOr - x || y
-
Legal ops: ! ~ & ^ | + << >>
-
Maxops: 20
-
Rating: 3
*/
int logicalOr(int x, inty) {
x=!!x;//把数字为0的化成0,其他化为1
y=!!y;
return x|y;
}
/*
功能:把数字的前n位移到最后面
*/
/*
-
rotateLeft - Rotate x to the left by n
-
Canassume that 0 <= n <= 31
-
Examples: rotateLeft(0x87654321,4) = 0x76543218
-
Legal ops: ~ & ^ | + << >> !
-
Maxops: 25
-
Rating: 3
*/
int rotateLeft(int x, intn) {
//把前n位取下来移到最右边+(x向左移n位)
int y=32+(~n+1);//y=32-n
int z=x&((~0)<<y);//取前n位,xxx00000
z=(z>>y)&((1<<n)+(~1+1));//-1
x=(x<<n)+z;
return x;
}
/*
功能:判断输入数含有的1的个数是否为奇数,是输出1
*/
/*
-
parityCheck - returns 1 if x contains an oddnumber of 1’s
-
Examples: parityCheck(5) = 0, parityCheck(7) = 1
-
Legal ops: ! ~ & ^ | + << >>
-
Maxops: 20
-
Rating: 4
*/
int parityCheck(int x) {
//把高16位右移16位,和低16位进行按位异或运算,这样就会消掉偶数个数的1;以此类推
x=(x>>16)^x;
x=(x>>8)^x;
x=(x>>4)^x;
x=(x>>2)^x;
x=(x>>1)^x;
return (x&1);
}
/*
功能:x*2,不溢出来
*/
/*
-
mul2OK - Determine if can compute 2*xwithout overflow
-
Examples: mul2OK(0x30000000) = 1
-
mul2OK(0x40000000) = 0
-
Legal ops: ~ & ^ | + << >>
-
Maxops: 20
-
Rating: 2
*/
int mul2OK(int x) {
//发现只要前两位不同就会溢出
int a=(x>>30)&1;//把前面第二个数取出来
int b=(x>>31)&1;// 把前面第一个数取出来
return !(a^b);
}
/*
功能:算出输入数*3/2 ,结果与c语言相同
*/
/*
-
mult3div2 - multiplies by 3/2 roundingtoward 0,
-
Should exactly duplicate effect of C expression (x*3/2),
-
including overflow behavior.
-
Examples: mult3div2(11) = 16
-
mult3div2(-9) = -13
-
mult3div2(1073741824) = -536870912(overflow)
-
Legal ops: ! ~ & ^ | + << >>
-
Maxops: 12
-
Rating: 2
*/
int mult3div2(int x) {
x=(x<<1)+x;//x*3
x+=(x>>31)&1;//加上一个偏移量(2^1-1)
x=(x>>1);//右移一位
return x;
}
加粗样式
/*
功能:判断x-y会不会溢出
*/
/*
-
subOK - Determine if can compute x-y withoutoverflow
-
Example: subOK(0x80000000,0x80000000) = 1,
-
subOK(0x80000000,0x70000000) = 0,
-
Legal ops: ! ~ & ^ | + << >>
-
Maxops: 20
-
Rating: 3
*/
int subOK(int x, int y) {
//只要x与y,x-y的符号位不同时就会溢出。(-++负减正得正,+--)
intz=x+(~y)+1;
int i=(1<<31);
z=z&i;
x=x&i;
y=y&i;
return !((x^y)&(x^z));
}
/*
功能:输入数的绝对值
*/
/*
-
absVal - absolute value of x
-
Example: absVal(-1) = 1.
-
Youmay assume -TMax <= x <= TMax
-
Legal ops: ! ~ & ^ | + << >>
-
Maxops: 10
-
Rating: 4
*/
int absVal(int x) {
//如果这个数是负数,负数的二进制取反加一就是绝对值
int a=(x>>31);
x=(x^a)+!!a;
return x;
}
/*
功能:返回输入数的绝对值,如果是NAN就返回原值
*/
/*
-
float_abs - Return bit-level equivalent ofabsolute value of f for
-
floating point argument f.
-
Boththe argument and result are passed as unsigned int’s, but
-
theyare to be interpreted as the bit-level representations of
-
single-precision floating point values.
-
Whenargument is NaN, return argument…
-
Legal ops: Any integer/unsigned operations incl. ||, &&. alsoif, while
-
Maxops: 10
-
Rating: 2
*/
unsignedfloat_abs(unsigned uf) {
int x=uf&0x7fffffff;//除去符号位
if(x>0x7f800000)//nan的定义,阶码都为1,尾数不为0
return uf;
else
return x;
}
/*
功能:将unsigned转为int型 ,当uf位NAN或越界时返回0x80000000
*/
/*
-
float_f2i - Return bit-level equivalent ofexpression (int) f
-
forfloating point argument f.
-
Argumentis passed as unsigned int, but
-
itis 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. ||, &&. alsoif, while
-
Maxops: 30
-
Rating: 4
*/
intfloat_f2i(unsigned uf) {
int x = ((uf & 0x7f800000) >> 23) -127;//阶码
int y = (uf & 0x007fffff) ^0x00800000;//尾数
if (x > 31 || ((uf & 0x7fffffff) >0x7f800000))
return 0x80000000;
if (x < 0)
return 0;
if (((uf >> 31) & 1) & 0x1)//符号位
{
if (x > 23)
return ~(y << (x - 23)) + 1;
else
return ~(y >> (23 - x)) + 1;
}
else
{
if (x > 23)
return (y << (x - 23));
else
return (y >> (23 - x));
}
}
标题四、实验结果(在虚拟机中,进行语法检查,编译,正确性检查的截图):
标题五、实验总结
通过这次实验,我对计算机底层的数据运算和处理有了一些了解。实践与理论相结合,明白了书上讲的一些内容,以及学会了一些linux的操作命令,更加深入理解了计算机系统基础。