cs app lab1 深入理解计算机系统
来源:互联网 发布:sql developer怎么卸载 编辑:程序博客网 时间:2024/06/03 19:26
#include <iostream>
// Rating: 1
/*
* bitAnd - x&y using only ~ and | 用~和|表示&
* Example: bitAnd(6, 5) = 4 0110 0101
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
return ~(~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)&~(~x&~y);
}
/*
* thirdBits - return word with every third bit (starting from the LSB) set to 1
* 从左到右每三个位有一个1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 1
*/
int thirdBits(void) {
int result = 0x24;
result = (result << 8) + 0x92;
result = (result << 8) + 0x49;
result = (result << 8) + 0x24;
return result;
return 2;
}
// Rating: 2
/*
* fitsBits - return 1 if x can be represented as an
* n-bit, two's complement integer. 如果x 能用n位二进制补码表示,返回1
* 1 <= n <= 32
* Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int fitsBits(int x, int n) {
int mask = x >> 31;
return !(((~x & mask) + (x & ~mask)) >> (n+~0));; // 实在想不出,抄答案了= =
}
/*
* sign - return 1 if positive, 0 if zero, and -1 if negative 抄的
* Examples: sign(130) = 1
* sign(-23) = -1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 10
* Rating: 2
*/
int sign(int x) {
int s = x >> 31;
int sig = (s & (~0)) + ((!s) & (!!x));
return sig;
}
/*
* 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 (x>>(n<<3))&0xff;
}
// Rating: 3
/*
* logicalShift - shift x to the right by n, using a logical shift 逻辑右移
* Can assume that 0 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalShift(int x, int n) {
return ((~(1<<31))>>(n+~0))&(x>>n);
}
/*
* addOK - Determine if can compute x+y without overflow
* Example: addOK(0x80000000,0x80000000) = 0,
* addOK(0x80000000,0x70000000) = 1,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int addOK(int x, int y) {
int a = x&1;
int b = y&1;
int c = a&b;//若最低位都为1,则相加会不丢失
return !(((x+y)>>1)^((x>>1)+(y>>1)+c)); //只这样会丢失右边第一位
}
/* invert - Return x with the n bits that begin at position p inverted
* (i.e., turn 0 into 1 and vice versa) and the rest left
* unchanged. Consider the indices of x to begin with the low-order
* bit numbered as 0.
* Example: invert(0x80000000, 0, 1) = 0x80000001, 10001110
* invert(0x0000008e, 3, 3) = 0x000000b6, 10110110
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int invert(int x, int p, int n) {
int an = ~(-1<<n); //n位1
int bn = an<<p;
return x^bn;
}
// Rating: 4
/*
* bang - Compute !x without using ! 抄的
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
return (~((x >> 31) | (((~x) + 1) >> 31))) & 1;
}
// Extra Credit: Rating: 3
/*
* 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) {
return (((!!x << 31) >> 31) & y) +((((!x) << 31) >> 31) & z);
}
// Extra Credit: Rating: 4
/*
* isPower2 - returns 1 if x is a power of 2, and 0 otherwise
* Examples: isPower2(5) = 0, isPower2(8) = 1, isPower2(0) = 0
* Note that no negative number is a power of 2.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 4
*/
int isPower2(int x) {
//判断是否为2的幂 抄的
//要点:x+mask 为 x-1 x&(x-1) 相当于得到x去掉了最低的为1的那位后的数,2的次幂数对应2进制应该只有1位1 同时还要保证x不为0和负数
int mask = ~0;
return (!(x & (x + mask)) & (!(x >> 31)) & (~(!x)));
}
int main(int argc, char** argv) {
printf("%x %x %x %x %d %x %x\n",
bitAnd(6, 5),bitXor(4, 5),thirdBits(),fitsBits(5,3),sign(150),getByte(0x12345678,1),
logicalShift(0x87654321,4));
printf("%x %x %d %x %x\n",addOK(0x8000000f,0x7000000f),invert(0x0000008e, 3, 3),
bang(0),conditional(2,4,5),isPower2(0));
}
// Rating: 1
/*
* bitAnd - x&y using only ~ and | 用~和|表示&
* Example: bitAnd(6, 5) = 4 0110 0101
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
return ~(~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)&~(~x&~y);
}
/*
* thirdBits - return word with every third bit (starting from the LSB) set to 1
* 从左到右每三个位有一个1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 1
*/
int thirdBits(void) {
int result = 0x24;
result = (result << 8) + 0x92;
result = (result << 8) + 0x49;
result = (result << 8) + 0x24;
return result;
return 2;
}
// Rating: 2
/*
* fitsBits - return 1 if x can be represented as an
* n-bit, two's complement integer. 如果x 能用n位二进制补码表示,返回1
* 1 <= n <= 32
* Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int fitsBits(int x, int n) {
int mask = x >> 31;
return !(((~x & mask) + (x & ~mask)) >> (n+~0));; // 实在想不出,抄答案了= =
}
/*
* sign - return 1 if positive, 0 if zero, and -1 if negative 抄的
* Examples: sign(130) = 1
* sign(-23) = -1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 10
* Rating: 2
*/
int sign(int x) {
int s = x >> 31;
int sig = (s & (~0)) + ((!s) & (!!x));
return sig;
}
/*
* 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 (x>>(n<<3))&0xff;
}
// Rating: 3
/*
* logicalShift - shift x to the right by n, using a logical shift 逻辑右移
* Can assume that 0 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalShift(int x, int n) {
return ((~(1<<31))>>(n+~0))&(x>>n);
}
/*
* addOK - Determine if can compute x+y without overflow
* Example: addOK(0x80000000,0x80000000) = 0,
* addOK(0x80000000,0x70000000) = 1,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int addOK(int x, int y) {
int a = x&1;
int b = y&1;
int c = a&b;//若最低位都为1,则相加会不丢失
return !(((x+y)>>1)^((x>>1)+(y>>1)+c)); //只这样会丢失右边第一位
}
/* invert - Return x with the n bits that begin at position p inverted
* (i.e., turn 0 into 1 and vice versa) and the rest left
* unchanged. Consider the indices of x to begin with the low-order
* bit numbered as 0.
* Example: invert(0x80000000, 0, 1) = 0x80000001, 10001110
* invert(0x0000008e, 3, 3) = 0x000000b6, 10110110
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int invert(int x, int p, int n) {
int an = ~(-1<<n); //n位1
int bn = an<<p;
return x^bn;
}
// Rating: 4
/*
* bang - Compute !x without using ! 抄的
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
return (~((x >> 31) | (((~x) + 1) >> 31))) & 1;
}
// Extra Credit: Rating: 3
/*
* 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) {
return (((!!x << 31) >> 31) & y) +((((!x) << 31) >> 31) & z);
}
// Extra Credit: Rating: 4
/*
* isPower2 - returns 1 if x is a power of 2, and 0 otherwise
* Examples: isPower2(5) = 0, isPower2(8) = 1, isPower2(0) = 0
* Note that no negative number is a power of 2.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 4
*/
int isPower2(int x) {
//判断是否为2的幂 抄的
//要点:x+mask 为 x-1 x&(x-1) 相当于得到x去掉了最低的为1的那位后的数,2的次幂数对应2进制应该只有1位1 同时还要保证x不为0和负数
int mask = ~0;
return (!(x & (x + mask)) & (!(x >> 31)) & (~(!x)));
}
int main(int argc, char** argv) {
printf("%x %x %x %x %d %x %x\n",
bitAnd(6, 5),bitXor(4, 5),thirdBits(),fitsBits(5,3),sign(150),getByte(0x12345678,1),
logicalShift(0x87654321,4));
printf("%x %x %d %x %x\n",addOK(0x8000000f,0x7000000f),invert(0x0000008e, 3, 3),
bang(0),conditional(2,4,5),isPower2(0));
}
0 0
- cs app lab1 深入理解计算机系统
- 《CS:APP》(深入理解计算机系统》reading notes
- 关于《深入理解计算机系统》第二版lab1
- <csapp> data lab (《深入理解计算机系统》lab1)
- Learning by doing 二 深入理解计算机系统(CS:APP)一信息表示
- 知其然,也要知其所以然,《CS: APP--深入理解计算机系统(原书第2版)》 书评
- cs app深入理解计算机系统:第五章 优化程序性能 几个优化的java实现
- 深入理解计算机系统 lab1 ——datalab 解答 (95个ops)
- cs:app2e深入理解计算机系统,原书网址
- 深入理解计算机系统 计算机系统漫游
- 深入理解计算机系统-计算机系统漫游
- 深入理解计算机系统--计算机系统漫游
- CSAPP lab1 datalab-handout(深入了解计算机系统 实验一)
- 《深入理解计算机系统》读书笔记
- 深入理解计算机系统
- 学习《深入理解计算机系统》
- 深入理解计算机系统笔记
- 《深入理解计算机系统》读书笔记
- mysql运维之服务器日志
- 设置popupWindow布局里面的控件
- bus_type
- python自动化之路-javascript补充
- spring +ehcache 缓存实例
- cs app lab1 深入理解计算机系统
- ES6 简单入门分享
- 给 Android 开发者的 RxJava 详解
- arcgis api for js入门开发系列十二 GP服务 实现缓冲区及运算分析(含源代码)
- ehcache 简介和基本api使用
- 中央巡视组进驻北京大学,公告模式与公告内容
- arcgis api for js入门开发系列十三 通过Ajax的调用执行GP服务 (含源代码)
- arcgis api for js入门开发系列十四 GP服务一框架介绍
- arcgis api for js入门开发系列十五 GP服务二手工建模