Question

**Finish buildFloat():**

It does three things:

- Given exponent it computes exponentPattern.
- If exponent is less than or equal to DENORMALIZED_POWER_OF_2 then exponentPattern should be EXPONENT_DENORMALIZED_BIT_PATTERN. (Alternatively, if the hidden bit is off in the mantissa then the number is denormalize and exponentPattern should be EXPONENT_DENORMALIZED_BIT_PATTERN.)
- If exponent is greater than or equal to INFINITE_POWER_OF_2 then exponentPattern should be EXPONENT_INFINITE_BIT_PATTERN.
- Otherwise, compute exponentPattern from exponent and EXPONENT_BIAS.

- Turn off the hidden bit in mantissa. (If you are clever you can turn MANTISSA_HIDDEN_BIT into a mask to remove just that one bit.)
- Reconstitute the floating point number into number by combining exponentPattern shifted into the proper position with mantissa shifted into the proper position.

It returns number.

// PURPOSE: To return the float with exponent 'exponent' and mantissa-field // 'mantissa'.

float buildFloat (int exponent, unsigned int mantissa )

{

unsigned int exponentPattern;

float number;

// YOUR CODE HERE to compute exponentPattern

// YOUR CODE HERE to turn off the hidden bit in the mantissa

*((unsigned int*)&number) = 0; // CHANGE THAT 0!

// It should be a bitwise integer expression

// combining exponentPattern and mantissa return 0; }

Answer #1

you have not mentioned any signed bit parameter in buildFloat function so I assume that you are using +ve no, you can change it var.raw.sign to -1 if required.

If you want anything else please comment.

```
#include <math.h>
#include <stdio.h>
float buildFloat (int exponent, unsigned int mantissa );
typedef union {
float f;
struct
{
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} raw;
} myfloat;
unsigned int convertToInt(int* arr, int low, int high)
{
unsigned f = 0, i;
for (i = high; i >= low; i--) {
f = f + arr[i] * pow(2, high - i);
}
return f;
}
int main()
{
buildFloat(128,1048576); for 2.250000 repesentation is 0 | 10000000 | 00100000000000000000000 0 | 128 |1048576
}
float buildFloat (int exponent, unsigned int mantissa ){
float number;
myfloat var;
var.raw.mantissa = mantissa;
var.raw.exponent = exponent;
var.raw.sign = 0; // make it -1 for negative no..
printf("The float value in"
" IEEE-754 representation is : \n");
printf("%f", var.f);
}
```

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 3 minutes ago

asked 23 minutes ago

asked 50 minutes ago

asked 54 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago