Link to home
Start Free TrialLog in
Avatar of erenozkan
erenozkan

asked on

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


}
ASKER CERTIFIED SOLUTION
Avatar of snehanshu
snehanshu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snehanshu
snehanshu

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
Sorry, you cannot use << for floats. my mistake.
...Snehanshu
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
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
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
Avatar of erenozkan

ASKER

hello guys, snehashu

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

error:
& only for int

message created
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
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

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




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:
https://www.experts-exchange.com/help/closing.jsp
Looks like the asker lost his original ID details.
Please help him finalize.
...Shu
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
>>Please leave any comments here within the next four days.
OK, my comment:
Thank you sunnycoder :-)
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