【发布时间】:2014-11-25 11:05:00
【问题描述】:
unsigned int x =-1; 的位表示是什么
我们可以为 unsigned int 分配一个负整数吗?
#include<stdio.h>
int main(){
unsigned int x = -1;
int y = ~0;
if(x == y)
printf("same");
else
printf("not same");
return 0;
}
输出:
一样
怎么可能,x 是无符号的
#include<stdio.h>
int main()
{
unsigned int x = -4;
if (x == -4)
printf("true");
else
printf("FALSE");
}
输出:
是的
【问题讨论】:
-
unsigned 不能为负 :) 当你写下“unsigned”这个词时,你不认为它是什么意思吗?
-
你的问题的答案已经在你的例子中了。
-
我的意思是,-1 如何以二进制形式表示。 -1和1相同的逻辑是什么
-
@larsmans:该语言要求
(unsigned)(-1)在所有平台上产生UNIT_MAX,无论是否使用2的共同实现。
标签: c integer-promotion