Why does the computer use the complement to perform the operation

Source: Internet
Author: User

Basic knowledge:

Within a computer, the length of an integer is determined, and in a computer with a length of 32 bits, an integer is 32 binary, which also includes the sign bit (1 means positive and 0 is negative). In order to facilitate the description, we assume that the machine word length is 8 bits.

For example, a decimal integer of 23, a binary truth representation of 10111, and its original code represented as 0001 0111.

The decimal integer-23, binary true value is represented as-10111, and the original code is represented as 1001 0111.

In short, the source code is the highest level for the sign bit, the other bits represent the absolute value of the number

If the computer inside the original code representation number, then in the addition and subtraction operation, the final conversion to two absolute value of the addition and subtraction operations, so in the design of the calculator both need to design the addition operator, but also design the subtraction operator (the cost is a bit large, can you use a type of the arithmetic device?) In fact, most people like to do the addition operation, do not like to use subtraction operation.

The idea of complement

We want to design only the addition operator, without the subtraction operator, we want to find a solution that uses this scheme to do the addition operation 1 + (-1), two numbers can be directly based on the binary addition rules to do the operation, get 0, without having to do subtraction.

Using 0000 0000 to represent 0 is a natural idea, with 0000 0001 to 0111 1111 for positive numbers from 1 to 127, which is also a natural idea, at which point 0 of the highest bit can be used as a symbol, or as an ordinary bits. Now the question is: what does it mean-1? We do a reverse thinking, 0000 0001 plus what binary number can get 0000 0000? That is: from right to left, the rightmost minimum bit of addend must be 1, according to the binary addition rule: 1+1=0, carry is 1. Again consider the second low, addend the sub-low must also be 1, and then add 1 to 0 into a bit, ... By analogy, the 8 of the addend must be 1 before it can get 8 0. The problem is that the final result is a carry, namely: 0000 0001 + (1111 1111) = 1 0000 0000 This is mathematically unacceptable, but it is just right in the computer, because in the design, the length of each number is determined, so no matter how much the result ends, only 8 bits are reserved. The extra bits will be discarded. So, we can represent 1111 1111来-1, and here's a way to rationalize how 1 becomes 1111 1111 of this form. the definition of complement:   The signed integer has the original code, the inverse code, the complement and so on several kinds of coding methods. The original code directly converts the true value into its corresponding binary form, while the inverse code and the complement are some kind of conversion encoding method for the original code. The original code, the inverse code and the complement of the positive integers are the same, negative anti-code is the original code in addition to the other bits of the symbol bit to reverse the result (the inverse is if the bit is 0 to 1 and the bit is 1 to 0 operation) and the complement is the original code of the inverse code, and then at the end of the anti-code to add 1 after the result, that is, the complement is anti-code
Complement is the most convenient way. Its convenience now, all addition operations can be done using the same circuit.
Take-8 as an example. Assume that there are two ways of representing it. One is intuitive notation, or 10001000, and the other is the 2 complement notation, or 11111000. What kind of notation is more convenient in addition operations?
Write a calculated formula, 16 + (-8) =?
The binary representation of 16 is 00010000, so with intuitive notation, addition is written as: 00010000
+10001000
---------
10011000 you can see that if you follow the normal addition rules, you will get 10011000 results, and turn to decimal is-24. Obviously, this is the wrong answer. That is, in this case, the normal addition rule does not apply to the addition of positive and negative numbers, so two sets of arithmetic rules must be developed, one for the number of positive home plus, and a set for positive plus negative numbers. From the circuit, it is necessary to do two kinds of circuit for addition operation.
Now, let's look at the 2 complement notation. 00010000
+11111000
---------
100001000 can be seen, according to the normal addition rule, the result is 100001000. Note that this is a 9-bit binary number. We have assumed that this is a 8-bit machine, so the highest 9th bit is an overflow bit that will be automatically abandoned. So, the result turns into 00001000, and turns into decimal exactly 8, which is the correct answer for 16 + (-8). This shows that 2 of the complement notation can be extended to the entire set of rules of addition, so that a set of circuits can be used to achieve the addition of all integers. The essence of the complement:

To convert a positive number to a negative, you can actually subtract it by 0. For example, 8 is actually 0-8.
It is known that the binary of 8 is 00001000,-8 and can be calculated using the following formula:

00000000
-00001000
---------

Because 00000000 (meiosis) is less than 0000100 (meiosis), it is not reduced enough. Please recall the elementary school arithmetic, if one of the meiosis is less than the meiosis, what do we do? Very simple, ask the last one to borrow 1 on it.
Therefore, 0000000 also asked on a borrowed 1, that is to say, the deduction is actually 100000000, the formula is also changed to write:

100000000
-00001000
---------
11111000
Further observation, you can find 100000000 = 11111111 + 1, so the above equation can be split into two:
11111111
-00001000
---------
11110111
+00000001
---------
11111000
This is how the two steps of the complement are to be converted. (of which 1111 1000 is-8 of the complement, is by 000 1000 to get the inverse of 111 0111 plus 1 eventually get 111 1000, and finally add the sign bit 1 is 1111 1000). This is the origin of the complement calculation rule.

Why does the computer use the complement to perform the operation

Related Article

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.