What is Complement (complement)? (Rpm)

http://www.tutorialspoint.com/computer_logical_organization/complement_arithmetic.htm

https://www.cnblogs.com/PI3141592657/p/7134474.html
over computer architecture course have contact with college friends complement this concept, but the book was taught as a prerequisite content is binary, that is, the so-called the complement of 2 (2's complement). Near term TCP / IP Volume 1, came in contact with "1's" concept, suddenly discovered that in fact not quite understand what it means in the end complement, so the access to information records.

Source: Wikipedia

Explanation of terms: Radix - base, in the context of this article is equivalent to "hex"

Definition: gives the length of the value y n, y is the complement to the base b is: b ^ n - y (i.e., the complement of b)

Limited, the translation of a mouthful, but the formula is very simple, in fact, complement by definition there is nothing hard to understand. However, some places need to be explained, complement this concept is based on binary (ie base) on, at least in terms of computer science, complement the definition of a certain base b is equal to the current band, that is, said defined above can be simplified to

Gives the length y and n has a value of the hexadecimal value is b, then the complement of y: b ^ n - y

In the following Examples decimal number

Given the value y = 1234, it is clear that the length of n = 4, the base b = 10 (10 you did not know where he comes from? On the importance of moderation)

According to the definition of the complement of the complement of y is then: 10 ^ 4--1234 = 8766

Purpose: What is the role of complement is it? You lucky matter, the use of very specific complement - adding operation with subtraction instead of

It sounds incredible, you almost want to blurt out: "No !!!", but in accordance with international practice, we take a look at how matter in the end it

Here to introduce another simple but very English and very bigger terms: narrow radix complement (diminished radix complement), see diminished the word I immediately make a Ji Ling, remembering the long-lost narrow incremental sort of (diminished increment sort ),What? He said the teacher did not teach you this? Oh, it's another name Hill sort, it is. . . Ahem, sorry beside the point.

Narrow base complement is actually (bn - 1) - y, means that you can add on a narrow base complement a 1's complement to get through to the base, that is

bn - and = (bn - 1) - y + 1

Is not feel like shouting: This is nonsense TM is not it? ?

The concept of what is the use? In fact, in the context of pure mathematics, this superfluous, no use. However, to my age, you will understand everything looks nonsense, certainly there must be a certain sense it has a context. In this article, the environment is the "value of a length", you know within the scope of mathematics, a number you want to write long there long, but long-values ​​are fixed within the computer, such int Java language length is 32-bit value, you can not use 32-bit number of 33 bits to represent, of course, even die 34!

Back to the top keyword base b, or as a decimal number as an example

Suppose the value of a fixed length of 4, the value given by y = 1234, if you really want as defined in the computer to obtain a complement, you can not do, because by definition the complement of 104--1234, but 104 = 10000, is beyond the range of 4 digits can be represented, you understand? Narrow radix complement is the complement to be able to get a fixed length value, the step backward ah

104--1 to 9999.
9999 - 1234 = 8765
8765 + 1 = 8766
However, smart as you will find that the use of complement code with subtraction instead of addition, but is obtained in step 2 above key step in complement, to be or not subtraction? ? This hairy difference? Ha, this is the most exciting place in the binary world, then you do not need subtraction, here is an example of a binary

Given the value y = 1011, it is clear that the latest steps in solving complement, complement = (24--1) --1011 + 1, suddenly saw where this complement of its commitment, with the addition instead of subtraction? Then we see a binary perspective

y = 1011

24 - 1 = 1111

In fact, you have discovered that this reduction is fixed radix complement - Computer numerical given length n, the reduction may be directly written radix complement: pppp ... ppp (n number p, p = binary --1 ), and critical

1111--1011, this step is actually not necessary for the operation of subtraction, directly y negated plus one can get a complement (not to be "negated plus one" are particularly familiar you are a class lectures? good child paper), this had to thank the great binary! Under require Again, such a smooth operation can be completed only binary, hexadecimal want to get complement other, still need subtraction.

The final chapter: the magic of the binary world, access to a number of complement simply negated a plus on it. Finally, we take a look at it, after complement hand, how can you replace the subtraction of it?

Suppose you want to solve x - y (x> = y), then divided into the following steps

Y is determined complement BN - y
X - X + y = (BN - y) = X - y + BN (this step by the addition of complement subtraction instead of the original value)
clearly x - y + bn> = bn However bn beyond the range of the number represented by the (overflow), are directly discarded, the final result is equal to x - y
1's complement: the world is not a band, it is a complement of a narrow base complement, is directly a binary value of negation

Tags: complement

Mathematical Principles integer twos complement (two's complement)
has recently re-learn the CPU architecture, the principle of using twos complement eliminated with the number of symbols and unsigned calculated difference, and integrating the subtracter to an adder, thereby simplifying the CPU hardware principles of design are very interested, so specifically thinking of the next, I looked at some online articles about two's complement, but most still too academic, after finishing, I would like to express it in a more concise manner. For simplicity, I use the word length register 4 by way of example, 32-bit and 64-bit for the same reason. For more scientific complement of mathematical principles can refer to the related wikipedia article about one's complement, two's complement of.

Hardware design simplicity is the goal, so only the best integer arithmetic addition, and do not sign bit special treatment, you can achieve this goal? Of course, that's complement (two's complement), the so-called complement is actually signed for a number of terms, which means that a positive number to use the original code, while a negative word using the 2 index burdens number of absolute value , i.e. x = pow (2, word_length) - abs (x), this simple calculation method is the complement of the computer book we often say, the negated absolute value of x plus 1. Now you know the real calculation methods complement it. Why should it be so negative number means? This is a mathematical principle, which is the need to explain the contents of this article, the full understanding of the common programming CPU instruction to lay a solid foundation (general purpose instructions are for integers), the future may also increase on floating point calculation specification of the article.

Now we look at a subtraction:
7--6 (type 1)
can turn it into a summer yet? I'll try:
7 + (16--6) --16 (Formula 2)
16 4-bit register is the minimum number of overflows (2 . 4 represents the operation pow), the above two equations are completely equivalent, in our view, the second more complicated equation is exactly the method employed cell internal CPU integer calculation, for some particular reason, CPU need only count the first adder, the other two are automatically cut subtraction is done by the compiler, or register a.
After the foregoing description, we know 16--6 -6 complement is in the 4-bit word length of the machine, this step is typically calculated compiler completed, the negative number is directly stored complement form, this is 1010. Let's see how to calculate CPU:
0111 (7)

  • 1 0 1 0 (10)
    ----------- ----------
    1 0 0 0 1 (17)

Completion of the above formula (2) calculation of the first two steps, Save 16 needs to get the correct result, the magical place, because the machine word length is 4, so a fifth discarded directly, that is the overflow, this is equivalent to automatically cut by 16, so the final result is 0001, equal to 1, type 2 completed all the calculations, got the right result, you should understand now why choose the minimum number of overflow to complement conversion is reduced the number of the bar, just to complete the automatic overflow, in order to achieve the final subtraction.

Let's look at two negative adding them to see how the CPU when the pure binary operation and the result is no less:
(-6) + (-7) (Formula 3)
was replaced with the above equation based on the law
[(16--6) --16] + [(16--7) --16] (formula 4)
where (16--6) and (16--7) part is done by the compiler, is the corresponding negative complement, so Let's look at the contents of the CPU computing:
1010 (10)

  • 1 0 0 1 (9)
    ----------- ------
    1 0 0 1 1 (19)

Equation 4 also need to subtract 2 16, where No. 5 has automatic overflow cut a 16, we have to subtract 16 to get a correct result, but the result register 0011, even with this result, I do not know in the end is Save final value 16 is required, it may be too bad, the reason for this problem is that if using all four register stored value, generates a binary number with a sign ambiguity, an analogy, two's complement represented -9 is (16--9), binary is 0111, and the integer 7 is actually the same, even with the string of binary I can not know it, or 7 -9, well, I really clever, thought of a way, hey, let Let's look at the binary 4-bit register can store What:
0 0 0 0
0 0 0 1
0 0 1 0
0 1 0 1
0 0 0 1
0 1 0 1
0 1 0 1
0 1 1 1
1 0 0 0
1 0 1 0
1 0 1 0
1 1 1 0
1 0 1 0
1 1 1 0
1 1 1 0
1 1 1 1
I can explain the most significant digit numeric symbols as a sign, 0 if it is I'll be a positive number interpretation, if it is a negative number when I explained that when a positive number not explain minus 16, the final result is direct, and if it is You also need to reduce the number 16 is the end result, because we are represented by 16-x -x, just being negative number in half (assuming that 0 is a positive number), then back to the question above, (- 6) + (-7) CPU final register 0011, if I should be interpreted as a positive number, not a positive number minus 16, the last is equal to 3, no! It should be -13, also we need to reduce the 16-fishes, but we just said no positive minus. In the end What went wrong? I think the next.
If we had to explain the manner set forth above with binary, determine the range of the binary representation of only 4 [-8 to 7], in fact, if the overflow from the left end of the register, then the value is [0 ... \ 1 \ 2 \ 3 \ 4 \ 5 \ 6 \ 7-8-7-6-5-4-3-2-1 \ 0 ...] the continuous cycle, that is to say just from -13 -8 left number five, and loops back to three, we must have a way to determine the overflow condition, if we register when unsigned binary interpretation, it is very simple, as long as it generates a carry-up to the overflow, but if when with a number of symbols to explain how to read the last value has overflowed it?
This is a complement Principia Mathematica code of the essence, with this reasoning, CPU can do the same process with a number of symbols and unsigned, we have to carefully analyze the principles on math, seems to register in the CPU is pure binary corresponds unsigned, if the two numbers, the most significant bit a carry, then situated affirmative result [16 to 30], if the highest order bit to a carry low result indicates the highest bit addition result 3 located in the range [8 to 14], since the most significant bit of overflow are discarded, the final result represents a reduction of 16, and a carry to the next highest order bit MSB, the final result represents a minimum of 8, there now are several conclusions:
(1) the highest bit carry, next highest order bit has a carry, then the final result is located [-8 to 6]
(2) the highest bit has a carry, next highest order bit no carry, then the final result is located [-16 to -9]
(3) the most significant bit None carry, next highest order bit has a carry, then the final result is located in [8-14]
(4) the most significant bit carry-less, next highest order bit no carry, then the final result is located in [0 to 7]
and interpretation of our unsigned determines the number of in the range of [-8 to 7], how Like at a glance how to judge whether a signed calculation overflow of it!

Then we look at the most common CPU EFLAGS register six flags CF, PF, AF, ZF, SF, OF, CF I only explain and OF, the remaining four are well understood, CF represents two operands highest occurred while binary integer computing the carry, it is clear that can be used to determine not whether the signed overflow, and oF is the symbol into bits whether the register the second uppermost occurs to the most significant bit (binary 1, otherwise 0) of the CF bit XOR value, is not it amazing, is four rules we set out last, just used to determine whether a signed integer overflow.

Behind complement specification is not impossible to imagine in the general book in passing integer calculation has hidden so many principles, it is these characteristics, determine the two's complement code integer calculation processor design, he makes the number of symbols with and unsigned subtraction number with all unsigned adder achieve the greatly simplified circuit implementation, the processor increases the efficiency and reduces the cost of design and manufacturing. However, integer multiplication / division can not deal with this, which is why there multiply and divide instructions and signed addition and subtraction but not, in a certain sense, in fact, just a subtraction instruction packed add instruction, because there is no internal CPU logic subtraction only addition.
https://www.cnblogs.com/effulgent/archive/2011/10/30/two_s_complement.html

Guess you like

Origin www.cnblogs.com/yasepix/p/11490926.html