update datalab01/bits.c

This commit is contained in:
2025-09-29 15:45:56 +09:00
parent 57a619ac7b
commit b189afd37a
2 changed files with 37 additions and 9 deletions

2
.gitattributes vendored
View File

@@ -1,3 +1,5 @@
* text=auto eol=lf
[attr]lfs-file filter=lfs diff=lfs merge=lfs -text
"*.pdf" lfs-file

View File

@@ -187,7 +187,7 @@ int bitAnd(int x, int y) {
* Rating: 1
*/
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,
@@ -196,8 +196,8 @@ int bitXor(int x, int y) {
* Max ops: 10
* Rating: 1
*/
int isTmin(int x) {
return !!x & !(x ^ (~x + 1));
volatile int isTmin(int x) {
return (!(!x)) & (!(((~(x) + 1) ^ x)));
}
//2
/*
@@ -208,7 +208,7 @@ int isTmin(int x) {
* Rating: 2
*/
int isEqual(int x, int y) {
return !(x ^ y);
}
/*
* negate - return -x
@@ -229,7 +229,7 @@ int negate(int x) {
* Rating: 2
*/
int getByte(int x, int n) {
return 2;
return (x >> (n << 3)) & 0xFF;
}
//3
/*
@@ -240,7 +240,7 @@ int getByte(int x, int n) {
* Rating: 3
*/
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
@@ -250,7 +250,7 @@ int isLess(int x, int y) {
* Rating: 3
*/
int conditional(int x, int y, int z) {
return 2;
return (((x | (~x + 1)) >> 31) & y) | (~((x | (~x + 1)) >> 31) & z);
}
//4
/*
@@ -265,7 +265,7 @@ int conditional(int x, int y, int z) {
* Rating: 4
*/
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
@@ -280,5 +280,31 @@ unsigned floatScale2(unsigned uf) {
* Rating: 4
*/
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;
}
}