C programing extract sign function for IEEE floating point value #ifdef HALF typ
ID: 3744139 • Letter: C
Question
C programing extract sign function for IEEE floating point value
#ifdef HALF
typedef short iFloat_t;
#define BITS 16
#define BITS_EXP 5
#define BITS_MANT 10
#define EXP_BIAS 15
#else
typedef int iFloat_t;
#define BITS 32
#define BITS_EXP 8
#define BITS_MANT 23
#define EXP_BIAS 127
#endif
/** Extract the sign of the argument.
* @param x the integer containing an IEEE floating point value
* @return 0 if the value is 0 or positive, 1 if it is negative
*/
iFloat_t floatGetSign (iFloat_t x) {
return 0; /* implement this */
}
Explanation / Answer
Given below is the completed code.
/** Extract the sign of the argument.
* @param x the integer containing an IEEE floating point value
* @return 0 if the value is 0 or positive, 1 if it is negative
*/
iFloat_t floatGetSign (iFloat_t x) {
//right shift number 1 to right most bit (i.e position BITS - 1) and perform AND with x to extract the sign bit of x
iFloat_t val = x & (1 << (BITS - 1));
if(val == 0)
return 0;
else
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.