The function and difference of original code, inverse code, complement and shift code

Source: Internet
Author: User
Recently reported a soft test, the basic knowledge of evil, the previous learned the original code, anti-code, the complement unexpectedly forget, there are not familiar with the shift code, deliberately spend some time to organize, hope to help everyone.
Concept Introduction:The information in the computer is expressed in binary form, the value is positive or negative, and the computer holds the symbol with the highest bit of a number (0 is positive and 1 is negative). This is the number of machines. Original CodeThe The number of bits that the machine can handle is 8. That is, the word length is 1byte, the original code can represent the range of values ( -127~-0 + 0~127) a total of 256.
It is possible to perform arithmetic operations on the logarithm with a numeric representation. However, it was soon discovered that the multiplication operation was correct with the original code with the sign bit, and there was a problem with the addition and subtraction of the operation, as follows (assuming the word length is 8bits): (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, it is found that the problem appears on the negative number with the sign bit, and the negation of the rest of the symbol bit is generated Anti-code。  The value space of the inverse code is the same as the original code and corresponds to one by one. Here is the subtraction of the Inverse code: (1) 10-(1) 10 = (1) + + (-1) 10 = (0) 10 (00000001) anti + (11111110) anti = (11111111) inverse = (-0) A little bit of a problem. (1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10 (00000001) inverse + (11111101) reverse = (11111110) reverse = (-1) correct
The problem arises on +0 and 0, and 0 is not positive or negative in people's computing concepts. So we introduced the complementConcept. The complement of negative numbers is to add one to the inverse code, and the positive number of the original code is the same as the reverse code complement. In the complement (-128) instead of (-0), so the complement of the expression range is: ( -128~0~127) a total of 256.
Here are the complementary plus and minus operations: (1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10 (00000001) complement + (11111111) complement = (00000000) complement = (0) correct (1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10 (00000001) complement + (11111110) complement = (11111111) complement = (-1) correct
The original code, the inverse code, the complement are the symbolic fixed-point number representation method (daily life usually uses the signed number, the fixed-point number corresponds to the floating-point numbers, namely the decimal), the inverse code, the complement is to simplify the binary number subtraction operation; Shift CodeCommonly used to compare the size of the floating-point code is generally used to express the order of the Code, said a little more popular, you put the value of the shift code to show that they can see their size at a glance. This makes it easy to determine the size of the order code, which can be used to simplify the multiplication operation of floating-point numbers.
function and Purpose:
Inverse code: Solve the problem of negative addition operation, the subtraction operation is converted to addition operation, thus simplifying the operation rules; complement: Solve negative addition operation plus or minus 0 problem, make up the lack of anti-code. In short, the anti-code and the complement are to solve the problem of negative numbers, and positive, so, whether it is a positive integer or positive decimal, the original code, anti-code, complement are all the same.
Summary:1, positive number of the original code, complement, anti-code are its own, 2, negative (binary) of the original code, complement, anti-code formula: Inverse Code = original code (except for the sign bit) each bit is reversed complement = anti-code + 1 Anti-code = complement-1 Shift code = complement symbol position inversion

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.