# how to convert decimal to floating point number

This is a discussion on how to convert decimal to floating point number within the C Programming forums, part of the General Programming Boards category; Ah, but this is C99 or C++. Here should be C89 compatible code: Code: #define bitmask(i) (1U << i) int ...

1. Ah, but this is C99 or C++.
Here should be C89 compatible code:
Code:
```#define bitmask(i) (1U << i)

int main(void)
{
/* The number of bits in an int */
const int intSize = 31;

/* The decimal number to convert */
int value;
int i;

char bit = '0';

printf("Please enter a decimal number to be converted: ");
scanf("&#37;d", &value);

/* Print the bits of the decimal value */
for (i = intSize; i >= 0; --i)
{
/* Assume bitmask(i) is unset */
bit = '0';

/* Pick the character to print */
{
bit = '1';
}

putchar(bit);
}

putchar('\n');
}```
Don't forget to include stdio.h.

2. Oh, I am guessing that it does not recognise the // comments. Change them to the /* */ style comments and see what happens, or maybe specify std=c99.

3. oh it works now.. I guess it's just the comments using // that doesn't work in my compiler, but I was able to fix it now..

I don't however understand this part:

can someone explain this to me??

and one more thing the code only works for integer numbers how about if I have a double value that I want to convert such as 1234.83?

4. Shift 1 left i times. Shifting is an essential part of converting between bases.

5. how do I shift in C?

6. n >> 2;
Shift n right 2 times.
n << 2;
Shift n left 2 times.
N is the variable you wish to shift and 2 is the number of times you wish to shift, so replace them as appropriate.

Well, I'd stay away from trying to convert floating points because they aren't represented as numbers, unlike integers.
You could modify the example to work with doubles, but you wouldn't get a sane output.

7. how do I shift in C?
<< shifts bits left and >> shifts bits right.

00000001 left shifted by 3 is 00001000. 00001000 right shifted by 3 is back to 00000001. But make sure that you only shift by a nonnegative amount and that the number you're shifting is unsigned. That way you don't have any weird problems.

8. yes, my assignment here is actually to create a code that will extract from a floating point number to it's mantissa and exponent

9. Well, that's an entirely different thing. That is not binary. And so you know, a number cannot be represented as binary. It must be a string or you would get weird results.
But I suggest you take a look at how floating points layout are, in binary terms. That's the first step.
Then you can extract them using binary operators. Specifically and.

10. 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.

11. Then go ahead and use binary operator AND to extract them.

12. and now lets use an example to do this, say I have a number −118.625 which is an IEEE floating point number and I want to convert that to the mantissa, sign bit, and exponent. First I know that the sign bit is 1 because it's negative and now the number.. this is where I get confused, especially doing this in C because 118.625 should be converted first into it's binary value then normalized and how would I do this??

13. Btw, you're describing a float, not a double.

14. So, do you want the actaul mantissa and exponent?

if so, you need to get the float to overlay an integer of same length, then use AND to get the portions out.

If you want something else, please clarify what you actually want.

--
Mats

15. yes I want the actual mantissa and exponent from a floating point number

Page 2 of 7 First 1234567 Last