CSAPP实验(1)----datalab

这篇博客详细介绍了CSAPP课程的datalab实验,包括实验说明和2.1至2.13各步骤的解题思路,涉及逻辑运算、位操作、浮点数等知识点,旨在帮助读者理解数据类型的操作方法。
摘要由CSDN通过智能技术生成

1 实验说明

CSAPP课程主页下载datalab实验所需的压缩包。下图说明本次实验需要补齐的函数名列表。
在这里插入图片描述

2 实验

2.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));
}

解题思路:
利用a⊕b = (¬a ∧ b) ∨ (a ∧¬b)公式和德-摩根定律得到上面的代码.

2.2

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

解题思路:
首先明确32位整数最小值的二进制为0x80000000.

2.3

/*
 * 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) {
   
  int res = !((~(x+1))^x) & !!(~x);
  return res ;
}

解题思路:
首先明确32位整形最大值为0x7fffffff.由于题目中规定不能使用移位符号,

2.4

/* 
 * 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) {
   
  int mask = 0xAA | (0xAA<<8) | (0xAA<<16) | (0xAA <<24);
  x = x & mask;
  return !~(x>>1 | x);
}

解题思路:
首先要获取奇数位的信息,则有mask:0xAAAAAAAA.(十六进制A的二进制表示为1010).当奇数位全为1时,(x>>1 | x)则表示0xffffffff,反之,则不成立.

2.5

/* 
 * negate - return -x 
 *   Example: negate(1) = -1.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int negate(int x) {
   
  return</
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值