datalab (原发布 csdn 2018年09月21日 20:42:54)
程序员文章站
2023-11-13 23:40:46
首先声明datalab本人未完成,有4道题目没有做出来。本文博客记录下自己的解析,以便以后回忆。如果能帮助到你就更好了,如果觉得本文没啥技术含量,也望多多包涵。 / bitAnd x&y using only ~ and | Example: bitAnd(6, 5) = 4 Legal ops: ......
首先声明datalab本人未完成,有4道题目没有做出来。本文博客记录下自己的解析,以便以后回忆。如果能帮助到你就更好了,如果觉得本文没啥技术含量,也望多多包涵。
/* * 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); } /* * 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) { int offsetvalue = 0xff; int offsetindex = n << 3; int value = (x & (offsetvalue << offsetindex)) >> offsetindex; return value & offsetvalue; } /* * 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 offset = 0x1 << 31; int offsetvalue = ~(offset >> n << 1); return (x >> n) & offsetvalue; } /* * bitcount - returns count of number of 1's in word * examples: bitcount(5) = 2, bitcount(7) = 3 * legal ops: ! ~ & ^ | + << >> * max ops: 40 * rating: 4 */ int bitcount(int x) { return 2; } /* * bang - compute !x without using ! * examples: bang(3) = 0, bang(0) = 1 * legal ops: ~ & ^ | + << >> * max ops: 12 * rating: 4 */ int bang(int x) { return 2; } /* * tmin - return minimum two's complement integer * legal ops: ! ~ & ^ | + << >> * max ops: 4 * rating: 1 */ int tmin(void) { return (0x1 << 31); } /* * 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 offsetvalue = 0x1 << n; int addvalue = (offsetvalue >> 1) & (~offsetvalue);//2^(n-1) int value1 = x + addvalue;//x - {-[2^(n-1)]} int value2 = addvalue + (~x);//[2^(n-1)-1] - x int maxvalue = 0x1 << 31; return (n >> 5) | ((!(value1 & maxvalue)) & (!(value2 & maxvalue))); } /* * divpwr2 - compute x/(2^n), for 0 <= n <= 30 * round toward zero * examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2 * legal ops: ! ~ & ^ | + << >> * max ops: 15 * rating: 2 */ int divpwr2(int x, int n) { int maxvalue = 0x1 << 31; int offsetvalue = ~(0x1 << 31 >> (32 + ~n)); int andvalue = offsetvalue & x; return (x >> n) + ((!!(x & maxvalue)) & (!!(andvalue))); } /* * 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) { return (!(x >> 31)) ^ (!x); } /* * 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 offsetvalue = 0x1; int offsetindex = 31; int offsetsign = offsetvalue << offsetindex; int signx = !(x & offsetsign); int signy = !(y & offsetsign); int value1 = ((!signx) & signy )^ 0x0; int value2 = (signx & (!signy)) ^ 0x1; int value3 = (!((y + ~x + 1) & offsetsign)) ^ 0x0; return value1 | (value2 & value3); } /* * ilog2 - return floor(log base 2 of x), where x > 0 * example: ilog2(16) = 4 * legal ops: ! ~ & ^ | + << >> * max ops: 90 * rating: 4 */ int ilog2(int x) { return 2; } /* * float_neg - return bit-level equivalent of expression -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 representations of * single-precision floating point values. * when argument is nan, return argument. * legal ops: any integer/unsigned operations incl. ||, &&. also if, while * max ops: 10 * rating: 2 */ unsigned float_neg(unsigned uf) { int offsetvalue = 0x1; int offsetindex = 0; int andvalue = 0; int signvalue; while (offsetindex < 31) { signvalue = (uf & offsetvalue) >> offsetindex; if (offsetindex < 23) { andvalue = andvalue | signvalue; } else { andvalue = andvalue & signvalue; } offsetindex += 1; offsetvalue <<= 1; } if (andvalue) { return uf;//nan } return uf ^ offsetvalue; } /* * 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) { return 2; } /* * 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) { int signindex = 31; int expindex = 23; int offsetvalue = 0x1; int offsetsign = offsetvalue << signindex; int andvalue = 1; int orvalue = 0; int signvalue; int offsetindex = expindex; while (offsetindex < signindex) { signvalue = (uf & (offsetvalue << offsetindex)) >> offsetindex; andvalue = andvalue & signvalue; orvalue = orvalue | signvalue; offsetindex += 1; } if (andvalue == 1)//exp==255 { return uf; } else if (orvalue == 0)//非规格化 { signvalue = !!(uf & offsetsign); uf <<= 1; if (signvalue == 0) { return uf & (~offsetsign); } return uf | offsetsign; } else { signvalue = ((uf >> expindex) + 1) << expindex; offsetindex = expindex; while (offsetindex < signindex) { uf &= ~(offsetvalue << offsetindex); offsetindex += 1; } return uf | signvalue; } }