Comprehensive Understanding of code complementing

Source: Internet
Author: User

I. Glossary:

Complement:

1. in a computer system, all values are represented (stored) by supplementary codes ). the complement code can be used to process the symbols and other bits in a unified manner. At the same time, the subtraction can also be processed by addition. in addition, if the maximum bit (symbol bit) has an incoming bit when two data values are added, the carry is discarded.

2. The replacement process is almost the same as that of the original code.

The value complement representation is also divided into two situations:
(1) positive complement: same as the original code. For example, the complement of + 9 is 00001001

(2) Complement of negative numbers: the sign bit is 1, and the rest is the original code of the absolute value of the number is reversed by bit; then the whole number is added to 1
For example, the-7 complement: because it is a negative number, the symbol bit is "1", the whole is 10000111; the remaining 7 digits are the absolute values of-7 + the original code 0000111 of 7 is reversed to 1111000 by bit; plus 1, the complement code of-7 is 11111001.
If you know the complement of a number, you can perform the following operations on the source code:

(1) If the sign bit of the complement code is "0", it indicates a positive number. Therefore, the complement code is the original code of the number.
(2) If the sign bit of the complement code is "1", it indicates a negative number. The operation to evaluate the source code can be: the sign bit is 1, and the rest will be reversed, and then the entire number is added to 1.
For example, if we know that a complement code is 11111001, the original code is 10000111 (-7): Because the symbol bit is "1", it indicates a negative number, so this bit remains unchanged and is still "1 "; the other 7 digits are reversed to 1111001; then 1 is added, so it is 0000110.

Ii. representation range of the complement code (understanding)

It is easier to understand without symbols. I will not talk about it here. The key is to consider the signed situation.

The maximum value of an 8-digit complement code is + 127. This is easy to understand and I will not talk about it anymore.

However, the minimum value is-128, which is hard to understand. So let's talk about the understanding of kaiwii.

-128 can be expressed as: 10000000. However, according to the first section, shouldn't this number be-0?

Xuan Miao is here. What is the difference between + 0 and-0? Actually, it's not the same. In a computer, two things are not used to express the same things (if so, it does not offend the computer's golden law: Is it necessary to make sense ?)

Therefore, since it is useless, we use the 10000000 originally-0 to represent-128, while + 0 is actually 0.

Another question is, why can't we use 0000000 to represent-128? Because 10000000 actually has a lot to do with-128. If we do not consider overflow, add one to nine digits to this number. In this case, the binary value of the number is 110000000. Have you found out? Isn't this a-128 complement?

So, to sum up:

1. For any length of the complement code, the binary number originally expressed as-0 is moved to the minimum value of the negative number.

2. the absolute value of the minimum value of a negative number is always greater than the maximum value of a positive number.

Iii. complement operation

The complement operation includes addition and subtraction. In terms of form, the two are actually the same, from small to big, each digit is added, more than one is forward into one.

Note that:

1. During calculation, the calculated digit includes the symbol bit.

2. Consider overflow. If it exceeds the highest bit (symbol bit), ignore it!

Iv. motivation for the invention of code complementing

In other words, there is no subtraction in the computer. In section 3, subtraction is the same as addition if the number is represented by a supplementary code. Therefore, by completing the code, subtraction becomes addition.

V. How Code complement works

In section 4, when we talk about code population, addition and subtraction operations will become the same thing. Why? This is the role of the legendary model.

Next we will talk about the principles of the complement operation by combining the modulo.

"Module" refers to the counting range of a metering system. such as clock. A computer can also be seen as a metering machine. It also has a metering range, that is, there is a "module ". for example:
The Metering range of the clock is 0 ~ 11, modulo = 12.
The N-bit computer metering range is 0 ~ 2 (n)-1, modulo = 2 (n). [Note: N indicates the index]
The "module" is essentially the amount of overflow produced by the metering device. Its value cannot be expressed on the metering device, and the metering device can only represent the output mode.
Remainder. Any modulo metering device can be reduced to addition.
For example, if the current hour hand points to 10 and the exact time is 6, there are two ways to adjust the time:
One is a four-hour switch, that is, 10-4 = 6.
The other is to distribute the traffic for 8 hours: 10 + 8 = 12 + 6 = 6
In a 12-mode system, the effect of adding 8 is the same as that of subtracting 4. Therefore, any operation of subtracting 4 can be replaced by adding 8.
In terms of "modulo", 8 and 4 complement each other. In fact, in a 12-module system, 11, and 6 both have this feature.
The common feature is that the sum of the two equals the modulo.
For a computer, the concept and method are exactly the same. N-bit computers with n = 8 can represent a maximum of 11111111.
The addition of 1 is called 100000000 (9 bits), but because there are only 8 bits, the maximum bit 1 is naturally lost, And 00000000 is returned, so the 8-bit binary system
The modulo is 2 (8). In such a system, the subtraction problem can also be converted into an addition problem. You only need to use the corresponding population to represent the reduction.
The complement is used to process the computer logarithm.

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.