計算機負數相加
-1 = 1111 1111
-2 =1111 1110
+----------------------
= 1111 1101 補碼為0000 0011 以補碼形式
#include<stdio.h>
#include<string.h>
int main(void)
{
unsigned j = 10;
int i = -20;
printf("%ud\n",i + j);
(i + j > 10000) ? (printf(">10000")) : (printf("<10000"));
printf("\n");
return 0;
打印結果是 -10 >10000
int i = -20;
unsigned j = 10;
i + j的結果為什么是-10呢?負數加無符號整數結果不應該默認為無符號數嗎?
無符號數和有符號數比較,默認將無符號數轉為有符號數,因為負數在內存中以補碼形式存在
比如-1 為1的補碼1111 1111 轉為無符號數為255
