在测试平台中,我编写如下程序:
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);
答案 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;
}