c 为什么打印负数?
我原以为它会打印一个非常大的数字和相同的数字 -1 但它只打印 -1 和 -2,这是为什么?
fprintf(stderr, "%d\n", 0xffffffff);
fprintf(stderr, "%d\n", 0xfffffffe);
I was expecting this to print a very large number and that same number -1 but it just prints -1 and -2, why is this?
fprintf(stderr, "%d\n", 0xffffffff);
fprintf(stderr, "%d\n", 0xfffffffe);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
%d
格式是有符号整数(十进制)。整数使用二进制补码存储,这意味着高位(8000 0000 ) 从某种意义上讲,表示值的符号。从 3 倒数,值为:
等等。
如果您希望 FFFF FFFF 显示为一个大的正数,请使用
%u
(无符号)格式。The
%d
format is a signed integer (decimal). Integers are stored using two's complement, which means that the high-order bit (8000 0000) indicates, in a manner of speaking, the sign of the value.Counting down from 3, values are:
etc.
If you want FFFF FFFF to display as a large positive number, use the
%u
(unsigned) format.参数“%d”将输入打印为有符号整数。因此,您发现了二进制补码表示,考虑“%u “ 反而。
The argument "%d" prints the input as a signed integer. As a result, you have discovered the two's complement representation, consider "%u" instead.
您提到的值是 -1 和 -2 的二进制补码表示
查找二进制补码
The values you mention are the two's complement representation of -1 and -2
Look up two's complement
有符号整数的第一位是符号,因此可以存储的最高数字是 0xEFFFFFFF。
The first bit on a signed integer is the sign, so the highest number that could be stored is 0xEFFFFFFF.