当前位置:文档之家› 华中科技大学计算机系统基础实验报告

华中科技大学计算机系统基础实验报告

课程实验报告课程名称:计算机系统基础专业班级:学号:姓名:指导教师:报告日期: 2016年 5月 24 日计算机科学与技术学院目录实验1: (2)实验2: (9)实验3: (23)实验总结 (32)实验1:数据表示1.1 实验概述本实验的目的是更好地熟悉和掌握计算机中整数和浮点数的二进制编码表示。

实验中,你需要解开一系列编程“难题”——使用有限类型和数量的运算操作实现一组给定功能的函数,在此过程中你将加深对数据二进制编码表示的了解。

实验语言:c; 实验环境: linux1.2 实验容需要完成 bits.c 中下列函数功能,具体分为三大类:位操作、补码运算和浮点数操作。

1.3 实验设计源码如下:/** lsbZero - set 0 to the least significant bit of x* Example: lsbZero(0x87654321) = 0x87654320* Legal ops: ! ~ & ^ | + << >>* Max ops: 5* Rating: 1*/int lsbZero(int x) {//x右移一位再左移一位实现把最低有效位置0x = x>>1;x = x<<1;return x;}/** byteNot - bit-inversion to byte n from word x* Bytes numbered from 0 (LSB) to 3 (MSB)* Examples: getByteNot(0x12345678,1) = 0x1234A978* Legal ops: ! ~ & ^ | + << >>* Max ops: 6* Rating: 2*/int byteNot(int x, int n) {//x第n个字节每位都和1异或实现取反int y = 0xff;n = n<<3;y = y<<n;x = (x^y);return x;}/** 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: ! ~ & ^ | + << >>* Max ops: 20* Rating: 2*/int byteXor(int x, int y, int n) {//把x和y的第n个字节取出来异或,再转换为逻辑的0和1n = n<<3;x = x>>n;y = y>>n;x = x&(0xff);y = y&(0xff);return !!(x^y);}/** logicalAnd - x && y* Legal ops: ! ~ & ^ | + << >>* Max ops: 20* Rating: 3*/int logicalAnd(int x, int y) {//把x和y分别转化为逻辑的0和1,再相与x = (!(!x))&(!(!y));return x;}/** logicalOr - x || y* Legal ops: ! ~ & ^ | + << >>* Max ops: 20* Rating: 3*/int logicalOr(int x, int y) {//把x和y分别转化为逻辑的0和1,再相或x = (!(!x))|(!(!y));return x;}/** rotateLeft - Rotate x to the left by n* Can assume that 0 <= n <= 31* Examples: rotateLeft(0x87654321,4) = 0x76543218* Legal ops: ~ & ^ | + << >> !* Max ops: 25* Rating: 3*/int rotateLeft(int x, int n) {//先构造低n位为1,高(32-n)位为0的数z,x左移n位后的数加上x右移(32-n)位的数&z即可int z;z = ~(((1<<31)>>31)<<n);x = ((x>>(32+(~n+1)))&z)+(x<<n);return x;}/** parityCheck - returns 1 if x contains an odd number of 1's* Examples: parityCheck(5) = 0, parityCheck(7) = 1* Legal ops: ! ~ & ^ | + << >>* Max ops: 20* Rating: 4*/int parityCheck(int x) {//每次将数的低半数位与高半数位比较,再把y右移31位,最后把y转化为逻辑的0和1int y;y = x<<16;y = y^x;y = y^(y<<8);y = y^(y<<4);y = y^(y<<2);y = y^(y<<1);y = y>>31;return !(!y);}/** mul2OK - Determine if can compute 2*x without overflow* Examples: mul2OK(0x30000000) = 1* mul2OK(0x40000000) = 0** Legal ops: ~ & ^ | + << >>* Max ops: 20* Rating: 2*/int mul2OK(int x) {//把x第31位和30位分别和1做按位与,再异或,再和1异或int m;m = ((x>>31)&0x1)^((x>>30)&0x1);return m^0x1;}/** mult3div2 - multiplies by 3/2 rounding toward 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: ! ~ & ^ | + << >>* Max ops: 12* Rating: 2*/int mult3div2(int x) {//左移一位再+x即x*3,右移一位的时候,当y的最高位和最低位都为0时还要+1int y = (x<<1)+x;y = (y>>1)+(((y>>31)&1)&(((y<<31)>>31)&1));return y;}/** subOK - Determine if can compute x-y without overflow* Example: subOK(0x80000000,0x80000000) = 1,* subOK(0x80000000,0x70000000) = 0,* Legal ops: ! ~ & ^ | + << >>* Max ops: 20* Rating: 3*/int subOK(int x, int y) {//x的最高有效位和y的最高有效位不同且x和(x-y)的最高位不同才能判断溢出int m = (x>>31)&1;int n = (y>>31)&1;x = (m^n)&(m^(((x+(~y+1))>>31)&1));return (!x);}/** absVal - absolute value of x* Example: absVal(-1) = 1.* You may assume -TMax <= x <= TMax* Legal ops: ! ~ & ^ | + << >>* Max ops: 10* Rating: 4*/int absVal(int x) {//x最高位为0时就是x,最高位为1时是~x+1int y = x>>31;x = (y&(~x+1))+((~y)&x);return x;}/** float_abs - Return bit-level equivalent of absolute value of 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 representations 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_abs(unsigned uf) {int x=uf&(~(1<<31));if(x>0x7f800000){return uf;}else return x;}/** float_f2i - 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*/int float_f2i(unsigned uf) {unsigned num=0x80000000;int x=(uf&0x007fffff)^0x00800000;int order=0;order=(uf&0x7f800000)>>23;if(order>158){return num;}if(order<127) return 0;else if(((uf>>31)&1)==1){if(order>150){return ~(x<<(order-150))+1;}else return ~(x>>(150-order))+1;}else{if(order>150) return x<<(order-150);else return x>>(150-order);}}1.4 实验过程编写源码,运行btest,得出实验结果。

相关主题