Why do unsigned int x = -1 and int y = ~0 have the same binary representation?

前端 未结 5 1460
感动是毒
感动是毒 2020-12-15 09:54

In the following code segment what will be:

  • the result of function
  • value of x
  • value of y
    {
         unsigned int x=-1;         


        
5条回答
  •  有刺的猬
    2020-12-15 10:13

    If you run this program, you will see that the answer a is wrong and c is the correct answer:

    #include 
    #include 
    
    int main() {
        unsigned int x=-1;
        int y;
        y = ~0;
        if(x == y)
            printf("same\n");
            if(x==INT_MAX) printf("INT_MAX\n");
            if(x==UINT_MAX) printf("UINT_MAX\n");
        else
            printf("not same");
        return 0;
    }
    

提交回复
热议问题