binary-float&int conversion

hello guys this is converting int to binary
my questions are :

1)
in printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
what is 1 doing with arg1?
2)
how can I modify this into a program which converts FLOAT to binary?
thanks


#include
#include

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
", arg1 );
printf( " BINARY:" );
printf( " " );

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


}


Comments

  • : hello guys this is converting int to binary
    : my questions are :
    :
    : 1)
    : in printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
    : what is 1 doing with arg1?


    The & operator is a bitwise operator, that is an operator which works on the bits.
    So, let's state arg1 is 125, that is, in binary 01111101.
    The & argument returns 1 if the 2 operands are both true, so:

    1 & 1 = 1
    1 & 0 = 0
    0 & 1 = 0
    0 & 0 = 0

    The other operator (<<) is the left-shift operator which shifts by x bit the bits of the number.

    So well'start with 125 & (1 << 7)
    1 >> 7 equals 128 (you shift the bites left 7 times so, 0000 0001 becomes 0000 0010 and then 0000 0100 and so on, until 1000 0000)
    125 & 128 equals 0 because:

    0111 1101 &
    1000 0000
    -----------
    0000 0000

    so it prints out 0

    Then you repeat the same thing with 1>>6 that is 64 (note that left shifting by 1 bit means multiplying by 2).

    0111 1101 &
    0100 0000
    -----------
    0100 0000

    125 & 64 equals 64, that is > 0, so it prints out 1

    125 & (1<<5) is 125 & 32 = 1

    And so on...
    So it will print out 01111101 !!!!

    : 2)
    : how can I modify this into a program which converts FLOAT to binary?
    : thanks

    I think you should first well understand integer conversion and bit operations before trying to use float numbers... anyhow if you want you can check on the net for the IEEE standard for representing float numbers in binary mode, but it's not so simple as for integers.

    nICO

    [hr]
    [italic]How beautiful, if Sorrow had not made sorrow more beautiful than Beauty itself.[/italic]
    JOHN KEATS


  • : : hello guys this is converting int to binary
    : : my questions are :
    : :
    : : 1)
    : : in printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
    : : what is 1 doing with arg1?
    :
    :
    : The & operator is a bitwise operator, that is an operator which works on the bits.
    : So, let's state arg1 is 125, that is, in binary 01111101.
    : The & argument returns 1 if the 2 operands are both true, so:
    :
    : 1 & 1 = 1
    : 1 & 0 = 0
    : 0 & 1 = 0
    : 0 & 0 = 0
    :
    : The other operator (<<) is the left-shift operator which shifts by x bit the bits of the number.
    :
    : So well'start with 125 & (1 << 7)
    : 1 >> 7 equals 128 (you shift the bites left 7 times so, 0000 0001 becomes 0000 0010 and then 0000 0100 and so on, until 1000 0000)
    : 125 & 128 equals 0 because:
    :
    : 0111 1101 &
    : 1000 0000
    : -----------
    : 0000 0000
    :
    : so it prints out 0
    :
    : Then you repeat the same thing with 1>>6 that is 64 (note that left shifting by 1 bit means multiplying by 2).
    :
    : 0111 1101 &
    : 0100 0000
    : -----------
    : 0100 0000
    :
    : 125 & 64 equals 64, that is > 0, so it prints out 1
    :
    : 125 & (1<<5) is 125 & 32 = 1
    :
    : And so on...
    : So it will print out 01111101 !!!!
    :
    : : 2)
    : : how can I modify this into a program which converts FLOAT to binary?
    : : thanks
    :
    : I think you should first well understand integer conversion and bit operations before trying to use float numbers... anyhow if you want you can check on the net for the IEEE standard for representing float numbers in binary mode, but it's not so simple as for integers.
    :
    : nICO
    :
    : [hr]
    : [italic]How beautiful, if Sorrow had not made sorrow more beautiful than Beauty itself.[/italic]
    : JOHN KEATS
    :
    :
    :
    You can convert a float to a binary representation if you deal with the integer and decimal part separately to for ex. convert 2.5 to 0010.1001.
    Separate the two parts into two separate integers. You have to decide how many decimals you want to use and round them correctly. You also have to output the decimal point yourself. You can then use the code you have to produce the binary representation.

    Thommie
  • : You can convert a float to a binary representation if you deal with the integer and decimal part separately to for ex. convert 2.5 to 0010.1001.
    : Separate the two parts into two separate integers. You have to decide how many decimals you want to use and round them correctly. You also have to output the decimal point yourself. You can then use the code you have to produce the binary representation.
    :
    : Thommie
    :
    Well, you can do in that way, but it would have no much sense!
    Floating point numbers are not precisely rapresentable in binary (that is the motif of the possible loss of precision when using floating points).
    The IEEE standard codes the floats as 4 byte values, check here for more info:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_ieee_floating.2d.point_representation_and_microsoft_languages.asp

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_why_floating_point_numbers_may_lose_precision.asp

    [hr]
    [italic]How beautiful, if Sorrow had not made sorrow more beautiful than Beauty itself.[/italic]
    JOHN KEATS


  • : hello guys this is converting int to binary
    : my questions are :
    :
    : 1)
    : in printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
    : what is 1 doing with arg1?
    : 2)
    : how can I modify this into a program which converts FLOAT to binary?
    : thanks
    :
    :
    : #include
    : #include
    :
    : 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
    ", arg1 );
    : printf( " BINARY:" );
    : printf( " " );
    :
    : for( x = 7; x > -1; x-- )
    : printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
    :
    :
    : }
    :
    :
    :


    [GREEN]1: The bit shift left operator (<<) precedes the bitwise and (&), so if arg1 = 15 and x = 3, the statement "arg1 & 1 << x" will yield

    0000 1111 & (0000 0001 << 3) =
    0000 1111 & 0000 1000 =
    0000 1000 = 8

    2: Real numbers (floats and doubles) are stored in two sections; the mantissa and the exponent. A 32 bit float fx. has a 24 bit segment reserved for the mantissa (including a bit for the sign (+/-)) and 8 bits for the exponent. I am, however, not sure how these numbers are stored in the memory, but the below program will show you the values of the different bytes in a float...then simply make a cup of coffee and start finding the system ;)[/GREEN]

    [CODE]#include <stdio.h>

    void showb( char* cp )
    {
    for( int i = 0; i < 8; i++ ) {
    printf( "%d", (*cp & 1) ? 1 : 0 ); *cp >>= 1;
    }
    }

    void main( void )
    {
    float f = 2.0; char* cp = (char*) &f;

    for( int c = 0; c < sizeof( float ); c++ ) {
    showb( cp++ ); printf( " " );
    }
    }[/CODE]
  • : 2: Real numbers (floats and doubles) are stored in two sections; the mantissa and the exponent. A 32 bit float fx. has a 24 bit segment reserved for the mantissa (including a bit for the sign (+/-)) and 8 bits for the exponent. I am, however, not sure how these numbers are stored in the memory, but the below program will show you the values of the different bytes in a float...then simply make a cup of coffee and start finding the system ;)[/GREEN]
    :

    You may use frexp() to retrieve the mantissa and the exponent of
    a double value.

    Regards,
    Blitz

  • : : 2: Real numbers (floats and doubles) are stored in two sections; the mantissa and the exponent. A 32 bit float fx. has a 24 bit segment reserved for the mantissa (including a bit for the sign (+/-)) and 8 bits for the exponent. I am, however, not sure how these numbers are stored in the memory, but the below program will show you the values of the different bytes in a float...then simply make a cup of coffee and start finding the system ;)[/GREEN]
    : :
    :
    : You may use frexp() to retrieve the mantissa and the exponent of
    : a double value.
    :
    : Regards,
    : Blitz
    :
    :

    Cool - thanks, Blitz :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories