# Thread: how to convert decimal to floating point number

1. Still not sure what you're on about, but it's just bits. You can set any of them. Or you zero them out, both as you like. You can hide some bits in there if you want, of course, but they'll also be part of the number.

2. Originally Posted by -EquinoX-
the 1. x x x x x x

where 1 is the hidden bit
Do you mean the sign bit? If you've already extracted it, then you know whether the number is supposed to be positive or negative. You can then multiply appropriately.

Edit: or do you mean the implicit 1 in front of the mantissa? It's not stored with the number (hence implicit), so you can't extract it. The way to check if it's supposed to be there is to extract the exponent: if the exponent is 0, the number is denormalized so there's no implicit 1.xxx, otherwise there is.

3. is there anyway to append bits in C?? because the exponent is located between bits 23-30 and I need to get that bits and transformed it into hexadecimal.. how would I do this?

and how would I move one bit to the other?? if I add one it will add an integer 1 not a bit

4. You can convert it or print it as hex.
printf("&#37;x", mantissa)
For example.

You move bits using shift >> and <<. As in my code I posted.
You can set bits using binary OR.
You can zero bits using a combination of binary AND and XOR:
Code:
```char c = 0xFF;
c & ~(0x8); // Zero fifth bit```

5. here's the code to find the exponent, is this right??

Code:
```#include <stdio.h>
#define SIGN_BIT (1U << 31)
#define EXPONENT (11111111U << 31)

int main()
{
float first = 56.43;
int *second;
unsigned int sign;
unsigned int mantissa;

second = (int *) &first;

sign = (*second) & SIGN_BIT; /* note one ampersand for bitwise-and */
sign = sign >> 31;

mantissa = ((*second) << 1)  & EXPONENT;
mantissa = mantissa >> 23;

printf("&#37;u\n", sign);
printf("%u\n", mantissa);

return 0;
}```

6. Exponent is correct. Mantissa is way off.
I'll help you, though. The easiest way to construct a proper mask to use is first to write down, in binary, the entire mask. I'll take the sign bit as example:
10000000000000000000000000000000 (31th bit signed, rest 0).
Use Windows Calculator (you are using Windows, I hope?), select scientific mode, select binary, past your mask there.
Doing that gives me 80000000.
So I do nTemp & 0x80000000 and I get the sign bit. The same hold true for the other parts as well.
Then shift into position.

7. oh I am sorry, I meant exponent here

8. 11111111U is not eight consecutive bits. If you want eight consecutive bits, the easiest way (I think) is to use hex notation: 0xff. Also note that you don't want to shift 31 places, because the exponent is not 31 places away from where you start.

9. And exponent is also way off, as tabstop notes. I find it easiest to just construct the mask directly instead of shifting, but it's up to you. You could also just construct a mask from 1111 1111 and shift it into place.

10. so is this right now:

Code:
```#include <stdio.h>

int main()
{
float first = -10.000000;
int *second;
unsigned int sign;
unsigned int exponent;
unsigned int mantissa;
second = (int *) &first;

sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */
sign = sign >> 31;

exponent = ((*second) << 1) & 0xff000000;
exponent = exponent >> 23;

mantissa = ((*second) << 9) & 0xfffffe00;
mantissa = mantissa >> 9;

printf("&#37;u\n", sign);
printf("%u\n", exponent);
printf("%x\n", mantissa);

return 0;
}```

11. Originally Posted by -EquinoX-
so is this right now:

Code:
```#include <stdio.h>

int main()
{
float first = -10.000000;
int *second;
unsigned int sign;
unsigned int exponent;
unsigned int mantissa;
second = (int *) &first;

sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */
sign = sign >> 31;

exponent = ((*second) << 1) & 0xff000000;
exponent = exponent >> 23;

mantissa = ((*second) << 9) & 0xfffffe00;
mantissa = mantissa >> 9;

printf("%u\n", sign);
printf("%u\n", exponent);
printf("%x\n", mantissa);

return 0;
}```
You don't ever ever ever ever ever ever ever ever ever ever want to shift *second.

As to your bit masks, do you know how to convert binary to hex? Let me take a completely arbitrary example: let's suppose I want to use the bitmask 11000110001100011000110001100011. I would break it up into groups of four:
1100 0110 0011 0001 1000 1100 0110 0011. I can then turn each group of four into a single hex digit: 12 6 3 1 8 12 6 3 -> 0xc6318c63. So write down your bitmask for exponent and mantissa and convert it to hex.

12. oh so we don't want to shift second?

13. Exponent is right. You did that excellent.
Mantissa is correct, by chance I believe.
Exponent is not correct.
You don't need to shift the actual number. Shift the mask or construct the correct mask in the first place.

Originally Posted by tabstop
So write down your bitmask for exponent and mantissa and convert it to hex.
It's far easier to just use a calculator to do the job. And there's nothing wrong with it, either.
Windows provides an excellent tool for this. I'm sure other OSes do too.

Originally Posted by -EquinoX-
oh so we don't want to shift second?
I'd avoid it, if I were you. You're essentially destroying original data by doing that.
In this case, you can get away with it, but next time you might not be so lucky.

14. I've revised the code so that I won't have to change second, let me know if I messed up.

Code:
```#include <stdio.h>

int main()
{
float first = -10;
int *second;
unsigned int sign;
unsigned int exponent;
unsigned int mantissa;
second = (int *) &first;

sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */
sign = sign >> 31;

exponent = (*second) & 0x7F800000;
exponent = exponent << 1
exponent = exponent >> 23;

mantissa = (*second) & 0x7FFFFF;
mantissa = mantissa << 9;
mantissa = mantissa >> 9;

printf("&#37;u\n", sign);
printf("%u\n", exponent);
printf("%x\n", mantissa);

return 0;
}```
and how do I directly eliminate the excess -127 in the exponent??

15. Well done. You have the right mask, but you need to stop needlessly shifting around the numbers.
Shift them right so that the first bit in the number becomes the 0th bit in the whole number.
Shift it into the right place and you don't have to worry about "excess" data. It will be as it should be.