同一个“数”绝对值的和
一个w位的数,最高位为1,解析为无符号数、无符号数后,两个数的绝对值的和为2^w。
例如:
1011,解析为无符号数为11,有符号数为-5,绝对值相加的和为16
2.3、整数的运算
2.3.1 无符号加法
两个w位的无符号数x,y相加,如果产生溢出,溢出到w+1位,此时该位值为1。
此时,舍弃溢出位后的实际的值为 x+y-2^w
(w+1位的值为2^w
)
例:4位无符号数,9(1001) 和10(1010),不考虑溢出,相加结果为19(10011),而去除溢出位后的结果为3。
1001
1010
--------
10011
C语言中如何判断无符号数溢出
无符号数加法发生溢出时,结果肯定小于两个加数中的任意一个
(x+y-2^w = x+(y-2^w) = y+(x-2^w) ,一个数加上一个负数,数变小)
()补码数相加
正数相加溢出,溢出后的结果为负数,负数的大小是多少?
负溢出
溢出
两个正数,相加结果为负,表示发生了正溢出
两个负数,相加结果为正,表示发生了负溢出
加法逆元(相反数)
x + x’ = 0,称x’为x的逆元(相反数)
即 x + x' = 2^w = 0
前面的例子中255+1导致溢出,结果同样为0
x>=0时,x逆元 x' = 2^w-x