补码加减法
一、规则
假定了A,B都是正数
加法:
整数: [A]补 + [B]补 = [A+B]补 (mod 2^(n+1))
小数: [A]补 + [B]补 = [A+B]补 (mod 2)
减法:
整数: [A-B]补 = [A]补 + [-B]补 (mod 2^(n+1))
小数: [A-B]补 = [A]补 + [-B]补(mod 2)
[-B]补的求法就是 [B]补的连同符号位在内,每位求反加一
二、例
A = +15, B = +24,求[A-B]补
换成二进制原码:
A = +15 = 0,0001111;
B = +24 = 0,0011000
换成补码:
[A]补 = 0,0001111,
[B]补 = 0,0011000,
[-B]补 = 1,1101000
[A-B]补 = [A]补 + [-B]补
0,0001111 +1,1101000
= 1,1110111
朝闻道