《华中科技大学计算机系统基础实验报告.doc》由会员分享,可在线阅读,更多相关《华中科技大学计算机系统基础实验报告.doc(33页珍藏版)》请在金锄头文库上搜索。
1、课 程 实 验 报 告课程名称: 计算机系统基础 专业班级: 学 号: 姓 名: 指导教师: 报告日期: 2016年 5月 24 日 计算机科学与技术学院目录实验1:2实验2:9实验3:22实验总结30实验1: 数据表示 1.1 实验概述 本实验的目的是更好地熟悉和掌握计算机中整数和浮点数的二进制编码表示。实验中,你需要解开一系列编程“难题”使用有限类型和数量的运算操作实现一组给定功能的函数,在此过程中你将加深对数据二进制编码表示的了解。实验语言:c; 实验环境: linux1.2 实验内容 需要完成 bits.c 中下列函数功能,具体分为三大类:位操作、补码运算和浮点数操作。1.3 实验设计
2、 源码如下:/* * 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右移一位再左移一位实现把最低有效位置0 x = x1; x = x1; return x;/* * byteNot - bit-inversion to byte n from word x * Bytes numbered from 0 (LSB)
3、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 = n3; y = yn; x = (xy); return x;/* * byteXor - compare the nth byte of x and y, if it is same, return 0, if not, return 1 * examp
4、le: 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和1 n = nn; y = yn; x = x&(0xff); y = y&(0xff); return !(xy);/* * logicalAnd - x & y * Legal ops: ! & | + * Ma
5、x 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
6、 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 = (131)(32+(n+1)&z)+(xn); return x;/* * parityCheck - returns 1 if x contains an odd num
7、ber of 1s * Examples: parityCheck(5) = 0, parityCheck(7) = 1 * Legal ops: ! & | + * Max ops: 20 * Rating: 4 */int parityCheck(int x) /每次将数的低半数位与高半数位比较,再把y右移31位,最后把y转化为逻辑的0和1 int y; y = x16; y = yx; y = y(y8); y = y(y4); y = y(y2); y = y(y31; return !(!y);/* * mul2OK - Determine if can compute 2*x wi
8、thout 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 = (x31)&0x1)(x30)&0x1); return m0x1;/* * mult3div2 - multiplies by 3/2 rounding toward 0, * Should exactly duplicate ef
9、fect 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时还要+1 int y = (x1)+(y31)&1)&(y31)&1
10、); 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 = (x31)&1; int n = (y31)&1; x
11、= (mn)&(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+1 int y = x31; x = (y&(x+1)+(y)&x); return x;/* * float_abs - Return bit-level e
12、quivalent of absolute value of f for * floating point argument f. * Both the argument and result are passed as unsigned ints, 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&(10x7f800000) return uf; else return x;/* * float_f2i - Return bit-level equivalen