Two's complement addition and subtraction

1. Rules

Assume that A and B are both positive numbers

addition:

Integer: [A]'s complement + [B]'s complement = [A+B]'s complement (mod 2^(n+1))
Decimal: [A]'s complement + [B]'s complement = [A+B]'s complement (mod 2)

Subtraction:
Integer: [AB]'s complement = [A]'s complement + [-B]'s complement (mod 2^(n+1))
Decimal: [AB]'s complement = [A]'s complement + [-B]'s complement (mod 2)
The method of finding the complement of [-B] is to add one to the negation of each bit, including the sign bit of the complement of [B].

 

Two, example

A = +15, B = +24, find [AB] complement

Convert to binary source code:

A = +15 = 0,0001111;

 B = +24 = 0,0011000 

Change to complement:

[A] Complement = 0,0001111,

[B] complement = 0,0011000,

[-B] complement=1,1101000


[AB] Complement = [A] Complement + [-B] Complement

0,0001111 +1,1101000 

= 1,1110111

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324955504&siteId=291194637