Skip to main content
deleted 26 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Remember that ALL the bits need to be reversed, not just the '1' bits. The following code compiles, links cleanly and works properly.

Remembering that ALL the bits need to be reversed, 
not just the '1' bits

the following code compiles, links cleanly
and works properly.

#include <stdio.h>
#include <stdlib.h>

#define INT_BITS (32)

int main()
{
    unsigned int input;
    unsigned int result = 0;
    unsigned int i; // loop counter

    printf("\nEnter the unsigned Number: ");
    if( 1 != scanf("%u", &input) )
    { // scanf failed
        perror( "scanf failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    for(i=0;i<INT_BITS;i++)
    {
      result  <<= 1;
      result  |= input & 1;
      input >>=1;
    } // end for

    fprintf(stdout, "%u\n", result );

    return(0);
} // end function: main
Remembering that ALL the bits need to be reversed, 
not just the '1' bits

the following code compiles, links cleanly
and works properly.

#include <stdio.h>
#include <stdlib.h>

#define INT_BITS (32)

int main()
{
    unsigned int input;
    unsigned int result = 0;
    unsigned int i; // loop counter

    printf("\nEnter the unsigned Number: ");
    if( 1 != scanf("%u", &input) )
    { // scanf failed
        perror( "scanf failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    for(i=0;i<INT_BITS;i++)
    {
      result  <<= 1;
      result  |= input & 1;
      input >>=1;
    } // end for

    fprintf(stdout, "%u\n", result );

    return(0);
} // end function: main

Remember that ALL the bits need to be reversed, not just the '1' bits. The following code compiles, links cleanly and works properly.

#include <stdio.h>
#include <stdlib.h>

#define INT_BITS (32)

int main()
{
    unsigned int input;
    unsigned int result = 0;
    unsigned int i; // loop counter

    printf("\nEnter the unsigned Number: ");
    if( 1 != scanf("%u", &input) )
    { // scanf failed
        perror( "scanf failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    for(i=0;i<INT_BITS;i++)
    {
      result  <<= 1;
      result  |= input & 1;
      input >>=1;
    } // end for

    fprintf(stdout, "%u\n", result );

    return(0);
} // end function: main
Source Link
user3629249
  • 2.8k
  • 11
  • 10

Remembering that ALL the bits need to be reversed, 
not just the '1' bits

the following code compiles, links cleanly
and works properly.

#include <stdio.h>
#include <stdlib.h>

#define INT_BITS (32)

int main()
{
    unsigned int input;
    unsigned int result = 0;
    unsigned int i; // loop counter

    printf("\nEnter the unsigned Number: ");
    if( 1 != scanf("%u", &input) )
    { // scanf failed
        perror( "scanf failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    for(i=0;i<INT_BITS;i++)
    {
      result  <<= 1;
      result  |= input & 1;
      input >>=1;
    } // end for

    fprintf(stdout, "%u\n", result );

    return(0);
} // end function: main