As part of the first big coding project for my coding class, an intro to C, the following question was asked, do note the restrictions:
/*
* Neg_Float - 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 operations: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 8
* Rating: 2
*/
Here is my code so far:
unsigned Neg_Float(unsigned uf) {
unsigned exp = (uf >> 23) & 0xff;
if (exp == 0xff)
return uf; //NaN
else {
return (uf ^ 0x80000000);
}
}
When I ran this piece of code through the checking software, I got this error:
ERROR: Test Neg_Float(0[0x0]) failed...
...Gives 0[0x0]. Should be -2147483648[0x80000000]
The purpose of this assignment is to learn floating point and bit manipulation. So I would appreciate if you would explain where I went wrong, and how I can fix it.
Also, yes I'm aware, that there is pretty much the same question in another post that's like 10 years old, but it doesn't answer the question at all.