找到你要的答案

Q:Recursive bits reverse program

Q:递归位反向程序

Recursive reverse bits program.

#include <stdio.h>
#include <conio.h>

void displayBits( unsigned value );
void reverseBits( unsigned value, unsigned c );

int main( void )
{
    unsigned value;
    unsigned c = 32;

    printf( "Enter a unsigned integer: " );
    scanf( "%u", &value );   

    displayBits( value );
    printf( "%10s", "Reversed:    " );
    reverseBits( value , c );

    getche();
    return 0;
}

void displayBits( unsigned value )
{
     unsigned c;
     unsigned mask = 1 << 31;

     printf( "%10u = ", value );

     for( c = 1; c <= 32; c++ ){
          putchar( value & mask ? '1' : '0' );
          value <<= 1;
          if( c % 8 == 0 )
              putchar( ' ' );

          }//end for    
     putchar( '\n' );

}

void reverseBits( unsigned value, unsigned c )
{
     unsigned mask = 1 << 31;

     if( c == 0 ){
         return;

         }//end if

     else{     
          value <<= 1;
          reverseBits( value , c - 1 );
          putchar( value & mask ? '1' : '0' );
          if( c % 8 == 0 ){
              putchar( ' ' );

              }//end if
          }//end else
}

When I input 15 it outputs

01111000 00000000 00000000 00000000

instead of

11110000 00000000 00000000 000000000. 

递归反向位程序。

#include <stdio.h>
#include <conio.h>

void displayBits( unsigned value );
void reverseBits( unsigned value, unsigned c );

int main( void )
{
    unsigned value;
    unsigned c = 32;

    printf( "Enter a unsigned integer: " );
    scanf( "%u", &value );   

    displayBits( value );
    printf( "%10s", "Reversed:    " );
    reverseBits( value , c );

    getche();
    return 0;
}

void displayBits( unsigned value )
{
     unsigned c;
     unsigned mask = 1 << 31;

     printf( "%10u = ", value );

     for( c = 1; c <= 32; c++ ){
          putchar( value & mask ? '1' : '0' );
          value <<= 1;
          if( c % 8 == 0 )
              putchar( ' ' );

          }//end for    
     putchar( '\n' );

}

void reverseBits( unsigned value, unsigned c )
{
     unsigned mask = 1 << 31;

     if( c == 0 ){
         return;

         }//end if

     else{     
          value <<= 1;
          reverseBits( value , c - 1 );
          putchar( value & mask ? '1' : '0' );
          if( c % 8 == 0 ){
              putchar( ' ' );

              }//end if
          }//end else
}

当我输入15它输出

01111000 00000000 00000000 00000000

而不是

11110000 00000000 00000000 000000000. 
answer1: 回答1:
 else{     
      value <<= 1;//<-- Value has been changed before it can be used
      reverseBits( value , c - 1 );
      putchar( value & mask ? '1' : '0' );
      if( c % 8 == 0 ){
          putchar( ' ' );

          }//end if
      }//end else

replace with

else {
    reverseBits(value << 1, c - 1 );
    putchar( value & mask ? '1' : '0' );
    if( c % 8 == 0 ){
        putchar( ' ' );
    }
}
 else{     
      value <<= 1;//<-- Value has been changed before it can be used
      reverseBits( value , c - 1 );
      putchar( value & mask ? '1' : '0' );
      if( c % 8 == 0 ){
          putchar( ' ' );

          }//end if
      }//end else

更换

else {
    reverseBits(value << 1, c - 1 );
    putchar( value & mask ? '1' : '0' );
    if( c % 8 == 0 ){
        putchar( ' ' );
    }
}
answer2: 回答2:

If you will try you current code for a number in which all bits are set, then you'll come to know about your mistake. In case of all bits are set, your program will return following o/p.

01111111 11111111 11111111 11111111
instead of
11111111 11111111 11111111 11111111

That is because you are shifting first bit of given number 32 times(positions), instead of 31. so you have to change your logic. You can do it as pointed out by @BLUEPIXY.

如果您将尝试您的当前代码为一个数字,其中所有位设置,那么你会来了解你的错误。在所有位设置的情况下,您的程序将返回以下O / P。

01111111 11111111 11111111 11111111
而不是
11111111 11111111 11111111 11111111

That is because you are shifting first bit of given number 32 times(positions), 而不是 31. so you have to change your logic. You can do it as pointed out by @BLUEPIXY.

c  recursion  reverse  bit