Idle original code, reverse code, and supplementary code)

Source: Internet
Author: User

I believe that everyone will not think of this title, because they have their explanations in the first chapter of any basic computer knowledge book, but we can only find some simple definitions in the book, I forgot it soon after I saw it. I have been asked about these concepts in the Forum recently. Many people have replied that they have read and forgotten to read such books before, and seldom give a reasonable explanation. So I started to think about it (although God will laugh, I still need to think about it .), The following conclusions are drawn.

A numerical value is expressed as a machine number in a computer. A computer can only recognize 0 and 1. Binary is used, while decimal is used in daily life, "As Aristotle has long pointed out, the widespread use of decimal today is only the result of the fact that most of us have 10 fingers in our lives. although in history, finger counting (hexadecimal) has appeared later than two or three hexadecimal counting. "(from <mathematical development history> if you have time, you can see it ~, (Very interesting). In order to facilitate binary conversion, the hexadecimal (2 4) and octal (23) are used. Next we will go to the topic.

In a computer system, all values are represented (stored) by supplementary codes ).
The main reason is that the complement code can be used to process the symbol bit and other digits in a unified manner. At the same time, the subtraction can also be processed by addition. The other two are supplemented.
If the highest bit (symbol bit) has a forward bit, the carry is discarded.
The conversion process of the complement code is almost the same as that of the original code.
The value complement representation is also divided into two situations:
(1) positive complement: the same as the original code.
For example, the complement code 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 other seven digits are the absolute value of-7 + the original code of 7
The bitwise decimal value of 0000111 is 1111000, and the value 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 are 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, and then 1 is added, so it is 0000110.

Values have positive and negative values, and the computer uses the highest bit of a number to store the symbols (0 is positive, 1 is negative ). this is the original code of the number of machines. assume that the number of digits processed by the machine is 8. that is, the word length is 1 byte, and the original code can indicate the value range is

(-127 ~ -0 + 0 ~ 127) a total of 256.

With the numerical expression method, you can perform arithmetic operations on the logarithm. however, it was soon discovered that the result was correct when the original code with a symbol bit was used for multiplication and division operations, and a problem occurred during addition and subtraction operations, as shown below: Assume the length is 8 bits

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) ORIGINAL + (10000001) original = (10000010) original = (-2) is obviously incorrect.

Because there is no problem in the addition operation of two integers, the problem occurs on a negative number with a symbol bit, A reverse code is generated for all the characters except the symbol bit. the anticode value space is the same and one-to-one correspondence with the original code. the following is the subtraction operation of the anticode:

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) + (11111110) = (11111111) = (-0) there is a problem.

(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10

(00000001) + (11111101) = (11111110) = (-1) Correct

The problem occurs in (+ 0) and (-0). In people's computing concepts, Zero is not divided into positive and negative. (Indians first mark zero as a mark and put it into the computation. The Indian mathematical and decimal counts with zero numbers contribute a lot to human civilization ).

So the concept of Code complement is introduced. the complement of a negative number is to add one to the anticode, while the positive number remains unchanged. The original positive number is the same as the reverse code. (-0) is replaced by (-128) in the complement code, so the range of the complement code is:

(-128 ~ 0 ~ 127) a total of 256.

Note: (-128) there is no corresponding source code and anticode. The addition and subtraction operations of (-128) = (10000000) are as follows:

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10

(00000001) Fill + (11111111) Fill = (00000000) Fill = (0) Correct

(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10

(00000001) Fill + (11111110) Fill = (11111111) Fill = (-1) Correct

Therefore, the purpose of code complementing is:

(1) Enable the symbol bit to join the operation with the valid value to simplify the operation rules.

(2) convert the subtraction operation to addition, further simplifying the line design of the calculator in the computer

All these conversions are performed at the bottom layer of the computer, and all the original codes are used in the assembly, C, and other advanced languages we use. After reading the above, you should have a new understanding of the original code, anti-code, and complement code!

Source: http://blog.csdn.net/fengzi_zhu/archive/2003/03/31/14337.aspx

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.