# Thread: how to convert decimal to floating point number

1. Code:
```  mantissa = mantissa << 9;
mantissa = mantissa >> 9;```
In particular the above seems very pointless...

--
Mats

2. Code:
```exponent = exponent << 1
exponent = exponent >> 23;```
This is also wrong. Why would you shift the exponent left when you want it at the beginning, to the right? If you remove the left shift, it's correct.

3. okay I fixed all the errors I have, now how do I translated the mantissa into hexadecimals. like for instance if it's -10 then it should be 0xa00000 in the mantissa

4. Your bits are in the wrong position, that's why it seems you are getting "excess."
You can use my function for debugging:

Code:
```void PrintBits(uint32_t nNumber)
{
char Binary[sizeof(nNumber) * 8 + 1] = {0};
char Binary2[sizeof(nNumber) * 8 + 1] = {0};
for (int i = sizeof(nNumber) * 8 - 1; i >= 0; i--)
{
if (nNumber & (0x1 << i))
Binary[i] = '1';
else
Binary[i] = '0';
}
for (int i = sizeof(nNumber) * 8 - 1, j = 0; i >= 0; i--, j++) Binary2[j] = Binary[i];
for (int i = 0; i < sizeof(nNumber) * 8; i++)
{
if (i == 1) cout << " ";
else if (i == 1 + 8) cout << " ";
cout << Binary2[i];
}
cout << "\n";
}```
It will print all bits of a number. Make sure there are no "0" to the right-most, because if there it, you didn't shift it right.
Shift it all the way to the right. And be careful not to shift too much.

5. Originally Posted by matsp
Code:
```  mantissa = mantissa << 9;
mantissa = mantissa >> 9;```
In particular the above seems very pointless...

--
Mats
Depending on the size of the mantissa variable, it could potentially set some of the higher bits to zero. I doubt that is the intention though.

6. Originally Posted by -EquinoX-
okay I fixed all the errors I have, now how do I translated the mantissa into hexadecimals. like for instance if it's -10 then it should be 0xa00000 in the mantissa
No, for -10, you should get:

Mantissa 0x00200000
Exponent 0x0082
Sign 0x01

7. Originally Posted by brewbuck
Depending on the size of the mantissa variable, it could potentially set some of the higher bits to zero. I doubt that is the intention though.
But only necessary if there wasn't an AND on the line before that already had removed any "unwanted" bits, right?

--
Mats

8. I think what brewbuck referred to was if you extracted a number such as
1111 11111 0000 0000
Shift left 9 times and we get:
1000 0000 0000 0000
Shift right 9 times and we get:
0000 0001 0000 0000
Unintentionally destroyed data.
But it depends on the size. It it was 32-bit, obviously this wouldn't happen.

9. yes I am doing this on a 32 bit machine

10. I mean the size of the variable. My example was of a 16-bit variable.
But it goes to show that you should be careful about shifting the original data since you can unintentionally destroy parts of it.

11. Originally Posted by Elysia
Your bits are in the wrong position, that's why it seems you are getting "excess."
You can use my function for debugging:

Code:
```void PrintBits(uint32_t nNumber)
{
char Binary[sizeof(nNumber) * 8 + 1] = {0};
char Binary2[sizeof(nNumber) * 8 + 1] = {0};
for (int i = sizeof(nNumber) * 8 - 1; i >= 0; i--)
{
if (nNumber & (0x1 << i))
Binary[i] = '1';
else
Binary[i] = '0';
}
for (int i = sizeof(nNumber) * 8 - 1, j = 0; i >= 0; i--, j++) Binary2[j] = Binary[i];
for (int i = 0; i < sizeof(nNumber) * 8; i++)
{
if (i == 1) cout << " ";
else if (i == 1 + 8) cout << " ";
cout << Binary2[i];
}
cout << "\n";
}```
It will print all bits of a number. Make sure there are no "0" to the right-most, because if there it, you didn't shift it right.
Shift it all the way to the right. And be careful not to shift too much.

which one is wrong?? I am sure that not all is wrong right?

12. Well, as I mentioned, for the number -10.0, you should get:

Mantissa 0x00200000
Exponent 0x0082
Sign 0x01

If you don't, then there's a bug somewhere in the code. Look at the results you are getting and compare them to these. If they don't match, then they're wrong.

13. Originally Posted by -EquinoX-
Code:
```  int* sign_pointer = (int*) &number;
int temp = *sign_pointer;

printf("%p\n", sign_pointer);```
%p takes a void *.
Code:
`printf("%p\n", (void *) sign_pointer);`

14. as I said the hidden value, which is 1. x x x x x is included here

my other question is that say that I have an unsigned int 126 and I want to substract that with the int 127 so the result will be -1, how would I do this??

15. Originally Posted by -EquinoX-
as I said the hidden value, which is 1. x x x x x is included here

my other question is that say that I have an unsigned int 126 and I want to substract that with the int 127 so the result will be -1, how would I do this??
I usually use "-" to subtract things. Make sure that the result variable is signed. You shouldn't need to cast your unsigned variable, I don't think.