update datalab01/bits.c

This commit is contained in:
2025-09-29 15:45:56 +09:00
parent 29734eb1e2
commit 8813f46c6c

View File

@@ -187,7 +187,7 @@ int bitAnd(int x, int y) {
* Rating: 1 * Rating: 1
*/ */
int bitXor(int x, int y) { int bitXor(int x, int y) {
return x & ~y | ~x & y; return ~((~(x & ~y)) & (~(~x & y)));
} }
/* /*
* isTmin - returns 1 if x is the minimum, two's complement number, * isTmin - returns 1 if x is the minimum, two's complement number,
@@ -196,8 +196,8 @@ int bitXor(int x, int y) {
* Max ops: 10 * Max ops: 10
* Rating: 1 * Rating: 1
*/ */
int isTmin(int x) { volatile int isTmin(int x) {
return !!x & !(x ^ (~x + 1)); return (!(!x)) & (!(((~(x) + 1) ^ x)));
} }
//2 //2
/* /*
@@ -208,7 +208,7 @@ int isTmin(int x) {
* Rating: 2 * Rating: 2
*/ */
int isEqual(int x, int y) { int isEqual(int x, int y) {
return !(x ^ y);
} }
/* /*
* negate - return -x * negate - return -x
@@ -229,7 +229,7 @@ int negate(int x) {
* Rating: 2 * Rating: 2
*/ */
int getByte(int x, int n) { int getByte(int x, int n) {
return 2; return (x >> (n << 3)) & 0xFF;
} }
//3 //3
/* /*
@@ -240,7 +240,7 @@ int getByte(int x, int n) {
* Rating: 3 * Rating: 3
*/ */
int isLess(int x, int y) { int isLess(int x, int y) {
return 2; return ((x >> 31) & 1 & ~((y >> 31) & 1)) | (~(((x >> 31) & 1) ^ ((y >> 31) & 1)) & (((x + (~y + 1)) >> 31) & 1));
} }
/* /*
* conditional - same as x ? y : z * conditional - same as x ? y : z
@@ -250,7 +250,7 @@ int isLess(int x, int y) {
* Rating: 3 * Rating: 3
*/ */
int conditional(int x, int y, int z) { int conditional(int x, int y, int z) {
return 2; return (((x | (~x + 1)) >> 31) & y) | (~((x | (~x + 1)) >> 31) & z);
} }
//4 //4
/* /*
@@ -265,7 +265,7 @@ int conditional(int x, int y, int z) {
* Rating: 4 * Rating: 4
*/ */
unsigned floatScale2(unsigned uf) { unsigned floatScale2(unsigned uf) {
return 2; return (((uf >> 23) & 0xFF) == 0xFF) ? uf : ((((uf >> 23) & 0xFF) == 0) ? ((uf << 1) | (uf & (1 << 31))) : (uf + (1 << 23)));
} }
/* /*
* floatFloat2Int - Return bit-level equivalent of expression (int) f * floatFloat2Int - Return bit-level equivalent of expression (int) f
@@ -280,5 +280,31 @@ unsigned floatScale2(unsigned uf) {
* Rating: 4 * Rating: 4
*/ */
int floatFloat2Int(unsigned uf) { int floatFloat2Int(unsigned uf) {
return 2; unsigned exp, sign, frac, m;
int value, shift;
exp = (uf >> 23) & ((1 << 8) - 1);
sign = (uf >> 31) & 1;
frac = uf & ((1 << 23) - 1);
if (exp >= 158) {
return 1 << 31;
}
if (exp < 127) {
return 0;
}
shift = exp - 150;
m = ((1 << 23) | frac);
if (shift > 0) {
value = m << shift;
} else {
value = m >> -shift;
}
if (sign) {
return -value;
} else {
return value;
}
} }