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 );


}
erenozkanAsked:
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.

snehanshuCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
snehanshuCommented:
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
0
snehanshuCommented:
Sorry, you cannot use << for floats. my mistake.
...Snehanshu
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

snehanshuCommented:
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
0
snehanshuCommented:
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
0
efnCommented:
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
0
erenozkanAuthor Commented:
hello guys, snehashu

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

error:
& only for int

message created
0
snehanshuCommented:
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
     unsigned char mymask = 1;

     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
0
snehanshuCommented:
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
0
hermescCommented:

C:\Program Files\Miracle C\HW2EX.c: line 9: variable 'arg1' not found
'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
0
snehanshuCommented:
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
0
hermescCommented:
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 )




0
snehanshuCommented:
hermesc,
  First let me know whether or not you were able to compile and run the code.
  Did you add
#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
0
snehanshuCommented:
Looks like the asker lost his original ID details.
Please help him finalize.
...Shu
0
hermescCommented:
Hello,  erenozkan was my old account  and I made it close by the administraters and I totally forgot this page .
( I have no right th grade the question ) Anyway, snehanshu should earn  the points .

Best Regards
0
snehanshuCommented:
>>Please leave any comments here within the next four days.
OK, my comment:
Thank you sunnycoder :-)
0
hermescCommented:
hello ! I have added a comment , this was my old account and I made it close , so I cant reach my account.As I have stated above points should be given to snehanshu.
Sorry for the inconvenience
Regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.