The original code, the reverse code, and the complement code, which are negative in the computer.

Source: Internet
Author: User

Original code: converts an integer to a binary value, which is the original code. For example, the original code of 5 in a single byte is 0000 0101, and the original code of-5 is 1000 0101.
Reverse code: the reverse code of a positive number is its original code. The reverse code of a negative number is to reverse each digit in the original code except the symbol bit. For example, the bitcode of 5 in a single byte is 0000 0101, And the bitcode of-5 is 1111 1010.
Complement: the positive complement is the original code, and the negative inverse code + 1 is the complement code. For example, the complement code of 5 in a single byte is 0000 0101, and that of-5 is 1111 1011.


In a computer, positive numbers are expressed directly by the original code. For example, if a single byte is 5, the positive number is expressed as 0000 0101 in the computer. A negative number is expressed by a supplementary Code. For example, a single byte of-5 is expressed as 1111 1011 in the computer.
Here is a question: why is a negative number in a computer expressed by a supplementary code? Why not use the original code? For example, a single byte-0101.
I want to consider the software for two reasons:

1. Range
Taking a single byte Integer as an unsigned type, the value range is [0,255], indicating a total of 256 data records. The signed type, which indicates the range is [-128,127].
First look at the unsigned, 0 represents 0000 0000,255 1111 represents 1111 256, just meet the requirements, can represent data.
If the signed one is used as the original code, the value 0 indicates 0000 000. Because we have symbols, we should also have a negative 0 (although it is still 0): 1000 0000.
Then let's see if this still meets our requirements, indicating 256 data records?
Positive number, no problem. 127 is 0111 1111,1 is 0000 0001. Of course, there should be no other problems.
What about negative numbers?-1 is 1000 0001, so remove the negative number. The maximum number is 111 1111, that is, 127. Therefore, the minimum value of negative numbers is-127.
This does not seem to be too complex. How do I represent-128? It seems that the original code cannot be used for representation, but we have two zeros.
If we specify a 0 as-128, isn't it? This is also an idea, but there are two problems: one is that the span between it and-127 is too large; the other is that it is not convenient to use hardware for computation.
Therefore, in a computer, the negative number is indicated by a supplementary code. For example, if a single byte-1, the original code is 1000 0001, the reverse code is 1111 1110, And the complement code is 1111 1111, the single byte-1 in the computer indicates 1111 1111.
Single-byte-127, the original code is 1111 1111, the reverse code is 1000 0000, the complement code is 1000 0001, and the single-byte-127 in the computer is 1000 0001.
Single-byte-128, the original code does not seem to be expressed, except for the symbol bit, the maximum number can only be 127, and its representation in the computer is 1000 0000.

2. Size habits (personal opinion)
You can also understand the data size. Take single-byte data as an example. In a signed number, the positive number ranges from 1,127 to 127, and the maximum value is 111. If the symbol is not considered, it is expressed as 1111. If the smallest value is 1, the symbol is not considered, the value is 0001.
In a negative number, the maximum value is-1, and we use 111 1111 to represent its numerical part. The subsequent data is reduced by 1 in sequence. When it is reduced to 0001, we use it to mark-127. After subtracting 1, it becomes 0000. Fortunately, we have two 0 s. Take the 0 with the symbol, indicating-128, which can satisfy the expression range.


The above is only an analysis from the software perspective. Of course, from the hardware perspective, there is also a reason for using a negative number to represent it. After all, in the computer, the final operation is still the hardware. There are three main reasons:

1. The conversion between a negative complement and its corresponding positive complement can be completed using the same method ---- complement operation to simplify hardware.
For example:
Source code anti-Code complement
-127-> 127 1000 0001-> 0111 1110-> 0111 1111
127->-127 0111 1111-> 1000 0000-> 1000 0001
-128-> 128 1000 0000-> 0111 1111-> 1000 0000
128->-128 1000 0000-> 0111 1111-> 1000 0000
We can find that the negative number and positive number complement methods are the same.

2. subtraction can be changed to addition, saving the subtraction.
In the computer, we can see that the result is a negative number corresponding to the value. The same is true for negative numbers.
In the calculation, if a number is subtracted, it is equal to the opposite number. This elementary school has learned. Since its complement is the opposite number, we can add it.
For example, a-127,
This is equivalent to: A + (-127 ),
Because the negative number is saved in the form of a complement, that is, the true value of the negative number is the complement code. In this case, when we want to subtract a number, we will take it directly and add it, that's OK. We can rest assured to say goodbye to subtraction!
Of course, this also involves the problem of type conversion. For example, if a single byte is 128, its original code is 1000 0000, and its complement code is 1000 0000. In this case, we add 128 or-128 to the sum of 1000 and 0000. Isn't this confusing? Okay, various Programming Language .
For example: (assuming constants are single-byte)
1 + 128, the true value is calculated as 0000 0001 + 1000 0000. If you assign the result to a single byte with a signed positive number, the editor will prompt that you have exceeded the representation range. Because the two data in the operation are unsigned, the result is also 129 of the unsigned, and the maximum value of the signed single-byte variable can be 127.
1-128, the truth calculation is 0000 0001 + 1000 0000, because-128 is signed, and its calculation result is also signed, 1000 0001, which is exactly the true value of-127 in the computer.

3. unsigned and signed addition operations can be completed using the same circuit.
The addition and subtraction of signed and unsigned values are actually the sum of their true values. The true value, that is, the binary representation of a value in a computer. The true value of a positive number is its original code, and the true value of a negative number is its complement code. Therefore, there are symbols and unsigned characters controlled by the compiler. What the computer wants to do is to take the two true values and add them together.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/njuitjf/archive/2009/09/23/4585247.aspx

Related Article

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.