为什么 unsigned int x = -1 和 int y = ~0 具有相同的二进制表示? [英] Why do unsigned int x = -1 and int y = ~0 have the same binary representation?
本文介绍了为什么 unsigned int x = -1 和 int y = ~0 具有相同的二进制表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码段中会是什么:
- 函数的结果
- x的值
- y 的值
<上一页>一个.相同,MAXINT,-1湾.不一样,MAXINT,-MAXINTC.相同, MAXUINT, -1d.相同,MAXUINT,MAXUINTe.不一样,MAXINT,MAXUINT
有人可以解释一下它是如何工作的吗?或者可以解释一下代码片段吗?
我知道这是关于补码 n 等.MAXINT 和 -1 的意义是什么?这是因为 unsigned int 和 int 的事情 - 我是对的吗?
解决方案
如果你运行这个程序,你会看到答案a是错,c是正确答案:
#include <stdio.h>#include <limits.h>int main() {无符号整数 x=-1;整数y;y = ~0;如果(x == y)printf("相同
");if(x==INT_MAX) printf("INT_MAX
");if(x==UINT_MAX) printf("UINT_MAX
");别的printf("不一样");返回0;}
In the following code segment what will be:
- the result of function
- value of x
- value of y
{ unsigned int x=-1; int y; y = ~0; if(x == y) printf("same"); else printf("not same"); }
a. same, MAXINT, -1 b. not same, MAXINT, -MAXINT c. same , MAXUINT, -1 d. same, MAXUINT, MAXUINT e. not same, MAXINT, MAXUINT
Can someone explain me how its works or can just explain the snippet??
I know it's about two's complement n etc.. What is the significance of MAXINT and -1 ? It is because of unsigned int and int thing - am I right ?
解决方案
If you run this program, you will see that the answer a is wrong and c is the correct answer:
#include <stdio.h>
#include <limits.h>
int main() {
unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same
");
if(x==INT_MAX) printf("INT_MAX
");
if(x==UINT_MAX) printf("UINT_MAX
");
else
printf("not same");
return 0;
}
这篇关于为什么 unsigned int x = -1 和 int y = ~0 具有相同的二进制表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文