Solved

float to binary

Posted on 2003-11-01
20
4,216 Views
Last Modified: 2013-11-15
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 );


}
0
Comment
Question by:erenozkan
20 Comments
 
LVL 5

Accepted Solution

by:
snehanshu earned 125 total points
ID: 9662651
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9662674
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9662682
Sorry, you cannot use << for floats. my mistake.
...Snehanshu
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9662694
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9663445
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
 
LVL 15

Expert Comment

by:efn
ID: 9664623
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
 

Author Comment

by:erenozkan
ID: 9665324
hello guys, snehashu

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

error:
& only for int

message created
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9665595
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Expert Comment

by:snehanshu
ID: 9665618
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
 

Expert Comment

by:hermesc
ID: 9665777

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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9665930
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
 

Expert Comment

by:hermesc
ID: 9666371
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9667921
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 10415488
Looks like the asker lost his original ID details.
Please help him finalize.
...Shu
0
 

Expert Comment

by:hermesc
ID: 10418798
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 10524092
>>Please leave any comments here within the next four days.
OK, my comment:
Thank you sunnycoder :-)
0
 

Expert Comment

by:hermesc
ID: 10524792
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A list of useful business intelligence software.
Let’s list some of the technologies that enable smooth teleworking. 
The viewer will learn how to create multiple layers to apply various filters and how to delete areas from each layer’s filter.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now