SSD6 Exercise 2: Data Lab(Manipulating Bits)
来源:互联网 发布:电信短信平台软件 编辑:程序博客网 时间:2024/05/17 07:33
解题思路:
bitAnd(x,y)
根据逻辑学基本知识易得bitOr(x,y)
根据逻辑学基本知识易得isZero(x)
!x操作符只会返回0(x不为0时)或1(x=0时),符合题目要求minusOne(void)
-1的补码表示是:0xFFFFFFFF,其对应的取反数为0x00000000,刚好为0,故为~0tmax(void)
0x80二进制表示即1000 0000,又因为32位的2的补码表示的最大数为 0111 1111 1111 1111 1111 1111 1111 1111,故将其左移24位(得1000 0000 0000 0000 0000 0000 0000 0000),再取反即满足题意bitXor(x,y)
推理过程如下,getByte(x,n)
将数据右移n*(2^3)位,即使得其高地址起的24位都为0,再与0xFF进行&运算,即可在最低字节截取所需的数据isEqual(x,y)
根据异或的定义,易得正解negate(x)
分为x为正数,0,负数三种情况来讨论,
a.根据2的补码运算的定义,易得正数取反加1即为其相反数;
b.0000 0000 执行2的补码运算(各位取反+1)得到 1 0000 0000 ,忽略溢位(只有八个位),则得 0000 0000
c.根据负数在计算机中的表示方法,可知,除了第一位后,其余所有位进行2的补码运算(各位取反+1),即可求其正值,而第一位取反后为0,无影响
综上,~x+1即满足题意isPositive(x)
a.正负之分根据符号位来判断即可,(1<<31)得1000 0000 0000 0000 0000 0000 0000 0000,当x为负数时,out=!((1 << 31) & x)返回0;否则返回1。
b.再来去除x为0的情况,out^!x返回值的情况如下
x为正数时,1^0=1;
x=0,1^1=0;
x为负数时,0^0=0;
显然,满足题意
修改bits.c后,程序运行效果如下:
bit.c文件部分:
/* * bitAnd - x&y using only ~ and | * Example: bitAnd(6, 5) = 4 * Legal ops: ~ | * Max ops: 8 * Rating: 1 */int bitAnd(int x, int y) { return ~((~x)|(~y));}/* * bitOr - x|y using only ~ and & * Example: bitOr(6, 5) = 7 * Legal ops: ~ & * Max ops: 8 * Rating: 1 */int bitOr(int x, int y) { return ~((~x)&(~y));}/* * isZero - returns 1 if x == 0, and 0 otherwise * Examples: isZero(5) = 0, isZero(0) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 2 * Rating: 1 */int isZero(int x) { return !x;}/* * minusOne - return a value of -1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 2 * Rating: 1 */int minusOne(void) { return ~0;}/* * TMax - return maximum two's complement integer * Legal ops: ! ~ & ^ | + << >> * Max ops: 4 * Rating: 1 */int tmax(void) { int x = 0x80;// 二进制表示即1000 0000 return(~(x << 24));}/* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Legal ops: ~ & * Max ops: 14 * Rating: 2 */int bitXor(int x, int y) { int a = ~(x&(~y)); int b = ~((~x)&y); return ~(a&b);}/* * getByte - Extract byte n from word x * Bytes numbered from 0 (LSB) to 3 (MSB) * Examples: getByte(0x12345678,1) = 0x56 * Legal ops: ! ~ & ^ | + << >> * Max ops: 6 * Rating: 2 */int getByte(int x, int n) { return (0xFF & (x >> (n << 3)));}/* * isEqual - return 1 if x == y, and 0 otherwise * Examples: isEqual(5,5) = 1, isEqual(4,5) = 0 * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */int isEqual(int x, int y) { return !(x^y);}/* * negate - return -x * Example: negate(1) = -1. * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */int negate(int x) { return ~x+1;}/* * isPositive - return 1 if x > 0, return 0 otherwise * Example: isPositive(-1) = 0. * Legal ops: ! ~ & ^ | + << >> * Max ops: 8 * Rating: 3 */int isPositive(int x) { int out = !((1 << 31) & x); int iszero = !x; return out ^ iszero;}
- SSD6 Exercise 2: Data Lab(Manipulating Bits)
- SSD6 Exercise 1: Decoding Lab
- CSAPP Lab1--Manipulating Bits
- CSAPP Lab1:Manipulating Bits
- Lesson9 Manipulating Data
- Data Lab
- 卡内基SSD6 System-Level Programming Exercise 1总结
- 2 Manipulating the Pixels
- Manipulating Rightmost Bits--come from Hacker's Delight
- CMU data lab
- CS:APP2e Data Lab
- CSAPP Data Lab
- ICS data lab总结
- data lab using zeppelin
- CSAPP data Lab
- Take Assessment: Exercise 1: Decoding Lab
- Data lab(two complements arithmetic)
- Accessing and Manipulating XML Data in .NET I
- WebGIS学习(九)WebGIS中的矢量查询(针对AGS和GeoServer)
- 用sflow玩儿转 共享专线带宽占用情况
- 移动端学习笔记(一)
- quartz调度器表达式
- 使用HTTP响应头X-Frame-Options防止网页被Frame
- SSD6 Exercise 2: Data Lab(Manipulating Bits)
- lua文件操作
- Spring learn note 3
- 小希的迷宫(并查集)
- matlab验证中心切片定理
- 关于conventer转换器
- 插入排序(Java实现)
- 排序算法 JS实现
- java线程学习(一)