# float to binary

hello guys this is my program and this simply converts
int to binary however I could not understand

printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );

the 1 near arg1 , what is this 1 doing there ...

my other question is can we modify this into float - binary converter?

#include <stdio.h>
#include <stdlib.h>

void main( int argc, char *argv[] )
{
int arg1, x;

if( argc == 1 )
{
puts( "BINARY num = displays num in binary form." );
exit( 1 );
}

arg1 = atoi( argv[1] );
printf( "INTEGER: %i\n", arg1 );
printf( " BINARY:" );
printf( "  " );

for( x = 7; x > -1; x-- )
printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );

}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
erenozkan,

<< is the binary shift operator

arg1 & 1 << x
gives
arg1 & (1 << x)
(1 << x) means shift 1 (Binary 00000001) to the left by x positions.
in effect, you are anding arg1 by
00000001 SHL 7 = 10000000 when x=7
00000001 SHL 6 = 10000000 when x=6
...
00000001 SHL 0 = 10000000 when x=0
to get the desired output.

Hope that helps,
...Snehanshu

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
A couple of more things:

arg1 & 1 << x becomes arg1 & (1 << x) because << (Left Shift) has higher precidence than & (Bitwise And)

A float is 4 bytes long. So, the for loop has to be of 32. So, to convert float, use:

long int myshift;
for( x = 31; x > -1; x-- )
{
myshift = myshift << x;
printf( " %i ", (arg1 & myshift) > 0 ? 1 : 0 );
}

I haven't tested the float thing, but I hope it would work :-)
...Snehanshu
Commented:
Sorry, you cannot use << for floats. my mistake.
...Snehanshu
Commented:
And,
00000001 SHL 7 = 10000000 when x=7
00000001 SHL 6 = 01000000 when x=6
...
00000001 SHL 0 = 00000001 when x=0

Sorry for creating the confusion: I am watching a cricket match actually :-)
...Snehanshu
Commented:
This would convert float to binary:

void main( int argc, char *argv[] )
{
union myfloat
{
char mybytes[4];
float myval;
};
int x, y;
myfloat arg1;

if( argc == 1 )
{
puts( "BINARY num = displays num in binary form." );
exit( 1 );
}

arg1.myval = atof( argv[1] );
printf( "FLOAT: %f\n", arg1.myval );
printf( " BINARY:" );
printf( "  " );

for (y=0;y<4;y++)//loop for each byte of float
{
for( x = 7; x > -1; x-- )
printf( " %i ", (arg1.mybytes[y] & 1 << x ) > 0 ? 1 : 0 );
}

}

Explanation:
The union myfloat allows you to access individual butes of the float using mybytes array. Then, you could simply display the four bytes using your existing routines. There may be other methods I am not aware of, but this one would work.
A union is a way to access the same memory location in different ways (as different data types).
...Snehanshu
Commented:
Floating-point numbers are typically stored in a format defined by IEEE Standard 754.  A number consists of a sign bit, an exponent, and a mantissa.  For details, see:

http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html

Converting a floating-point number to binary representation could mean two different things.  It could mean showing the bits in the machine representation of the number, or it could mean showing the value of the number in the binary number system.  If the first one is what you want, Snehanshu has shown you how to do it.  If the second one is what you want and you say so, somebody here can probably advise you.  It would not be a simple change from your integer conversion function, but it certainly can be done.

--efn
Author Commented:
hello guys, snehashu

printf( " %i ", (arg1.mybytes[y] & 1 << x ) > 0 ? 1 : 0 );

error:
& only for int

message created
Commented:
erenozkan,
The lowest version C compiler I have is Visual Studio 2002 .net and I have no clue how to compile the good old c programs in it. So, here's a suggestion without actually testing, I hope it would work (BTW, what are you compiling this on? Unix/Linux? or Dos/Win?):

void main( int argc, char *argv[] )
{
union myfloat
{
unsigned char mybytes[4];//it shouldn't matter, but still
float myval;
};
int x, y;
myfloat arg1;
//old c compilers perhaps need same data types for bit-wise operations

if( argc == 1 )
{
puts( "BINARY num = displays num in binary form." );
exit( 1 );
}

arg1.myval = atof( argv[1] );
printf( "FLOAT: %f\n", arg1.myval );
printf( " BINARY:" );
printf( "  " );

for (y=0;y<4;y++)//loop for each byte of float
{
for( x = 7; x > -1; x-- )
printf( " %i ", (arg1.mybytes[y] & mymask << x ) > 0 ? 1 : 0 );
}

}

Hope that helps,
...Snehanshu
Commented:
And if the above didn't work, then try this
void main( int argc, char *argv[] )
{
union myfloat
{
unsigned int mybytes[2];
float myval;
};
int x, y;
myfloat arg1;

if( argc == 1 )
{
puts( "BINARY num = displays num in binary form." );
exit( 1 );
}

arg1.myval = atof( argv[1] );
printf( "FLOAT: %f\n", arg1.myval );
printf( " BINARY:" );
printf( "  " );

for (y=0;y<2;y++)//loop for each byte of float
{
//this is what should have been done for integer to binary also (loop of 15, not 7)
for( x = 15; x > -1; x-- )
printf( " %i ", (arg1.mybytes[y] & 1 << x ) > 0 ? 1 : 0 );
}

}

...Snehanshu
Commented:

'arg1'
aborting compile

still there is error ... so you advise me to use visual...

can you send your e-mail to krymyr@mynet.com

thanks
Commented:
Oops,
So, did I do homework for you?
In that case, please do try to understand what's being done rather than simply copying it.
No, I don't advise using Visual Studio: the programs above should work just fine.
hermesc, try harder: you might have missed something. Also, this is not the complete source code: a few statements are missing: I hope you can figure out which ones :-)
erenozkan, let me know if you still face any problems. I hope it's not you who has created another ID "hermesc".
Good luck,
...Snehanshu
Commented:
yes it is me erenozkan=hermesc but this is not due creating a lot of accounts for asking questions. I dont know simply how to cancel my erenozkan account.  Now, I am a premium member ( with hermesc ) .

No you have not done my homework ( because this is not  a homework )   :)) I just asked this in order to understand that << thing
and it was not a homework a part of my work. Anyway thanks for your help ( by the way I  dont know how to give points )

Commented:
hermesc,
First let me know whether or not you were able to compile and run the code.
#include <stdio.h>
#include <stdlib.h>
to the code I posted? And did you check that the code is exactly like the one posted? Like all the ;s and case etc. are the same?
And after all that, does it still not work? Then let me know what are you trying to compile with.
...Snehanshu
P.S.
To accept the answer, you need to login as erenozkan and press the accept button for the comment you thing was the correct answer. See:
http://www.experts-exchange.com/help/closing.jsp
Commented:
Looks like the asker lost his original ID details.
...Shu
Commented:
( I have no right th grade the question ) Anyway, snehanshu should earn  the points .

Best Regards
Commented: