# Thread: how to convert decimal to floating point number

1. To get the sign bit (untested, so I may have done something stupid, apart from not actually writing main and all the rest):
Code:
```#define SIGN_BIT (1 << 31)
float first = -56.43;
int *second;
unsigned int sign;

second = (int *) &first;
sign = (*second) & SIGN_BIT; /* note one ampersand for bitwise-and */```

2. Originally Posted by xuftugulus
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.
Using binary AND or float is illegal. So is shifting.

Originally Posted by tabstop
To get the sign bit (untested, so I may have done something stupid, apart from not actually writing main and all the rest):
Hey now... are we supposed to spoil the solution?
Meh. Well, I figure you can get the mantissa and exponent yourself, -EquinoX-.
It's an assignment, after all.

3. Originally Posted by Elysia
Hey now... are we supposed to spoil the solution?
Meh. Well, I figure you can get the mantissa and exponent yourself, -EquinoX-.
It's an assignment, after all.
I figured that getting the mantissa was "different enough".

4. True enough.

5. Originally Posted by xuftugulus
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;

/* sign_mask is : 1000 0000 0000 0000 0000 0000 0000 0000
in binary therefore, sign_bit is either all zeros or like sign mask in binary,
which is a large number of course. */
printf("%#08x\n", sign_bit);```
Try this see what it outputs. Binary and hexademical arithmetic are not taught in school usually but they are not that tough to understand... Numbers are absolute quantities that can be represented in various ways.

error: invalid operands to binary &

6. You can't use binary & with floats. Your original solution works. Use an integer type:
float d = -56.43f;
uint32_t nTemp = *(uint32_t*)&d;

7. okay I tried another code:

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

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

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

printf("&#37;u\n", sign);
return 0;
}```
however it does not print a 1 or 0 instead of a very large number

8. Because there is a difference between what you have:
10000000000000000000000000000000
And how 1 or 0 is represented:
00000000000000000000000000000001
OR
10000000000000000000000000000000
Shift the bit into correct position.

9. so it should be 1U >> 31 then??

10. No, read again. After you use binary AND, your sign has the value of
10000000000000000000000000000000
So you need to shift that left-most bit so it becomes
00000000000000000000000000000001
So shift the variable sign.

The define SIGN_BIT is the mask. You use it to extract the bit you want, so you shouldn't modify that.

11. oh yeah I forgot about that, now I am going to try to create the code for the mantissa and exponent and post here if I have problems

12. I'll admit that I started fiddling with it, too. Though I've already solved it, I'll just show how I extracted the sign bit since you figured that one out already (it's fun! ):

Code:
```float d = -56.43f;
uint32_t nTemp = *(uint32_t*)&d;
uint8_t Sign = (nTemp & 0x80000000) >> (23 + 8);```
Good luck!

13. oh one more question, is there any way in C so that when I extract the mantissa I can have the hidden bit in the value??

14. What do you mean "hidden bit"?

15. the 1. x x x x x x

where 1 is the hidden bit