【发布时间】:2018-02-03 18:56:26
【问题描述】:
y 提升为 unsigned int 并在此处与 x 进行比较。二进制数比较是否每次都发生?那么如果(12 == -4)完成了,为什么不能将LHS提升为无符号并打印“相同”?(考虑到12 = 1100,-4 = 1100)如果我错了,请纠正。
#include<stdio.h>
int main()
{
unsigned int x = -1;
int y = ~0;
if(x == y)//1.what happens if( y == x) is given?.O/P is "same" then also.
printf("same");//output is "same"
else
printf("not same");
printf("%d",x);//2.output is -1.Won't x lose it's sign when unsigned is given?My hunch is x should become +1 here.
getchar();
return 0;
}
请同时提供上述代码的二进制数以及代码cmets中1.和2.的答案。谢谢。
【问题讨论】:
-
您的问题中的
why can't it promote LHS to 4 and print "same"部分非常不清楚IMO。 -
顺便说一句,
-4 == 1100仅适用于 4 位系统。 -
printf("%d",x);是未定义的行为,因为x不是int。 -
请在代码 cmets 中提供我的家庭作业的答案在这里对你来说不会很好。
-
和
%d,你告诉printf()它是签名的。所以printf()相信你。
标签: c operators bitwise-operators unsigned-integer