Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么此按位运算符返回负数?_C_Bitwise Operators - Fatal编程技术网

C 为什么此按位运算符返回负数?

C 为什么此按位运算符返回负数?,c,bitwise-operators,C,Bitwise Operators,我刚刚读了一篇关于位运算符的教程,我不明白为什么这个数字是-61 unsigned int a = 60; /* 60 = 0011 1100 */ unsigned int b = 13; /* 13 = 0000 1101 */ int c = 0; c = ~a; /*-61 = 1100 0011 */ printf("Line 4 - Value of c is %d\n", c ); 11000011不应该是195吗?128+64+2+1 不,不

我刚刚读了一篇关于位运算符的教程,我不明白为什么这个数字是-61

unsigned int a = 60;    /* 60 = 0011 1100 */  
unsigned int b = 13;    /* 13 = 0000 1101 */
int c = 0; 
c = ~a;          /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
11000011不应该是195吗?128+64+2+1

不,不应该

您可能希望对所有参数使用无符号字符

unsigned char a = 60;
unsigned char b = ~a;
char c = ~a;
printf("b=%d\nc=%d\n", b, c);
给出输出:

b=195
c=-61

你把它存储到一个有符号的整数中,是什么让你想到这些整数是8位的?编辑:我看到你给出的链接使用了8位数学。由于2的补码的工作方式,这恰好为有符号整数给出了正确的答案,但是对于无符号整数,得到的数字要比195大得多;是一种超出范围的分配,它会导致在公共系统上执行定义的行为,即选择最低的8位并重新解释为字符,尽管通常不能保证这一点。