1 's complement 2 's complement 1 ' s complement

Source: Internet
Author: User


1 's complement 2 's complement 1 ' s complement
      This is a description of the TCP Header checksum field (Checksumfield). The complement meaning in the sentence is "complement". For the people who study computer science, the complement is not what is fresh, now the new is this English article appears is "1 ' scomplement", the translation should be "1 of the complement", for this author has not encountered before, to online check it. Online query results, "1 ' scomplement" keyword appeared a lot, but are English keywords, there is no corresponding Chinese translation and interpretation, so first look at English, and finally do their own explanations.     complement: Complement is a computer in the binary number of negative expression method, so you can in the computer to two number of subtraction into addition. The complement form has 1 complement and 2 complement, 1 of the complement is used in the IP, TCP checksum; at ordinary times students in computer science to learn the complement is 2 of the complement (that is, the complement of positive numbers and the original code is the same, negative complement according to the original code corresponding to the number of positive by the position of the Counter plus 1).     is only the usual Chinese textbooks and Chinese translation of the book, there is no more explanation, all translated as complement. For example, "Computernetwork" (Andrew S. Tanenbaum) in China's translation of the "Computer Network" (Tsinghua University Press) for the TCP head checksum is this translation:     (sic) the Checksum algorithm is simply to add up all 16-bitwords in one's complement and then to take the one ' s complement ofthe Sum. The algorithm for     checksum is simply to add all 16-bit words in complementary form and then add and fill them.     carefully compare the sentences that I encountered in the upper part of this article, which means the same as the sentence just now. This is not annotated with the 1 complement, the translation is only "complementary" description, perhaps the translator does not want to be here, because the 1 of the complement is a more troublesome thing, the author here to translate the "1 of the complement" to the translation out, so that everyone notice that this 1 of the complement is not normal to learn that 2 of the complement.     I only discuss translation here, not in the discussion of complement. 1 of the complement is more complex, if interested, you can search the Internet to find RFC1071, which is the official description of the TCP checksum authority.

//======================================================
about the 2 complement

Author: Ruan Yi Feng

Date: August 5, 2009

Ask a basic question.

how negative numbers are represented in the computer.

For example, +8 is represented in the computer as a binary 1000, then-8 how to say it.

It is easy to think that a bits (bit) can be specifically defined as a sign bit, it equals 0 o'clock to represent positive numbers, equal to 1 o'clock to represent negative numbers. For example, in a 8-bit machine, the highest bit of each byte is defined as the symbol bit. So, +8 is 00001000, and-8 is 10001000.

However, if you look for a "computer principle", you will be told that in fact, the computer uses 2 of the complement (two ' s complement) to represent negative numbers.

what is the 2 complement.

It is a numeric conversion method that is completed in two steps:

In the first step, each bits takes the opposite value, 0 becomes 1, and 1 becomes 0. For example, the opposite value of 00001000 is 11110111.

The second step is to add the value from the previous step by 1. 11110111 becomes 11111000.

So, 00001000 of the 2 's complement is 11111000. In other words, 8 is represented by a 11111000 in a computer (8-bit machine).

I do not know what you think, anyway, I think it is strange, why to use such a cumbersome way to express negative, more intuitive way is not good.

Yesterday, I saw this problem in a book, and then spent a little time on the internet to find information, and now finally thoroughly understand.

2 The advantages of the complement

First, be clear. It doesn't really matter how the computer is used to represent negative numbers. You can represent negative numbers in any way, as long as you can maintain a one by one-corresponding relationship. So, since you can arbitrarily choose, you should choose one of the most convenient way.

The 2 complement is the most convenient way. Its convenience now, all the addition operations can be done using the same circuit.

Or 8 as an example.

Suppose there are two ways of representing. One is the intuitive representation, 10001000, and the other is the 2 complement notation, or 11111000. Which notation is more convenient in addition operation.

Write a calculated formula casually, 16 + (-8) =?

The binary representation of 16 is 00010000, so by using the intuition notation, the addition will be written:

00010000
+10001000
---------
10011000

As you can see, if you follow the normal addition rules, you will get a 10011000 result, turn into decimal is-24. Obviously, this is the wrong answer. In other words, in this case, the normal addition rule does not apply to the addition of positive and negative numbers, so it is necessary to make two sets of operation rules, one for positive Gazheng number and one for positive and negative numbers. On the circuit, it is necessary to do two kinds of circuits for the addition operation.

Now, let's look at the 2 complement notation.

00010000
+11111000
---------
100001000

As you can see, 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 shed. So, the result becomes 00001000, and the decimal is exactly 8, which is the correct answer for 16 + (-8). This shows that the complement notation of 2 can extend the addition rule to the whole set, so that the addition of all integers can be achieved with a set of circuits.

The essence of the complement of 2

Before replying to the 2 's complement for the correct addition operation, let's look at its essence, which is how the two steps are converted.

To turn a positive number into a corresponding negative, you can actually use a 0 minus that. For example,-8 is actually 0-8.

The binary of the known 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 should we do? Very simple, ask the last one to borrow 1 on it.

So, 0000000 also asked the last one to borrow 1, that is to say, the meiosis is actually 100000000, the formula is changed to write:

100000000
-00001000
---------
11111000

Further observation, you can find 100000000 = 11111111 + 1, so the above equation can be broken down to two:

11111111
-00001000
---------
11110111
+00000001
---------
11111000

2 of the complement of the two conversion steps is so.

Why is positive addition applicable to 2 complement.

In fact, what we have to prove is that XY or x+ (-y) can be done with X plus 2 of Y's complement.

The 2 complement of Y is equal to (11111111-y) +1. So, X plus the 2 complement of Y is equal to:

X + (11111111-y) + 1

We assume that the result of this formula is equal to Z, that is, z = X + (11111111-y) + 1

Next, divide into two kinds of situation discussion.

In the first case, if X is less than y, then Z is a negative number. At this point, we will use the inverse of the 2 complement of Z, find out its corresponding positive value, and then add a minus sign on the line. So

Z =-[11111111-(Z-1)] =-[11111111-(X + (11111111-y) + 1-1)] = x-y

In the second case, if X is greater than Y, this means that Z is definitely greater than 11111111, but we specify that this is a 8-bit machine, the highest 9th bit is the overflow bit and must be shed, which is equivalent to subtracting 100000000. So

Z = Z-100000000 = X + (11111111-y) + 1-100000000 = x-y

This proves that, under the normal addition rule, 2 of the complement can be used to get the correct result of the addition of positive and negative numbers. In other words, the computer can complete the addition of all integers as long as the addition circuit and the complement circuit are deployed.

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.