# Thread: how to convert decimal to floating point number

1. so say that I have an unsigned integer :

unsigned int exponent = 126;
exponent = exponent - 127 will result -1?? I tried this and it gives me a really big number instead

2. Originally Posted by -EquinoX-
so say that I have an unsigned integer :

unsigned int exponent = 126;
exponent = exponent - 127 will result -1?? I tried this and it gives me a really big number instead
Keyword "unsigned" integer. That means that ALL unsigned integers are POSITIVE. Going less than 0 is an overflow(actually, that might not be the correct term. it's more of an underflow). It acts the same as if you add too much to a number except with subtraction.

3. okay and say that I want to convert the unsigned int to an int can I do that so that I will be able to do this?

4. Originally Posted by -EquinoX-
okay and say that I want to convert the unsigned int to an int can I do that so that I will be able to do this?
Yes. You can convert an unsigned int to signed. In fact, the operations (add, subtraction) are the same for unsigned and signed. So the result you get as far as the actual binary data stored in memory from the operation 0 - 1 on signed integers is the same as you would get with unsigned.
Code:
```unsigned int u = 0 - 5;
int i = 0 - 5;```
If you looked at the binary memory for both i and u, they would look identical. So the conversion from unsigned to sign is really just a matter of how the program interprets the memory.
Code:
```unsigned int u = 0 - 5;
int i = (int)u;```
i would now hold the value -5

5. okay just one more issue.. you know that a mantissa is always precedeed with the 1. 0100101001 and I can't anyway extract the 1 in front of the fraction because it's actually not inside the 1, how do I append the extra one to the 0100101001?? do I shift it right first and then do an or with 1000000000 (masking technique) ?? the problem is if I do this I would lose bit 0 from the mantissa (the right most bit in the mantissa). I need to append the string so that it becomes 24 bit..

6. Originally Posted by -EquinoX-
okay just one more issue.. you know that a mantissa is always precedeed with the 1. 0100101001 and I can't anyway extract the 1 in front of the fraction because it's actually not inside the 1, how do I append the extra one to the 0100101001?? do I shift it right first and then do an or with 1000000000 (masking technique) ?? the problem is if I do this I would lose bit 0 from the mantissa (the right most bit in the mantissa). I need to append the string so that it becomes 24 bit..
It's not there. So when you print, just print a "1." first, and then print the actual number.

7. well no, after I got the one I would like to convert all that together into a hex

8. A possible solution....

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

void PrintTheBits(long lInput, int iStart, int iEnd)
{
unsigned int uiMask = 1 << iStart;
unsigned int uiEndMask = 1 << iEnd;
{
putchar ((uiMask & lInput) ? '1' : '0');
}
}

int main(void)
{
float fInput = 56.43f;
long  lLongValue;
lLongValue = *((long *)(&fInput));
PrintTheBits (lLongValue, 31, 31);
putchar ('.');
PrintTheBits (lLongValue, 30, 23);
putchar ('.');
PrintTheBits (lLongValue, 22,  0);
putchar ('\n');
return 0;
}```

9. Originally Posted by -EquinoX-
well no, after I got the one I would like to convert all that together into a hex
If you want to print the bits that are there then print the bits that are there as a hex integer using %x, which will print as hex.

If you want to print "what it means", then remember: the bits that you have come after the binary point, and the implicit one comes before, so it's really "1."001010011100 or whatever. (You can also print that in hex as 0x1.29c or so.)

There is no context in which you would want to add the implicit one as a bit in front and then print the result as a hex integer.