CS:APP Data Lab

使用方法

1.编写bits.c,然后编译

unix> ./dlc -e bits.c

dlc:bits.c:147:bitXor: 8 operators
dlc:bits.c:158:tmin: 1 operators
dlc:bits.c:169:isTmax: 7 operators
dlc:bits.c:185:allOddBits: 10 operators
dlc:bits.c:196:negate: 2 operators
dlc:bits.c:214:isAsciiDigit: 14 operators
dlc:bits.c:226:conditional: 7 operators
dlc:bits.c:243:isLessOrEqual: 18 operators
dlc:bits.c:261:logicalNeg: 12 operators
dlc:bits.c:306:howManyBits: 37 operators
dlc:bits.c:327:floatScale2: 9 operators
dlc:bits.c:355:floatFloat2Int: 23 operators
dlc:bits.c:375:floatPower2: 5 operators

2.运行和评分 (每次修改bit.c后都要重新编译btest)

unix> make btest && ./btest

Score   Rating  Errors	Function
    1       1		0	bitXor
    1       1   	0	tmin
    1       1   	0	isTmax
    2       2   	0	allOddBits
    2       2   	0	negate
    3       3   	0	isAsciiDigit
    3       3   	0	conditional
    3       3   	0	isLessOrEqual
    4       4   	0	logicalNeg
    4       4   	0	howManyBits
    4       4   	0	floatScale2
    4       4    	0	floatFloat2Int
    4       4    	0	floatPower2
Total points: 36/36

参考代码

整数

bitXor: 计算x^y

/* 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) & ~(~y&x)); // x^y = (~x&y) | (x^~y) 
}

tmin: 返回最小的32位有符号数

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

  return 1<<31;

}

isTmax: 判断一个数是不是最大的32位有符号数

/*
 * 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+x+2) & !!(~x) ;   //x !=-1 && x+x+2 == 0
}

allOddBits: 判断二进制表示中是否所有的奇数位都是1

/* 
 * 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
 *   二进制表示中所有的奇数位都是1
 */
int allOddBits(int x) {
	x = (x>>16) & x;
	x = (x>>8) & x;
	x = (x>>4) & x;
	x = (x>>2) & x;
	return (x>>1)&1;
}

negate: 返回-x

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

isAsciiDigi: 判断一个数是否在[0x30,0x39]范围内

/* 
 * 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 b0 = (x>>3) & 1;
  int b1 = (x>>2) ^ 1;
  int b2 = (x>>1) ^ 1;

  return (!a) & ((!b0) | (b0&b1&b2));
}

conditional: 实现 x ? y : z

/* 
 * 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) {
  x = !x;
  x = (x<<31)>>31;//把x扩展到31位
  return (~x&y) | (x&z);
}

isLessOrEqualisLessOrEqual 返回a<=b

/* 
 * 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 z,s,sx,sy;
  sx = (x>>31)&1;
  sy = (y>>31)&1;
  z = x + ~y + 1; //x-y
  s =  ((z>>31) & 1) | (!(z^0)); //负数或者0
  //x和y符号相同时看差的符号
  //x<0 && y >=0 返回true
  return  ((!(sx^sy))&s) | (sx&(!sy));
}

logicalNeg: 返回一个数的逻辑值,0=flase,非0=true

/* 
 * 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) {
  x |= x>>16;
  x |= x>>8;
  x |= x>>4;
  x |= x>>2;
  x |= x>>1;
  x ^= 1; 
  return x&1;
}

howManyBits:返回用补码表示x需要至少几位

/* 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
 */
//如果是正数的话就直接,二分最高位的1,然后再+1
//如果是负数取反
int howManyBits(int x) {
  int s,c1,c2,c3,c4,c5,c6;
  int cnt = 0;
  s = (x>>31)&1;
  x = ((s<<31)>>31) ^ x;

  s = !!(x>>16);  //高16位是否非0
  c1 = s<<4;      //非0的话右移16位
  x >>= c1;       

  s = !!(x>>8);
  c2 = s<<3;
  x >>= c2;

  s = !!(x>>4);
  c3 = s<<2;
  x >>= c3;

  s = !!(x>>2);
  c4 = s<<1;
  x >>= c4;

  s = !!(x>>1);
  c5 = s;
  x >>= c5;

  c6 = !!x;
  cnt = c1+c2+c3+c4+c5+c6+1;
  return cnt;
}

float

floatScale2: 返回2*f

/* 
 * 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
 * 返回2*f
 */
unsigned floatScale2(unsigned uf) {
  unsigned f = uf;
  if ((f & 0x7F800000) == 0)  //如果阶码为0时,尾数左移1位
    f = ((f & 0x007FFFFF) << 1) | (0x80000000 & f);
  else if ((f & 0x7F800000) != 0x7F800000) //阶码不等于128时,阶码+1
     f =f + 0x00800000;
  return f;   //NaN
}

floatFloat2Int: float转化成int

/* 
 * 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) {
  unsigned INF = 1<<31;
  int e = (uf>>23) & 0xff;
  int s = (uf>>31) & 1;
  if (uf == 0) return 0;
  uf <<= 8;
  uf |= 1<<31;
  uf >>= 8;
  e -= 127;
  if ((uf & 0x7f80000) == 0x7f80000 || e >= 32) return INF;
  if (e < 0) return 0;
  if (e <= 22) uf >>= 23-e;
  else uf <<= e-23;
  if (s) uf = ~uf + 1;
  return uf;
}

floatPower2: 返回pow(2,x)

/* 
 * 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) {
  unsigned INF = 0xff << 23;
  int e = 127 + x;
  if (x < 0) return 0;
  if (e >= 255) return INF;
  return e << 23;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1、概述(Introduction) 1.1测试管理过程(The Test Management Process) 1.2需求定义(Specify Requirements) 1.3测试计划(Planning Tests) 1.4测试执行(Running Tests) 1.5缺陷跟踪(Tracking Defects) 1.6 Working with Project Database 1.7用户权限(User Privileges) 1.8导出Word文档(Exporting Documents from Microsoft Word) 1.9导出Excel文档(Exporting Documents from Microsoft Excel) 1、开始(Getting Started) 1.1启动TestDirector(Starting TestDirector) 1.2 TestDirector窗口(The TestDirector Window) 1.3 TestDirector工具栏(The TestDirector Toolbar) 1.4修改密码(Changing Passwords) 1.5修改用户属性(Changing User Properties) 1.6 清除历史记录(Clearing History) 3、Working With TestDirector Data 1.1 About Working with TestDirector Data 1.2 组织列(Arranging Columns) 1.3 过滤记录(Filtering Records) 1.4高级/交叉过滤记录(Advanced/Cross Filtering Record) 1.5记录分类(Sorting Records) 1.6 刷新和清除设置(Refreshing and Clearing Settings) 1.7保存数据到文件(Saving Data to a File) 4、添加附件(Adding Attachments) 4.1 About Adding Attachments 4.2 Attaching a File 4.3 Attaching a URL 4.4 Attaching a Snapshot 4.5 Attaching System Properties 4.6 Attaching an Image from the Clipboard 4.7 Managing Attachments 5、Working With Favorite Views 4.1 About Working with Favorite Views 4.2 Adding Favorite Views 4.3 Organizing Favorite Views 6、需求定义工作流(The Requirements Specification Workflow) 6.1定义测试范围(Defining the Testing Scope) 6.2创建测试需求大纲(Creating the Testing Requirements Outline) 6.3定义需求(Defining Requirements) 1.4分析需求定义(Analyzing your Requirements Specification) 7、需求模块一览(The Requirements Module at a Glance) 7.1需求模块(The 7.2 需求菜单栏(The Requirements Menu Bar) 7.3 需求工具栏(The Requirements Toolbar) 7.4需求树(Requirements Tree) 1、开发需求树(Developing Requirements Tree) 8.1关于需求树(About the Requirements Tree) 8.2 创建需求树(Creating a Requirements Tree) 8.3 在树中查找需求(Finding Requirements in the Tree) 8.4查看需求树(Viewing the Requirements Tree) 8.5查看需求历史(Viewing Requirement History) 8.6 邮寄需求(Mailing Requirements) 8.7 查看关联缺陷(Viewing Associated Defects) 8.8修改需求树(Modifying the Requirements Tree) 8.9 从需求创建测试(Creating Tests from Requirements) 9、测试计划工作流(The Test Plan Workflow) 9.1定义测试策略(Defining Testing Strategy) 9.2定义测试主题(Defining Test Subjects) 9.3设计测试(Planning Tests) 9.4创建需求覆盖(Creating Requirements Coverage) 9.5设计测试步骤(Designing Test Steps) 9.6自动测试(Automating Tests) 9.7分析测试计划(Analyzing Your Test Plan) 10、测试计划模块一览(The Test Plan Module at a Glance) 10.1测试计划模块(The Test Plan Module) 10.2 测试计划菜单栏(The Test Plan Menu Bar) 10.3 测试计划工具栏(The Test Plan Toolbar) 10.4测试网格(The Test Grid) 11、开发测试计划树(Developing Test Plan Tree) 11.1关于测试计划树(About the Test Plan Tree) 11.2 创建测试计划树(Creating a Test Plan Tree) 11.3 添加测试到测试计划树(Adding Tests to a Test Plan Tree) 11.4查看测试计划树(Viewing the Test Plan Tree) 11.5关联缺陷到测试(Associating Defects with a Test) 11.6 邮寄测试(Mailing Tests) 11.7在树中查找测试(Finding Tests in the Tree) 11.8排列测试计划树(Sorting a Test Plan Tree) 11.9修改测试计划树(Modifying the Test Plan Tree) 12、连接测试到需求(Linking Tests to Requirements) 12.1关于连接测试到需求(About Linking Tests to Requirements) 12.2 连接需求到一个测试(Linking Requirements to a Test) 12.3 连接测试到一个需求(Linking Tests to a Requirement) 11.4连接需求和测试覆盖(Linking Requirements and Tests Coverage) 13、构建测试(Building Tests) 13.1关于构建测试(About Building Tests) 13.2 设计测试步骤(Designing Tests Steps) 13.3 调用一个具有参数的手动测试(Calling a Manual Test with Parameters) 13.4管理测试步骤(Managing Test Steps) 14、创建自动化测试(Developing Test Plan Tree) 14.1关于创建自动化测试(About Creating Automated Tests) 14.2产生自动化测试模板(Generating Automated Test Templates) 15、Working With System Tests 15.1 About Working with Systems Tests 15.2添加系统测试到测试计划树(Adding System Tests to a Test Plan Tree) 15.3定义一个系统测试(Defining a System Test) 15.4运行一个系统测试(Running a System Test) 15.5查看系统测试结果(Viewing System Test Results) 16、测试实验室工作流(The Test Lab Workflow) 16.1创建测试集(Creating Tests Sets) 16.2制定测试运行安排(Scheduling Test Runs) 16.3运行手动测试(Running Tests Manually) 16.4运行自动测试(Running Tests Automatically) 16.5分析测试结果(Analyzing Test Results) 17、测试实验室模块一览(The Test Lab Module at a Glance) 17.1测试实验室模块(The Test Lab Module) 17.2 测试实验室菜单栏(The Test Lab Menu Bar) 17.3 测试实验室工具栏(The Test Lab Toolbars) 17.4执行网格(The Execution Grid) 17.4执行流程(The Execution Flow)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值