-
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("%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 */
if (value & bitmask(i))
{
bit = '1';
}
putchar(bit);
}
putchar('\n');
}
Don't forget to include stdio.h.
-
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.
-
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:
#define bitmask(i) (1U << i)
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?
-
Shift 1 left i times. Shifting is an essential part of converting between bases.
-
-
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.
-
Quote:
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.
-
yes, my assignment here is actually to create a code that will extract from a floating point number to it's mantissa and exponent
-
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.
-
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.
-
Then go ahead and use binary operator AND to extract them.
-
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??
-
Btw, you're describing a float, not a double.
-
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
-
yes I want the actual mantissa and exponent from a floating point number