问题描述
#包括LT&;&stdio.h中GT;诠释的main()
{
int类型的= 32;
的printf(%d个\\ N,〜一); // 2号线
返回0;
}O / P = -33
其实在原来的片段行2
的printf(%X \\ n,〜一); // 2号线
我解决它像
十六进制的32 20。
0000 0000 0010 0000
现在波浪运营商具有互补性
1111 1111 1101 1111 = ffdf。
我很困惑如何解决这个问题时,我有
的printf(%d个\\ N,〜一); // 2号线即%D NOT%X。
在您的C实现,如在任何编程语言中最现代化的实现,有符号整数重新使用的。
在二的补码,高位指示一个负数,而值都设有$ C $光盘作为这些样品中
位小数
0 ... 011 +3
0 ... 010 +2
0 ... 001 +1
0 ... 000 0
1 ... 111 -1
1 ... 110 -2
1 ... 101 -3
因此,如果平时(无符号)的二进制的位的值是 N 的和高比特是零,重新presented值是+的 N 的。然而,如果最高位为一,则重新presented值的 N 的-2 ,其中的是W 的是宽度(比特格式的数字)。
所以,在一个无符号的32位格式,32个1位通常会4,294,967,295。在一个二的补32位格式,32个1比特是4,294,967,295 - 2 = -1
。在你的情况,你有位1111 1111 1111 1111 1111 1111 1101 1111 32位无符号格式,即4294967263。以二的补,这是4294967263 - 2 = -33
#include<stdio.h>
int main()
{
int a=32;
printf("%d\n", ~a); //line 2
return 0;
}
o/p = -33
Actually in the original snippet line 2 was
printf("%x\n", ~a); //line 2
I solved it like
32 in hex is 20.
0000 0000 0010 0000
now tilde operator complements it
1111 1111 1101 1111 = ffdf.
I am confused how to solve it when I have
printf("%d\n", ~a); //line 2 i.e %d NOT %x.
In your C implementation, as in most modern implementations of any programming language, signed integers are represented with two’s complement.
In two’s complement, the high bit indicates a negative number, and the values are encoded as in these samples:
Bits Decimal
0…011 +3
0…010 +2
0…001 +1
0…000 0
1…111 -1
1…110 -2
1…101 -3
Thus, if the usual (unsigned) binary value for the bits is n and the high bit is zero, the represented value is +n. However, if the high bit is one, then the represented value is n-2, where w is the width (the number of bits in the format).
So, in an unsigned 32-bit format, 32 one bits would normally be 4,294,967,295. In a two’s complement 32-bit format, 32 one bits is 4,294,967,295 - 2 = -1.
In your case, the bits you have are 1111 1111 1111 1111 1111 1111 1101 1111. In unsigned 32-bit format, that is 4,294,967,263. In two’s complement, it is 4,294,967,263 - 2 = -33.
这篇关于为什么是这个code段输出-33的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!