为什么无符号整数X = -1,INT Y =〜0具有相同的二进制重新presentation? [英] Why do unsigned int x = -1 and int y = ~0 have the same binary representation?
本文介绍了为什么无符号整数X = -1,INT Y =〜0具有相同的二进制重新presentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下code段将是什么:
In the following code segment what will be:
- 函数的结果
- X 价值
- 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??
我知道这是关于补ñ等。
什么是MAXINT和-1的意义是什么?
正是由于unsigned int类型和INT的事情 - 我说的对
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 ?
推荐答案
如果你运行这个程序,你会看到答案一个是的错误的和C是正确答案:
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\n");
if(x==INT_MAX) printf("INT_MAX\n");
if(x==UINT_MAX) printf("UINT_MAX\n");
else
printf("not same");
return 0;
}
这篇关于为什么无符号整数X = -1,INT Y =〜0具有相同的二进制重新presentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文