linux按位运算datalab-handout
2016-04-28 13:58
429 查看
进入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);
代码如下
如何编译:
•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; } }
如何编译:
相关文章推荐
- 解决Linux c语言运行时候“段错误 (核心已转储)”问题-采用gdb 解决
- centos 6.7 zerorpc TypeError
- Linux内核分析 期末总结
- 王高利:Kvm虚拟化(4)__为客户机添加virsh console支持
- CentOS 7 安装 mysql-5.6.14
- linux基础(四)
- GraphicsMagick在centos环境的安装
- linux 小技巧
- Centos安装
- Linux下C语言Mysql数据库使用范例
- Linux下python安装升级详细步骤 | Python2 升级 Python3
- Linux System Admin Handbook - Service Config
- win10 安装vagrant + VirtualBox并安装系统(linux/centos/ubuntu)
- CentOS yum更换为国内的源,解决安装软件时无法从国外镜像下载的问题
- CentOS 7 安装 php-5.6.20
- Linux下远程连接Jupyter+pyspark部署教程
- Linux 打开文件数1024限制的原理以及解决办法
- CentOS7执行ifconfig Command not found 未找到命令
- Linux最大打开文件描述符数
- linux下面的fd限制