Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# float to binary

Posted on 2003-11-01
Medium Priority
4,271 Views
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
Question by:erenozkan
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 5

Accepted Solution

snehanshu earned 500 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

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

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

LVL 5

Expert Comment

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

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

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

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

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

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

LVL 5

Expert Comment

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

ID: 9665777

'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

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

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

ID: 9667921
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
0

LVL 5

Expert Comment

ID: 10415488
Looks like the asker lost his original ID details.
...Shu
0

Expert Comment

ID: 10418798
( I have no right th grade the question ) Anyway, snehanshu should earn  the points .

Best Regards
0

LVL 5

Expert Comment

ID: 10524092
OK, my comment:
Thank you sunnycoder :-)
0

Expert Comment

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifiâ€¦
Developer portfolios can be a bit of an enigmaâ€”how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, itâ€™s also a statement of how you work.
This video demonstrates basic masking and how to edit the mask to reveal the desired image.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seveâ€¦
###### Suggested Courses
Course of the Month9 days, 16 hours left to enroll