gpt4 book ai didi

C、unsigned int(a)^unsigned int(b) 和 unsigned int(a^b) 有什么区别?

转载 作者:行者123 更新时间:2023-11-30 21:25:28 28 4
gpt4 key购买 nike

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

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);

最佳答案

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

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

  • 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;
    }

关于C、unsigned int(a)^unsigned int(b) 和 unsigned int(a^b) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862279/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com