diff --git a/labs/datalab/bits.c b/labs/datalab/bits.c index 7e2a1f6..a420dff 100644 --- a/labs/datalab/bits.c +++ b/labs/datalab/bits.c @@ -143,7 +143,7 @@ NOTES: * Rating: 1 */ int bitXor(int x, int y) { - return 2; + return ~(~(x & ~y) & ~(~x & y)); } /* * tmin - return minimum two's complement integer @@ -152,9 +152,7 @@ int bitXor(int x, int y) { * Rating: 1 */ int tmin(void) { - - return 2; - + return 1 << 31; } //2 /* @@ -165,7 +163,7 @@ int tmin(void) { * Rating: 1 */ int isTmax(int x) { - return 2; + return !((x + 1) ^ ~x) & !!(x + 1); } /* * allOddBits - return 1 if all odd-numbered bits in word set to 1 @@ -176,7 +174,11 @@ int isTmax(int x) { * Rating: 2 */ int allOddBits(int x) { - return 2; + x = x & (x >> 16); + x = x & (x >> 8); + x = x & (x >> 4); + x = x & (x >> 2); + return (x >> 1) & 0x01; } /* * negate - return -x @@ -186,7 +188,7 @@ int allOddBits(int x) { * Rating: 2 */ int negate(int x) { - return 2; + return ~x + 1; } //3 /* @@ -199,7 +201,8 @@ int negate(int x) { * Rating: 3 */ int isAsciiDigit(int x) { - return 2; + x = x ^ 0x30; + return (!(x >> 4)) & ((~x >> 3) | ((~x >> 2) & (~x >> 1))); } /* * conditional - same as x ? y : z @@ -209,7 +212,9 @@ int isAsciiDigit(int x) { * Rating: 3 */ int conditional(int x, int y, int z) { - return 2; + x = !!x; + x = (x << 31) >> 31; + return (x & y) | (~x & z); } /* * isLessOrEqual - if x <= y then return 1, else return 0 @@ -219,7 +224,9 @@ int conditional(int x, int y, int z) { * Rating: 3 */ int isLessOrEqual(int x, int y) { - return 2; + int x_neg = x >> 31, y_neg = y >> 31; + y = y + ~x + 1; + return ((x_neg & ~y_neg) | ~((~x_neg & y_neg) | (y >> 31))) & 1; } //4 /* @@ -231,7 +238,12 @@ int isLessOrEqual(int x, int y) { * Rating: 4 */ int logicalNeg(int x) { - return 2; + x = x | (x >> 16); + x = x | (x >> 8); + x = x | (x >> 4); + x = x | (x >> 2); + x = x | (x >> 1); + return ~x & 1; } /* howManyBits - return the minimum number of bits required to represent x in * two's complement @@ -246,7 +258,26 @@ int logicalNeg(int x) { * Rating: 4 */ int howManyBits(int x) { - return 0; + int cnt = 1, a; + x = x ^ (x >> 31); + + a = !!(x >> 16) << 4; + x = x >> a; + cnt = cnt + a; + + a = !!(x >> 8) << 3; + x = x >> a; + cnt = cnt + a; + + a = !!(x >> 4) << 2; + x = x >> a; + cnt = cnt + a; + + a = !!(x >> 2) << 1; + x = x >> a; + cnt = cnt + a; + + return cnt + !!x + !!(x >> 1); } //float /* @@ -261,7 +292,27 @@ int howManyBits(int x) { * Rating: 4 */ unsigned floatScale2(unsigned uf) { - return 2; + unsigned s = uf & 0x80000000, f = uf & 0x007FFFFF, e = (uf >> 23) & 0xFF; + + if (e == 0xFFU) { + return uf; + } + + if (e == 0U) { + if (f == 0U) { + return uf; + } else { + e = (f >= 0x00400000U); + f = (f << 1) & 0x007FFFFFU; + return s | (e << 23) | f; + } + } + + e = e + 1; + if (e == 0xFFU) { + f = 0; + } + return s | (e << 23) | f; } /* * floatFloat2Int - Return bit-level equivalent of expression (int) f @@ -276,7 +327,31 @@ unsigned floatScale2(unsigned uf) { * Rating: 4 */ int floatFloat2Int(unsigned uf) { - return 2; + unsigned s = uf & 0x80000000, f = uf & 0x007FFFFF, e = (uf >> 23) & 0xFF; + + if (e == 0xFFU) { + return 0x80000000U; + } + + if (e == 0U) { + return 0; + } else { + int E = e - 127; + + if (E <= -1) { + return 0; + } else if (E <= 30) { + f = (f | 0x00800000); + if (E >= 23) { + f = f << (E - 23); + } else { + f = f >> (23 - E); + } + return s ? -f : f; + } else { + return 0x80000000U; + } + } } /* * floatPower2 - Return bit-level equivalent of the expression 2.0^x @@ -292,5 +367,13 @@ int floatFloat2Int(unsigned uf) { * Rating: 4 */ unsigned floatPower2(int x) { - return 2; + if (x < -149) { + return 0U; + } else if (x <= -127) { + return 1U << (x + 149); + } else if (x <= 127) { + return (x + 127) << 23; + } else { + return 0x7F800000U; + } }