文档介绍:
课程实验报告
课程名称: 计算机系统基础
专业班级:
学号:
姓名:
指导教师:
报告日期: 2016年 5月 24 日
计算机科学与技术学院
目录
实验1: 2
实验2: 9
实验3: 22
实验总结 30
实验1: 数据表示
1.1 实验概述
本实验的目的是更好地熟悉和掌握计算机中整数和浮点数的二进制编码表示。
实验中,你需要解开一系列编程“难题”——使用有限类型和数量的运算操作实现一组给定功能的函数,在此过程中你将加深对数据二进制编码表示的了解。
实验语言:c; 实验环境: linux
1.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右移一位再左移一位实现把最低有效位置0
x = 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<
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和1
n = 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
内容来自淘豆网www.taodocs.com转载请标明出处.