Principle and proof of the negative value of two's Complement

Source: Internet
Author: User
Tags modulus

(From: http://blog.pcware.cn/linux/tows_complement_proof.html)

In this article, com refers to complement, neg refers to negative, and this article involves "2's complement" (two's
Complement) instead of "1 Completion Code" (one's complement)

Most people who have learned about computers know that negative numbers are represented by supplementary codes in their computers. However, most textbooks and articles simply tell you that the complement code of a negative number is equal to the opposite code and then the cloud, for
What is so, it is basically not detailed.

The advantage of using the complement expression for negative numbers is that subtraction can be converted to addition, which simplifies the hardware design. The CPU can perform addition and subtraction operations only by using one adder.

Next I will try to prove why the complement code of the negative number is equal to the inverse code plus one.
Understanding the derivation below requires that the reader must understand the concept of modulus and evaluate the modulo operation.

Assume that our operation uses N-bit binary numbers, the modulus of these N-bit binary numbers is 2n.
, Shuα is a constant expressed in n-bit binary, and number X is a n-bit binary table.
The value of α <2N
, X <2N
Yes. Here, both α and X are represented by the original code. Now
We deduct X from α and deduct it as follows:

α-x = 2n
% 2N
+ (α-x) % 2N
= (2N
+ (α-x) % 2N
= (α + 2N
-X) % 2N
= (α) % 2N
+ (2N
-X) % 2N
= α + (2N
-X)

Now we have successfully evolved the subtraction operation α-X into α + (2N
-
X) This addition operation. From the concept of modulus, we know that if the sum of two numbers is equal to its modulus, the two numbers are complementary. Here X and 2n
-
X is complementary. subtraction X and addition of the population 2N
-
X is equal. This also hides a layer of meaning. If a positive number is added with a negative number, simply dropping the carry will not affect the calculation result.
We come to the first conclusion:
Xcom
= 2n
-X

The reverse code is much simpler, and the reverse code is obtained when all the binary bits of a number are reversed. We can see that if a number is added with its reverse code, all the binary bits are full, that is, all values are 1 and the value is 2n-1.
+ 2N-2
+... + 22
+ 21
+ 20
=
2N
-1
We come to the second conclusion.
Xneg
= 2n
-1-x

In conclusion 1 and 2, we can make the following derivation:
Xcom
= 2n
-X
= 2n
-1-x + 1
= Xneg
+ 1

So far, we come to the final conclusion that the complement of an n-bit binary number is equal to its inverse code plus one.

Let's look back and see, we once said "X and 2n
-X is complementary. subtraction X and addition of the population 2N
-
X is equal. If we subtract X, we can consider it as adding-X, that is, adding-X and adding 2n.
-X "is equivalent. in the computer,-X uses 2n.
-X completes its complement. Note that this is a complement rather than a complement.

Now we replace X with 1, n with 8, and the-1 complement code is 28.
-1 = 255 = 0xff, which is the true face of-1 in the computer. Of course
A positive number is a different theory.

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.