C,unsigned int(a)^ unsigned int(b)和unsigned int(a ^ b)之间有什么区别?

时间:2015-04-25 07:20:13

标签: c

在测试平台中,我编写如下程序:

int a, b;
scanf_s("%d%d",&a,&b);
unsigned int c = a^b;//this can not pass
unsigned int c = unsigned int(a)^unsigned int(b) //this can pass
int cnt = 0;
while (c){
    cnt++;
    c = c&(c-1);
}
printf("%d\n",cnt);

1 个答案:

答案 0 :(得分:1)

你的程序中有一些严重的语法错误我怀疑它编译尝试使用以下程序,它应该工作正常。

您的代码中存在以下问题。

  • c被宣布两次
  • int转换为unsigned int时出错了

    int main()
    {
    
      int a, b;
      scanf_s("%d%d",&a,&b);
      unsigned int c = a^b;//this can not pass
      c = (unsigned int)a^( unsigned int )b;//this can pass
      int cnt = 0;
      while (c){
          cnt++;
          c = c&(c-1);
      }
      printf("%d\n",cnt);
    
      return 0; 
    }