Explanation and misunderstanding of C language code complementing

Source: Internet
Author: User

 

In the C language textbooks in Chinese, some people are confused by the original code, reverse code, and complement code. The following articles are well written and explained ......

(In fact, I also think that anti-code and other things are defined by some people who make their own smart work. Instead, they make people dizzy and sometimes simple)

Body start:

I have read some books and online articles about code complementing, and I have repeatedly stressed the methods and steps of "Seeking for Inverse and adding one". However, I have not discussed the essence and definition of code complementing. This is misleading for beginners, and many people are entangled in the process of obtaining the-128 complement code.
We are all explaining the anti-code and the original code. Actually, people who have learned it know that their importance is 0!
As a saying goes, I will write down my understanding of the supplementary code below. I hope it will be helpful to readers.

Adder
In the computer, there is only a multiplier, and there is no subtraction. All subtraction operations must be performed using addition.
That is, the calculation of subtracting a number (or adding a negative number) should be studied how to use addition.

Modulo and population
In daily life, we can see many such things:
Turning an object 90 degrees left is the same as turning the object 270 degrees right without considering the number of circles;
If you change the minute-hand to 20 minutes, and then 40 minutes, the effect is the same without considering the hour-hand;
When the number 87, 25, and 75 are subtracted, the effect is the same without considering the number of hundred bits;
.......
The preceding numbers have the following relationship:
90 plus 270 = 360
20 + 40 = 60
25 + 75 = 100
360, 60, and 100 are "modulo" (which can also be understood as "hexadecimal ").
90, 270, 20, and 40, and 25 and 75 are complementary figures.

Knowing the "modulo", it is easy to find the "Population" of a number:
If the modulo is 365, the number 120 must be 365-120 = 245.

Replace the original number with the complement number to convert the subtraction to addition. The occurrence of carry is a modulo. At this time, carry should be ignored.

Binary modulo
The preceding Decimal Numbers 25 and 75 are two-digit operations. The modulo is 100, that is, two zeros are added after 1.
If three digits are involved in the operation, the modulo is 1000, that is, three zeros are added to the end of 1.
Here, 1000 is one thousand of the decimal number. It can be written as 10 ^ 3, that is, the 3rd power of 10.
Inference: the number of digits involved in the operation is the number of digits after 1.

The same is true for binary numbers.
If it is a three-digit binary number, the modulo is 1000, that is, three zeros are added to the end of 1;
When the 8-bit binary number is involved in the operation, the modulo is 1 0000 0000, that is, 8 zeros are added after 1.
When the 16-bit binary number is involved in the operation, the modulo is large, that is, 16 zeros are added to the end of 1.
Note: The values 1 and 0 mentioned here are binary numbers.
The 8-bit binary number modulo can be written in decimal format as 2 ^ 8, that is, 256.
The 16-digit binary number is 2 ^ 16. It is 65536 in decimal format.

Complement the binary number
Calculates the complement of binary numbers to store them in the computer.
In a computer, all the numbers stored are called machine codes. In binary form, the number is changed to the complement code.
Generally, the complement code is discussed based on the 8-bit binary number. A few 16-digit complement codes are also useful.

If a positive number is added during calculation, the population is not required. Only subtraction (or negative addition) is required.
The complement is defined according to this requirement: the positive number remains unchanged, and the negative number is calculated by the modulus minus the absolute value.

An X number is known.The 8-character-length complement is defined:

     (1) X;      0 <= x <= + 127 (positive number and 0 complement, that is, the number itself)
 [X] fill =
(2) 2 ^ 8-| X |;   -128 <= x <0(The complement code of a negative number is 1 0000 0000 (Power 8 of 2), minus the absolute value of the number)

For example, if X =-126 and its complement code is 1000 0010, the calculation method is as follows:

1 0000 0000
-01111110
-----------
1000 0010

It can be seen that, according to the definition of the complement code, the concept is very clear, and the method and steps are very simple.

Application complement for Calculation
Calculated using the complement code: 83-25 = 58.

83 --- All are converted into supplementary codes, and then use addition operations --> 0101 0011
-25-> 1 0000 0000-0001 1001-> + 1110 0111
-----                              --------
58 <-- ignore carry 1, and the result is correct -- [1]0011 1010

If the calculation result exceeds-128 ~ In the range of + 127, the result will be incorrect, which cannot be corrected.

The application uses the complement code for computation, which fully complies with the previous practice of "using the complement number to convert subtraction to addition", as long as the carry 1 is ignored (this carry 1 is the time for completing, the result is correct in 1 0000 0000.

The definitions, methods, and steps of population and complement code can be summarized if you understand the previous text.
So why do some netizens always ask questions about code complementing?
In my opinion, it is because many teaching materials and texts are "Superfluous" on this issue ".

About the bucket
After the occurrence of the complement code, the future generations added a lot of "snake foot": Symbol bit, reverse addition, original code, reverse code .......
The following table provides 8-digit complement codes.

 

-- Symbol bit
From this table, we can see that the highest bit of a positive number is 0, and the highest bit of a negative number is 1.
In this way, someone will understand the highest bit as a symbol bit. 0 indicates the positive number ,....... In addition, the supplementary explanation of the serious issue is: "The symbol bit also participates in the operation ". Really fooled! Turning and selling cars are all easy to beat.
In fact, in the definition of population and complement, there is no sign bit at all.. The 1 and 0 values of the highest digit are natural, not defined by humans.
-- Reverse Addition
The last seven digits of the negative complement can also be seen as an incomplete rule: there is a "inverse addition" relationship between them and the absolute values.
So, againSomeone introduced this algorithm different from the definition..
-- Original and reverse code
Because we use "Seek inverse and add one" to obtain the complement code, by the way, we introduce two junk concepts: the original code and the reverse code.

In fact, the calculation method of "inverse addition" is only applicable to the calculation of binary complement, which is not universal.
In addition, the "reverse addition" is used to obtain the-128 complement code. This is an overflow phenomenon. Many people have been listed here for a long time.
The original code and reverse code are just "manually"The intermediate process of inverse addition does not exist in the computer, so they are useless.

Practical suggestions
To obtain the complement code, the negative number is calculated by the modulo minus the absolute value according to the definition. This is a general method for calculating the complement number and is suitable for various hexadecimal and various numbers.
Do not use the reverse addition method, or ignore the source code and the reverse code, and do not involve the symbol bit.
In future computing, there is no need to note that "symbol bits are involved in the calculation...", because there is no symbol bit at all.

If you delete the original and reverse code, symbol bit, and other junk concepts from the computer book, it will be effort-saving to learn the supplementary code.

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.