Original code anti-code and complement detailed

Source: Internet
Author: User

Original code:

The original code is the absolute value of the symbolic bit plus the truth, that is, the first digit represents the symbol, and the remaining bits represent the values. For example, if it is a 8-bit binary:

[+1] original = 0000 0001

[-1] original = 1000 0001

The first bit is the sign bit. Because the first bit is the sign bit, the range of values for the 8-bit binary number is:

[1111 1111, 0111 1111], i.e.:

[-127, 127]

For example, the original code for 5 and-5:

00000000 00000000 00000000 00000101//for 5

10000000 00000000 00000000 00000101//For-5

The computer does not reduce the operation, the subtraction is actually added a negative number of operations, which is why the addition of positive numbers will be faster than the cause of the reduction, the reduction of operations need to do more processing to allow the computer to interpret the added operations, such as 5-5 of computers to be processed into 5+ (-5), The original code in the high with 0 and 1 solved the distinction between positive and negative problems, but also attract new problems, because we all know that 5+ (-5) is equal to 0, but let's look at the result of the operation between the original code:

00000000 00000000 00000000 00000101+10000000 00000000 00000000 00000101

=10000000 00000000 00000000 00001010

10000000 00000000 00000000 00001010 The original code is not what we expect of 0, but -10,5+ (-5) =-10 This is certainly not the result we want, not only so the original code positive high is 0, all negative high is 1, this time there will be two 0 of the case, One is positive 0, one is negative 0, this is obviously not reasonable, 0 is not positive or negative, in order to solve the first problem later introduced a new concept, that is, anti-code.

Anti-code:

The inverse code is represented by: the inverse of the positive number is its own; the inverse code of the negative number is on the basis of its original code, the sign bit is unchanged, the remaining bits are reversed.

[+1] = [00000001] original = [00000001] Reverse

[-1] = [10000001] original = [11111110] Reverse

It can be seen that if an inverse code represents a negative number, the human brain cannot visually see its value. It is usually converted to the original code and then calculated.

The inverse of a positive number and its corresponding negative numbers complement each other, so the inverse of the positive and negative numbers corresponding to the positive numbers is the same.

For example, 5 and-5

00000000 00000000 00000000 00000101//Positive 5 with the same representation as the original code and the normal binary

11111111 11111111 11111111 11111010 The inverse of the//-5 is 5 binary (or the original code, the inverse code can be, because all the same) 0 and 1 reversal

00000000 00000000 00000000 00000101+11111111 11111111 11111111 11111010

=11111111 11111111 11111111 11111111

Here the 11111111 11111111 11111111 11111111 is the 00000000 00000000 00000000 00000000 Anti-code, so is-0, so that the appearance of anti-code to solve the positive and negative numbers of the operation problem, but there is a problem, Is the above 0 is two, a positive 0, all 0, a negative 0, all 1. In order to solve this problem, the concept of complement was introduced.

Complement:

The complement is expressed by: the complement of the positive is itself, the complement of the negative number is on the basis of its original code, the sign bit is unchanged, the rest of you take the reverse, the last +1. (That is, on the basis of the anti-code +1)

[+1] = [00000001] original = [00000001] counter = [00000001] Complement

[-1] = [10000001] original = [11111110] counter = [11111111] Complement

For negative numbers, the complement representation is also the human brain can not intuitively see its value. It is also often necessary to convert the original code to calculate its value.

5 Anti-code: 00000000 00000000 00000000 00000101

5 Complement: 00000000 00000000 00000000 00000101

-5 anti-code: 11111111 11111111 11111111 11111010

-5 Complement: 11111111 11111111 11111111 11111011//is based on the anti-code of 5

00000000 00000000 00000000 00000101+11111111 11111111 11111111 11111011

=00000000 00000000 00000000 00000000//5 complement + (-5 complement) =0 and 0 no sign

Summary: Binary can not be negative, and the original code, although can be negative, but not positive and negative numbers and two 0 problems, anti-code to solve the problem of positive and negative numbers and then still retain the problem of two 0, only the complement perfectly solves the problem of positive and negative numbers, but also solves the problem of two different symbols 0

Original code anti-code and complement detailed

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.