linux按位运算datalab-handout
来源:互联网 发布:养牛软件下载 编辑:程序博客网 时间:2024/05/22 12:14
进入bits.c ,根据bits.c中的具体要求补全以下13个函数:
•intbitXor(intx, int y);
•inttmin(void);
•intisTmax(intx);
•ntallOddBits(intx);
•intnegate(int x);
•intisAsciiDigit(intx);
•intconditional(int x, inty, int z);
•intisLessOrEqual(intx, int y);
•intlogicalNeg(intx);
•inthowManyBits(intx);
•unsignedfloat_twice(unsigneduf);
•unsignedfloat_i2f(int x);
•intfloat_f2i(unsigned uf);
代码如下
//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))));}/* * tmin - return minimum two's complement integer * Legal ops: ! ~ & ^ | + << >> * Max ops: 4 * Rating: 1 */int tmin(void) { return 1<<31;}//2/* * isTmax - returns 1 if x is the maximum, two's complement number, * and 0 otherwise * Legal ops: ! ~ & ^ | + * Max ops: 10 * Rating: 2 */int isTmax(int x) { return !((x+x+2)|!(~x));}/* * allOddBits - return 1 if all odd-numbered bits in word set to 1 * Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 12 * Rating: 2 */int allOddBits(int x) { int y=x>>16;x=x&y; y=x>>8;x=x&y; return !((x&0xAA)^0xAA);}/* * negate - return -x * Example: negate(1) = -1. * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */int negate(int x) { return (~x+1);}//3/* * 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 min=0x30; int max=0x39; return (!((x+(~min+1))>>31)) & (!((max+(~x+1))>>31));}/* * 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+1))&y)|((~!x+1)&z);}/* * 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 x1=x>>31; int y1=y>>31; int r=((x+(~y))>>31)&(!(x1^y1)); int s=x1&(!y1); return r|s;}//4/* * 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) { return ~(((~x+1)|x)>> 31)&1;}/* 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 */int howManyBits(int x) { int shiftSign = x >> 31; int operations = shiftSign ^ x; int negateOps = !operations; int oppSign = (!(!operations) << 31) >> 31; int shift16 = !(!(operations >> 16)) << 4; operations = operations >> shift16; int shift8 = !(!(operations >> 8)) << 3; operations = operations >> shift8; int shift4 = !(!(operations >> 4)) << 2; operations = operations >> shift4; int shift2 = !(!(operations >> 2)) << 1; operations = operations >> shift2; int shift1 = !(!(operations >> 1)); operations = shift16 + shift8 + shift4 + shift2 + shift1; operations += 2; return (negateOps | (operations & oppSign));}//float/* * float_twice - 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 */unsigned float_twice(unsigned uf) { unsigned temp = uf & 0x7F800000; unsigned sign = uf & 0x80000000; if (temp) { if ( temp != 0x7F800000 ) { uf = uf + 0x00800000 ; if (temp == 0x7F000000) uf = (uf & 0xFF800000); } } else uf = ( uf << 1) | sign ; return uf;}/* * float_i2f - Return bit-level equivalent of expression (float) x * Result is returned as unsigned int, but * it is to be interpreted as the bit-level representation of a * single-precision floating point values. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */unsigned float_i2f(int x) { unsigned shiftLeft=0; unsigned afterShift, tmp, flag; unsigned absX=x; unsigned sign=0; if (x==0) return 0; if (x<0) { sign=0x80000000; absX=-x; } afterShift=absX; while (1) { tmp=afterShift; afterShift<<=1; hiftLeft++; if (tmp & 0x80000000) break; } if ((afterShift & 0x01ff)>0x0100) flag=1; else if ((afterShift & 0x03ff)==0x0300) flag=1; else flag=0; return sign + (afterShift>>9) + ((159-shiftLeft)<<23) + flag; }/* * float_f2i - 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 float_f2i(unsigned uf) { int flag = uf & (1 << 31); uf = uf & ~(1 << 31); int exp = (uf >> 23); int num = exp + ~126; if( !exp || (num & (1 << 31))) return 0; else if(!(exp ^ 0xFF)) { return (1 << 31); } else { num = 1 << num; if(flag) return ~num + 1; else return num; }}
如何编译:
0 0
- linux按位运算datalab-handout
- datalab-handout实验
- Datalab-handout数据表示实验
- 一些有趣的位运算题(CMU 15213 DataLab)
- CSAPP lab1 datalab-handout(深入了解计算机系统 实验一)
- linux内核中的位运算
- Linux c中的位运算
- Linux c中的位运算
- linux内核中的位运算
- 【Linux】C语言位运算
- 按位运算
- 按位运算符
- 按位运算
- 按位运算符
- 按位操作运算
- 按位运算符
- 按位运算 & 、|、^、~
- 按位运算
- 欢迎使用CSDN-markdown编辑器
- 获取准确的操作系统名称的Java工具类
- mysql安装最后一步error nr.1045解决方法
- Pixhawk_bootloader简介
- Mysql(四)- 存储引擎和事务,备份和恢复,索引,存储过程
- linux按位运算datalab-handout
- mysql避免插入重复记录
- 自定义导航条时返回按钮失效的问题
- Mahalanobis距离(马氏距离)的“哲学”解释
- 手动调用手机返回键
- String、StringBuffer与StringBuilder的区别
- 1.block_inode
- Android HandlerThread使用
- iOS动画详解