please explain in detail in c++ /* * float_neg - Return bit-level equivalent of
ID: 3750076 • Letter: P
Question
please explain in detail
in c++
/*
* 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) {
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) {
return 2;
}
Explanation / Answer
Below is your code..
unsigned float_neg(unsigned uf) {
//change sign bit of the value. that's all.
unsigned nonnan = (0x80000000 ^ uf);
//check "Exp" bits.
unsigned curry = 0xff << 23;
//if "Exp" bits are filled with 1 and "Frac" bits have at least one bit, it is NaN.
if(((curry & uf) == curry) && (uf & ((1<<23) + (~0)))) return uf;
return nonnan;
}
unsigned float_twice(unsigned uf) {
//uf = +-0 case.
if(uf==0 || uf == 0x80000000) return uf;
//NaN case.
if(((uf>>23) & 0xff) == 0xff) return uf;
//Tiny value, but non-zero case.
if(((uf>>23) & 0xff) == 0x00) {
return (uf & (1<<31)) | (uf<<1);
}
//Otherwise, Add 1 to exp value.
return uf + (1<<23);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.