Complement addition subtraction _ Digital coding

Source: Internet
Author: User

The basic rule is:
Addition:
Integer: [A] Complement + [B] complement = [a+b] Complement (mod 2^ (n+1))
Decimal: [A] Complement + [B] complement = [a+b] complement (mod 2)

Subtraction:
Integer: [a-b] complement = [A] complement + [-b] Complement (mod 2^ (n+1))
Decimal: [a-b] complement = [A] complement + [b] complement (mod 2)
[-b] the complement of the method is a "complement" with the symbol bit, each of the reverse plus a

PS: This assumes that a,b are positive.

If you write the complement of a negative number directly, then the direct calculation is good. by [-b] to fill the solution [b], or by [b] to make up [a] complement, all together with the symbol bit each one, the expression itself, as long as the complement, direct calculation can be.
The following examples can be very well illustrated:

Set the machine number to 8 digits long, one as a symbol bit. If A = +15, B = +24, ask [a-b] complement, and revert to the truth value.
PS: Here the restore you really are to calculate the complement of the value is not 15-24 =-9 multi-digit?

Solution: First is to change into binary original code: A = +15 = 0,0001111; B = +24 = 0,0011000
Replace with complement: [A] complement = 0,0001111, [B] complement = 0,0011000,[-b] complement = 1,1101000

The above is the standard preparation
[A-b] complement = [A] complement + [B] Complement
0,0001111
+1,1101000
= 1,1110111

You can also think: direct a-b complement subtraction:
0,0001111
-1,1101000
= 1,1110111

A 2 is borrowed from the Nineth (unknown), and the result is the complement we need.

The truth value is-9.

Summary: Regardless of whether the operand is positive or negative, in the complement operation, you only need to take the symbol bit and the value part of the operation, and the symbol bit produced by the natural throw away. You can also borrow from a higher level to participate in the operation, so that the symbol bit will be 1 of the way, well, I'm not as big as you, the formation is negative.

In the calculation of this subtraction form, it is necessary to pay attention to the borrow thing.
In decimal:
1000
-99
=901

The process is this: 1000 of the single-digit is 0 to reduce, to the high to borrow a 10, at the top it seems to be only 1 but the following seems to be 10, OK, 10 can be reduced 9 to get a bit is 1;1000 10 is 0, but also was a bit to borrow a, so strictly 10 is called-1, OK,-1 do not move 9, Need to borrow from the hundred, the Hundred gave a in it seems to be 1, but in 10 looks is 10, OK, 10-bit borrowed, first also own debt 1, only 9, 9 minus 9, can be reduced, get 0;1000 hundred oneself is also 0, and was borrowed a, so can be-1, to thousands borrow, the debt, left 9, There's nothing to lose, so the end result is 9.
So get 901.

Similarly, in the binary system, is the same idea.
In one example: Ask 1,0100011-1,1010011
1,0100011
-0,0101101
=0,1110110

Process: Also starting from the first to reduce, 1-1 = 0; Second: 1-0 = 1; Third: 0-1 = 1, and the fourth has 1 liabilities (not looking at deposits);
Fourth: Deposit is 0, the liability is 1, then they are-1 ah, minus 1, so borrowed 2, first also debt, leaving 1, and then minus 1 = 0;
Fifth: Also debt 1, borrowed 1, minus is 0 so there are 1;
Sixth: Originally is 1, was the fifth to borrow, so only 0, reduce the 1, so to the seventh to borrow 2, minus 1 also left 1;
Seventh: itself is 0, by the sixth to borrow 1, so is-1, to reduce 0, first to borrow 2, debt after the remaining 1, minus 0 or 1
Eighth: itself is 1, was borrowed, well, that's 0 left, just need to subtract is also 0, don't go up to borrow. Of course, when it is necessary to borrow, the thing that exceeds eight will be taken from the void, not yet.

Overflow judgment:
1. Only one sign bit, the actual participation in the operation of the two number, which is, if it is a-b, see is a [a] complement and [b] complement the symbol bit
If the two sign bits are the same, but the result of the finding (complement result) sign bit is different, then it is overflow.

2. Two-bit sign bit: When the two-bit sign bit does not also indicate overflow, there is no overflow.
Regardless of whether there is overflow, the highest bit of symbol bit represents the real symbol.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.