# Thread: how to convert decimal to floating point number

1. So what have you got so far as to your code?

--
Mats

2. The bits are already stored in the number. Otherwise machines would not be able to understand it. To extract the specific values you would use both bitwise & (AND) and binary shifts (<<, >>).
You would have to construct a mask to isolate the parts of the number that form the mantissa and the exponent.
yes I know how floating points are layed out in binary the 31st bit is the sign bit and 23-31 is the mantissa and 0-23 is the exponent.
Wrong here, the 32nd bit is the exponent but bits are indexed 0..31 in a 32 bit number.
[31 SIGN][30-23 EXPONENT][22-0 MANTISSA]
Now for the masking part. A mask is a series of 1111 in a machine word usually positioned at the right place so that when AND'ed with a number they zero out all the bits we don't care about.
If you want the sign bit, where should the 1's be in a 32 bit unsigned integer mask and how many of them also?
If you can answer that question for the EXPONENT and the MANTISSA i might guide you to code it in C.

3. okay let me get this clear first, say I have a float in C which is 56.43 the 56.43 are already stored in the computer and all I need to do is to extract them by the masking techniques right??

so for a 32 bit if I want the sign bit it should be on the 31 bit?

4. Yes, it would. The data is always stored raw in memory, but is represented as something else depending on the type.
So all you do is extract the appropriate bits.

5. okay I get it, so therefore I don't have to convert the floating point to a binary first and then find the mantissa and so on. I just have to extract the right one because it's already layed out that way in the memory?

6. Originally Posted by -EquinoX-
okay let me get this clear first, say I have a float in C which is 56.43 the 56.43 are already stored in the computer and all I need to do is to extract them by the masking techniques right??

so for a 32 bit if I want the sign bit it should be on the 31 bit?

If you use 56.43, you should get the result sign: 0, exponent 0x84 (132 decimal), and 0x61b852 as the mantissa.

--
Mats

7. okay so I got the idea now, how do I do the AND sign at?? do I have to find the location of the memory where 56.43 is stored?? using pointers?? 56.43 is a stored in memory as a 32 bits right??

8. Originally Posted by -EquinoX-
okay so I got the idea now, how do I do the AND sign at?? do I have to find the location of the memory where 56.43 is stored?? using pointers?? 56.43 is a stored in memory as a 32 bits right??
If it's a float, yes. And one way to get to the float is by pointer, another solution is to overlay it with a union of integer and float.

Also, you can avoid the AND-business altogether and use a bitfield struct - although that will make it more compiler dependant.

--
Mats

9. pointer seems to be a great idea say that I do this in C:

float number = 56.87

int* point = &number

then point will point to the address of the sign bit of the float?

10. You probably need a cast to make the compiler happy, but yes, that's the idea.

--
Mats

11. I just wrote this simple code:

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

int main()
{
float number = 56.73;
int* sign_pointer = (int*) &number;
int temp = *sign_pointer;

printf("&#37;p\n", sign_pointer);
printf("%d\n", temp);
return 0;
}```
I am trying to get the sign bit here, but somehow it gets a very large number

12. Of course, you're just putting the whole data into temp.
This is what -56.43 is in binary (sign, exponent, mantissa):
1 10000100 11000011011100001010010
So number containers
1 10000100 11000011011100001010010
Then the value of number is assigned to temp, so temp becomes
1 10000100 11000011011100001010010

You need to use binary and to extract the bits you want. Then you need to shift the bits into the correct position to get a valid result.
Btw, consider using a negative number. So when you get 1, you know that you got the right bit, since the sign bit is set when the number is negative.

13. could you give me an example of using this and binary to extract the bits in C code, because I don't quite get it. an example might really help.. thanks

14. Binary AND works with a mask.
It's simple, really...
The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
Quote from MSDN.

So say you have this
Code:
```char c = 0xFF;
char c2 = c & mask;```
This sample will extract the 5th bit. But if you want to use it, you want it in the correct position, because the number will be 0x8, not 0 or 1.
Code:
`c2 >>= 4;`
This code shifts the number right 4 times, back into its rightful position as bit 0. Thus it will be 0 or 1.

15. Why use a pointer? The general rule for working with bits is that unsigned types are the best to use even though it really doesn't matter, it is much better when viewed hexadecimally.
Code:
```    float number = 56.73f;
unsigned int sign_mask = 0x80000000;  /* Bit 31 */
/* Alternative option */
unsigned int sign_mask = (1U << 31);  /* The same as above */
unsigned int sign_bit;