CSAPP_lab之datalab

之datalab


文章目录

  • CSAPP_lab之datalab
  • 一、代码
    • 1、bitXor
    • 2、tmin
    • 3、isTmax
    • 4、allOddBits
    • 5、negate
    • 6、isAsciiDigit
    • 7、conditional
    • 8、isLessOrEqual
    • 9、logicalNeg
    • 10、howManyBits
    • 11、floatScale2
    • 12、floatFloat2Int
    • 13、floatPower2
  • 总结

一、代码

bits.c文件

1、bitXor

//1
/* * bitXor - x^y using only ~ and & *   Example: bitXor(4, 5) = 1*   Legal ops: ~ &*   Max ops: 14*   Rating: 1*/
int bitXor(int x, int y) {return ~(~(x&(~y))&~((~x)&y));
}

2、tmin

/* * tmin - return minimum two's complement integer *   Legal ops: ! ~ & ^ | + << >>*   Max ops: 4*   Rating: 1*/
int tmin(void) {return 1<<31;}

3、isTmax

//2
/** isTmax - returns 1 if x is the maximum, two's complement number,*     and 0 otherwise *   Legal ops: ! ~ & ^ | +*   Max ops: 10*   Rating: 1*/
int isTmax(int x) {return !(x^(~(1<<31)));
}

4、allOddBits

/* * allOddBits - return 1 if all odd-numbered bits in word set to 1*   where bits are numbered from 0 (least significant) to 31 (most significant)*   Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1*   Legal ops: ! ~ & ^ | + << >>*   Max ops: 12*   Rating: 2*/
int allOddBits(int x) {return !((x&0xaaaaaaaa)^0xaaaaaaaa);
}

5、negate

/* * negate - return -x *   Example: negate(1) = -1.*   Legal ops: ! ~ & ^ | + << >>*   Max ops: 5*   Rating: 2*/
int negate(int x) {return ~x+1;
}

6、isAsciiDigit

//3
/* * isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')*   Example: isAsciiDigit(0x35) = 1.*            isAsciiDigit(0x3a) = 0.*            isAsciiDigit(0x05) = 0.*   Legal ops: ! ~ & ^ | + << >>*   Max ops: 15*   Rating: 3*/
int isAsciiDigit(int x) {int a=!((x>>4)^0x3);int b=!!(0x8^(x&0x8));int c=!((x&0xF)^0x9);int d=!((x&0xF)^0x8);return (a&(b|c|d));
}

7、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 a=~(!!x)+1;//a为全1 且a+1溢出 为0return (a&y)|(~a&z);
}

8、isLessOrEqual

/* * isLessOrEqual - if x <= y  then return 1, else return 0 *   Example: isLessOrEqual(4,5) = 1.*   Legal ops: ! ~ & ^ | + << >>*   Max ops: 24*   Rating: 3*/
int isLessOrEqual(int x, int y) {int a=x>>31&0x1;int b=y>>31&0x1;int c1=(a&~b); //表示 x为- y为+int c2=(~a&b); //表示 x +   y -int e=y+(~x+1); // y-x;int flag=e>>31; //如果flag 和 c2 不同则说明了溢出了return c1 |(!c2&!flag);
}

9、logicalNeg

//4
/* * logicalNeg - implement the ! operator, using all of *              the legal operators except !*   Examples: logicalNeg(3) = 0, logicalNeg(0) = 1*   Legal ops: ~ & ^ | + << >>*   Max ops: 12*   Rating: 4 */
int logicalNeg(int x) {//非0为1,0为0return ((x|(~x+1))>>31)+1;
}

10、howManyBits

/* howManyBits - return the minimum number of bits required to represent x in*             two's complement*  Examples: howManyBits(12) = 5*            howManyBits(298) = 10*            howManyBits(-5) = 4*            howManyBits(0)  = 1*            howManyBits(-1) = 1*            howManyBits(0x80000000) = 32*  Legal ops: ! ~ & ^ | + << >>*  Max ops: 90*  Rating: 4*/
int howManyBits(int x) {//二分法int flag=x>>31;x=(~flag&x)|(flag&~x);int b16,b8,b4,b2,b1,b0,y;y=x>>16;b16=(!!y)<<4;x=x>>b16;y=x>>8;b8=(!!y)<<3;x=x>>b8;y=x>>4;b4=(!!y)<<2;x=x>>b4;y=x>>2;b2=(!!y)<<1;x=x>>b2;y=x>>1;b1=(!!y)<<0;x=x>>b1;b0=x;return b16+b8+b4+b2+b1+b0+1;
}

11、floatScale2

//float
/* * floatScale2 - Return bit-level equivalent of expression 2*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 representation 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 floatScale2(unsigned uf) {unsigned exp=(uf&0x7f800000)>>23;unsigned sign=uf>>31&0x1;unsigned frac=uf&0x7FFFFF;unsigned res;//对非规格数处理if(exp==0xff){return uf;}else if(exp==0){frac<<=1;//res=(sign<<31)|(exp<<23)|frac;}else{exp++;res=(sign<<31)|(exp<<23)|frac;}return res;
}

12、floatFloat2Int

/* * floatFloat2Int - 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 floatFloat2Int(unsigned uf) {int exp=(uf&0x7f800000)>>23;int sign=uf>>31&0x1;int frac=uf&0x7FFFFF;int e=exp-127;if(e<0){return 0;}else if(e>=31){return 0x80000000u;}else{frac=frac|1<<23;if(e<23) {//需要舍入frac>>=(23-e);}else{//右移frac <<= (e - 23);}}if (sign)return ~frac+1;elsereturn frac;
}

13、floatPower2

/* * floatPower2 - Return bit-level equivalent of the expression 2.0^x*   (2.0 raised to the power x) for any 32-bit integer x.**   The unsigned value that is returned should have the identical bit*   representation as the single-precision floating-point number 2.0^x.*   If the result is too small to be represented as a denorm, return*   0. If too large, return +INF.* *   Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while *   Max ops: 30 *   Rating: 4*/
unsigned floatPower2(int x) {//都是正数//int sign=x>>31&0x1;//取int的符号位int E=x;unsigned res;if(E<0){return 0;}else if(E>127){return 0x7f800000;}else{int e=E+127;return res=(0x0<<31)|(e<<23);}
}

总结

datalab主还是考察对基本数据类型的理解,当然本人一开始也花了很长时间来实现,也参考了许多博文

CSAPP datalab相关推荐

  1. CSAPP datalab实验

    1.bitAnd (1)Instructions getByte - Extract byte n from word x Bytes numbered from 0 (LSB) to 3 (MSB) ...

  2. 计算机基础实验_lab1(CSAPP datalab)

    NPU_CS_DataLab 计算机系统基础实验_数据表示 1. bitAnd 2. upperBits 3. anyEvenBit 4. leastBitPos 5. byteSwap 6. isN ...

  3. 【CSAPP Lab-1 DataLab详解】

    CSAPP Datalab详解 文章目录 CSAPP Datalab详解 实验环境 Lab要求 实验内容 bitXor tmin isTmax allOddBits negate isAsciiDig ...

  4. CSAPP实验——DataLab

    CSAPP - DataLab CSAPP实验记录 Data Lab   实验的内容是关于计算机信息的表示,主要包括位操作.整型及浮点型相关知识. 题目列表 名称 任务 难度 bitXor(x, y) ...

  5. CSAPP Lab1:Data Lab (虚拟机安装+Lab环境配置+函数实现)

    目录 前言 一.WIN10虚拟机安装 1.关于Vmware Workstation,Ubuntu和Vmware tools 2.安装步骤 二.Lab环境配置(安装GCC编译套装) 三.README及实 ...

  6. CSAPP - LAB 1 datalab

    Environment Construction 确保有一个linux系统,并已经执行过以下两条命令: 安装gcc:sudo apt-get install build-essential 安装gcc ...

  7. CSAPP实验记录(一):环境配置datalab

    CSAPP实验记录(一):环境配置&datalab 1.环境配置 下载Ubuntu虚拟机.我之前用的是Ubuntu18.04,非常坑,强烈建议换成Ubuntu20.04 windows和Ubu ...

  8. CSAPP实验1:datalab

    前言 CSAPP上的实验还是十分有趣的,尤其是其自动评分系统.做完了实验之后自己也确实对知识的理解更加深入了.有关CSAPP的知识以后或许我会再写博客,现在则先把实验写写博客吧 百度网盘下载实验文档 ...

  9. CSAPP实验记录(1)--------- DataLab

    Datalab Datalab实验是关于数据的机器级表示,实验要求实现给定的位级运算符,同时要满足一些要求,如只能使用某些限定的运算符,运算符总数不超过某数字等.第一次刷感觉难度还是很大的. 题目一: ...

最新文章

  1. R语言保存图片为特定dpi值(分辨率)的图像
  2. 中兴今年的毕业生面试题,给大家参考参考
  3. x window的奥秘
  4. 2018年香港私隐公署接129宗资料外泄通报 创新高
  5. awk分析nginx日志里面的接口响应时间
  6. 2.15.9.menuconfig的实验学习思路
  7. 如何阻止机器人杀害人类?
  8. 16.定位模板,布局和样式
  9. 软考高级 真题 2017年下半年 信息系统项目管理师 论文
  10. CorelDRAW2022最新电脑版离线安装教程
  11. 爬虫抓包问题全面分析
  12. 金融服务公司域名备案
  13. [VT虚拟化驱动]利用EPT实现无痕HOOK
  14. 有限合伙税收“优惠”:上市公司股东大举入疆或为避税
  15. 苏宁小BIU诞生日 机器人员工正式“入职”
  16. 系统突然变慢的处理方案
  17. outlook使用笔记
  18. 程序员如何写出技术好文?
  19. 储能变电站互动系统通讯协议 (征求意见稿)
  20. 转行软件测试的最佳时机,一定要好好看看

热门文章

  1. Jarvis OJ BASIC 公倍数
  2. 《机器学习基石》学习笔记 1 The Learning Problem
  3. vivo手机的android系统,vivo X3S的手机系统是什么?能升级安卓4.3吗?
  4. 【正点原子FPGA连载】第一章 ZYNQ简介 -摘自【正点原子】领航者ZYNQ之FPGA开发指南_V2.0
  5. C primer plus(第六版)第五章源代码
  6. 快速定位到上次编辑位置
  7. 微信第三方平台授权时域名问题
  8. 计算机U盘启动键,u盘启动按哪个键? 教你进入bios设置U盘启动
  9. linux下c使用lzma_linux 编译lzma
  10. 如何以活动价在官网购买百度网盘会员