About twos complement

Source: Internet
Author: User

Ext.: http://www.ruanyifeng.com/blog/2009/08/twos_complement.html

Ask a major question.

How do negative numbers show up in the computer?

For example, +8 is represented in the computer as a binary 1000, so what does 8 say?

Very easy to think that a bits (bit) can be specified as a sign bit, it equals 0 o'clock represents a positive number, equals 1 o'clock represents a negative number. For example, in a 8-bit machine, the highest bit of each byte is specified as the sign bit. So, +8 is 00001000, and-8 is 10001000.

However, to find a "computer principle", will tell you, in fact, the internal use of the computer with a complement (complement) to indicate negative numbers.

What is a complement?

It is a numeric conversion method that takes two steps to complete:

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 1 to the value from the previous step. 11110111 becomes 11111000.

So, 00001000 of the complement is 11111000. In other words, 8 is represented in the computer (8-bit machine) with 11111000.

I don't know what you think, anyway, I think it's very strange, why is it not good to have a more intuitive way of expressing negative numbers in such a troublesome way?

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

The merit of the complement

First of all, be aware of a point. In fact, it does not matter what method is used to represent negative numbers inside the computer. To be able to maintain one by one corresponding relationships, you can represent negative numbers in a casual way. Therefore, since can choose freely, then should choose one of the most convenient way.

Complement is the most convenient way. Its conveniences today, all addition operations can be completed using the same circuit.

or 8 as a sample.

Assume that there are two ways of representing it. One is the intuitive notation, or 10001000, and the other is the 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

Can see, assuming according to normal addition rule, will get 10011000 result, turn into decimal is-24. Obviously, this is the wrong answer. That is, in such a case, the normal addition rule does not apply to the addition of positive and negative numbers, so two sets of operation rules must be established, one for the positive home plus number, and the other for positive plus negative numbers. From the circuit, it is necessary to do two kinds of circuit for addition operation.

Now, look at the complement notation.

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

Can see that, 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 self-initiated. So, the result turns into 00001000, and turns into decimal exactly 8, which is the correct answer for 16 + (-8). This shows that the complement notation is able to extend the addition operation rule to the entire set of integers, so that all integer additions can be achieved with a set of circuits.

The essence of complement

Before answering the question of how the complement can be correctly implemented, let's look at its nature, that is, how the two-step conversion approach came about.

To turn a positive number into a negative one, in fact it is only possible to subtract this by 0. For example, 8 is actually 0-8.

It is known that the binary of the 8 is 00001000,-8 and can be calculated using the following formula:

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

Since 00000000 (meiosis) is less than 0000100 (meiosis), it is not reduced enough. Please review the elementary school arithmetic, suppose one of the minuend is less than the meiosis, what do we do? Very easy, ask the last one to borrow 1 to be able to.

Therefore, 0000000 also asked the last one borrowed 1, that is, the deduction is actually 100000000, the formula is also changed to write:

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

Further observation, can be found 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.

Why does positive addition apply to complement?

In fact, what we want to prove is that X-Y or x+ (-y) can be finished with the complement of Y.

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

X + (11111111-y) + 1

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

Next, there are two kinds of situations to discuss.

In the first case, assuming that X is less than y, then Z is a negative number. At this point, we will use the inverse of the complement of Z to find its corresponding positive value, and then add the minus sign in front. So

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

In another case, assuming X is greater than Y, which 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 abandoned, which is equivalent to minus 100000000. So

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

This proves that, under the normal addition rule, the correct result of adding positive and negative numbers can be obtained by using the complement. In other words, the computer simply deploys the addition circuit and the complement circuit to complete the addition of all the integers.

Finish

Wonderful comments:

z = X + (11111111-y) + 1 can be written as Z = XY +100000000, which can be understood in hardware as two parts of the circuit to achieve, the first part is the front of the X-y (here, regardless of whether the result of the calculation is positive or negative), the second part is the results of X-y calculation and 100 000000 Add, finally get the result of the calculation z, and on the 8-bit computer 100000000 can not appear, in fact, 100000000 is equivalent to 00000000 (the highest bit), and then we look at some of the calculation process:
Z = X + (11111111-y) + 1
= x-y + 100000000
= x-y + 00000000
= X-y
The certificate is completed.
In this way we prove that X-Y or x+ (-y) can be finished with 2 of the complement of Y, without having to prove it in two different cases.

About twos complement

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.