在c

时间:2017-03-12 15:53:42

标签: c

在这段代码中,我应该要求用户翻转32位中的1位。当我翻转第32位(索引31)时,即使我使用unsigned int,我也会收到-2147483648。如何正确修理翻盖?

int main(int argc, char *argv[]) {
   int32_t num = 0;
   int bitC = 0;
   int number;
   char cont;
   do {
      printf("Enter a number between 1 and 1000\n");
      do {
         scanf("   %d", &num);
         if(num < 0 || num > 1000) {
            printf("Error: Number MUST be between 1 and 1000.\n");
            //printf("Please re-enter number.\n");
         }
      } while(num < 0 || num > 1000);
      printf("Choose a bit, between 0 and 31, to flip\n");
      do {
         scanf("   %d", &bitC);
         if(bitC < 0 || bitC > 31) {
            printf("Error: bit MUST be between 0 and 31\n");
            //printf("Please re-enter bit to change.\n");
         }
      } while(bitC <0 || bitC > 31);
      printf("Number before bit flip was %d\n", num);
      //num ^= (-bitC ^ num)&(1 << bitC);
      num ^= (1UL << bitC);
      printf("New number is: %d\n", num);
      printf("Would you like to shift another bit?\n");
      printf("Enter y to continue or n to quit\n");
      scanf("   %s", &cont);
   } while(cont == 'y');
   return 0;
}

2 个答案:

答案 0 :(得分:0)

您现在正在使用该程序中的签名内容。但真正的问题是,当你使用printf时,你正在使用%d,这会导致printf评估参数(记住printf不知道它是什么,它只是堆栈上的一个值)作为一个带符号的int,即使传递给printf的值是无符号的。使用%u。

答案 1 :(得分:0)

这是一个发布的代码版本,它干净地编译,并且没有OP发布的代码中固有的逻辑和数据缺陷。

它还正确地将错误消息输出到stderr而不是stdout,并且这些错误消息包括操作系统认为发生错误的原因。

#include <stdio.h>    // scanf(), printf(), fprintf(), perror()
#include <stdlib.h>   // exit(), EXIT_FAILURE
#include <inttypes.h> // uint32_t


int main( void )
{

   uint32_t num  = 0;
   uint32_t bitC = 0;
   char cont     = 'y';

   do
   {
      printf("Enter a number in the range 1...1000\n");

      do
      {
         if( 1 != scanf("%u", &num) )
         {
             perror( "scanf for number failed" );
             exit( EXIT_FAILURE );
         }

         // implied else, scanf successful

         // the 'num' is to check != 0
         if( !num || 1000 < num )
         {
            printf("Error: Number MUST be in range 1...1000.\n");
         }

         else
         {
             break;
         }

      } while(1);

      printf("Choose a bit, in range 0...31, to flip\n");

      do
      {
         if( 1 != scanf("%u", &bitC) )
         {
             perror( "scanf for bit number failed" );
             exit( EXIT_FAILURE );
         }

         // implied else, scanf successful

         if( bitC > 31 )
         {
            fprintf( stderr, "Error: bit must be in range 0...31\n" );
         }

         else
         {
             break;
         }

      } while(1);

      printf("Number before bit flip was %u\n", num);

      num ^= (1U << bitC);

      printf("New number is: %u\n", num);

      printf("Would you like to shift another bit?\n");
      printf("Enter y to continue or n to quit\n");

      if( 1 != scanf(" %c", &cont) )
      {
          perror( "scanf for continuing failed" );
          exit( EXIT_FAILURE );
      }

   } while('y' == cont || 'Y' == cont );
   return 0;
}