CSAPP——实验一 位运算

来源:互联网 发布:东莞信捷plc编程兼职 编辑:程序博客网 时间:2024/05/16 12:13

最近学习了Coursera上面的《Hardware/Software Interface》 。该课源自于华盛顿大学计算机专业,华盛顿大学将该课搬到了Coursera,现在已经是公开课性质的,视频完全可以自由下载。

课程链接如下:Coursera–Hardware/Software Interface

这门课的大部分内容是对应于书《深入理解计算机系统》,该书的英文名称是《Computer Systems: A Programmer’s Perspective》。 该书讲解了计算机的底层知识,其中大部分我认为是一个合格程序员必须掌握的,在此推荐此书。该课的主要亮点是实验,5个实验不同的知识考查,能够让我们比平时更加深入地了解计算机的原理。

位运算

实验一考查了位运算,实验的要求是实现用几个有限的运算符实现简单的功能函数。但是有一些自己感觉还是很难实现,参考了github之后才明白过来。具体如下:

/*  * bitAnd - x&y using only ~ and |  *   Example: bitAnd(6, 5) = 4 *   Legal ops: ~ | *   Max ops: 8 *   Rating: 1 */int bitAnd(int x, int y) {        //只用 ~ 和 | 操作符 来实现与操作        // 将 x 和 y的 为0的位 并起来 然后取反即可       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) {  //要点:找到 x 和 y 中 都为0 和都为1的 位  // (x&y)都为1     (~x&~y)都为0  return ~(x&y)&(~(~x&~y));}/*  * thirdBits - return word with every third bit (starting from the LSB) set to 1 * and the rest set to 0 *   Legal ops: ! ~ & ^ | + << >> *   Max ops: 8 *   Rating: 1 */int thirdBits(void) {//返回的int值的二进制中每3位中有一个1并在3位中的最低位  int result = 0x49; result = (result << 8) + 0x24; result = (result << 8) + 0x92; result = (result << 8) + 0x49; return result;}// Rating: 2/*  * fitsBits - return 1 if x can be represented as an  *  n-bit, two's complement integer. *   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) {//判断一个数为正负或者0  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) {  //返回x中的第N字节  return (x>>(n<<3))&(0x000000ff);}// 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) {   int mask = ((1 << 31) >> n) << 1;   return (x >> n) & ~mask;}/*  * 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) {//要点:相加前符号位相同,相加后的结果符号位与x和y不同,anemic代表加法溢出  int sx = x>>31;  int sy = y>>31;  int ssum = (x+y) >> 31;  return !(~(sx ^ sy) & (sy ^ ssum));}// 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)));}
0 0
原创粉丝点击