Floating point number to binary

K

Kenneth Brody

Gaurav said:
Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?

float MyNum = 123.456;
 
W

Walter

Gaurav Verma said:
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?

Here's a simple program that does it for Digital Mars C:

#include <stdio.h>

int main()
{
double d = 123.456;
printf("%032b %032b\n", ((int*)&d)[0], ((int*)&d)[1]);
return 0;
}

which prints:

00011010100111111011111001110111 01000000010111101101110100101111

Of course, it relies on some non-standard behavior, like the %b format for
binary, it assumes that sizeof(double)==2*sizeof(int)==64 bits, and there
are endianness issues. But this should help in seeing how it might be done.

-Walter
www.digitalmars.com free C/C++/D compilers
 
J

jmh

Gaurav said:
Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?

Thanks
Gaurav

One approach wuold be to split the number into it's integer
and it's fractional parts, use the division-remainder method
to convert the integer part to a string representatin of the
binary value (i.e., case all the 1s and 0s as char and then
contatinate them) they apply the mulitple-remainder (is that
what it's called?) method to the frational part and do the
same casts, then concatinate the two parts together.


jmh
 
P

pete

Nicolas said:
unsigned char comp_mask = 0x1 << ((sizeof(char) * 8) -1);
char* bin_rep = malloc(sizeof(char) * sizeof(char) * 8 + 1);

Those statements won't do what you want, unless CHAR_BIT equals 8.
sizeof(char) always equals 1, so it's a little strange to use
sizeof(char) as a multiplicand in an expression.
If 8 is replaced by CHAR_BIT, then the 0x1 expression, would
also have to be replaced by one with an unsigned type, like 1u.
 
N

Nicolas Pavlidis

pete said:
Those statements won't do what you want, unless CHAR_BIT equals 8.
sizeof(char) always equals 1, so it's a little strange to use
sizeof(char) as a multiplicand in an expression.
If 8 is replaced by CHAR_BIT, then the 0x1 expression, would
also have to be replaced by one with an unsigned type, like 1u.

It was code I wrote for some assignement on a university course.
At that time I didn't know that sizeof(char) == 1, on all platforms,
CHAR_BIT would be better here.

But I had no problems on 32bit Linux.

Kind regards,
Nicolas
 
M

Mark McIntyre

Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?

It will almost certainly be pointed out to you by many people that all
numbers in C are already in binary format (unless you're one of the rare
people with a nonbinary computer).

I expect you mean you want to print out the bitpattern of a floating point
value. Thats not really a C question, its a question about how to translate
the floating point format for your hardware.

Once you know, that, simply iterate over the bits of the number, printing a
1 for each set bit and a 0 for each unset bit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top