Java source code, reverse code, and complement code

Source: Internet
Author: User

Why?

The computer uses binary to represent a value. For example, decimal number 10 is expressed as 1010 in binary number. Set the computer to 8 characters, that is, 1 byte. The highest digit indicates the symbol. 0 is positive and 1 is negative.
Let's take a look at the problems with the addition, subtraction, multiplication, division, and number represented by the original code:

  • Decimal: 1-1 = 1 + (-1) = 0
  • Original Code: (0000 0001)-(0000 0001) = (0000 0001) + (-0000 0001) = (0000 0001) ORIGINAL + (1000 0001) original = (1000 0010) original =-2, apparently incorrect

The above operation shows that the original code operation cannot produce correct results in some cases for negative numbers (I .e., signed numbers, positive numbers.
Let's take a look at the problem with the reverse code representing the addition and subtraction of multiplier operations:

  • Decimal: 1-1 = 1 + (-1) = 0
  • Reverse Code: (0000 0001)-(0000 0001) = (0000 0001) + (-0000 0001) = (0000 0001) reverse + (1111 1110) reverse = (1000 0000) inverse =-0, negative 0.

Generally, we do not distinguish between + 0 and-0. This problem occurs when we use the reverse code.
Finally, let's look at the complement operation:

  • Decimal: 1-1 = 1 + (-1) = 0
  • Complement: (0000 0001)-(0000 0001) = (0000 0001) + (-0000 0001) = (0000 0001) supplement + (1111 1111) Supplement = (0000 0000) fill = 0

The complement operation has its own unique advantages. There is no problem of source code or reverse code when using the complement operation together with the symbol bit. This is why modern computers generally use the complement operation.

Calculation method:

Because the positive number addition and subtraction operation does not have this problem, the positive number does not have an anti-code, complement code, or the original code, anti-code, or complement code of the positive number is the same

Original code: the negative original code is 1 | x |

Negative code: Negative negative code. The symbol bit remains unchanged. The value is reversed.

Complement: Add 1 to the negative code (it will affect the symbol bit)

Scope

Here, byte is used as an example to explain the value range. Byte occupies one byte, which is 8 bits. If it is an unsigned integer, the maximum value of the original code is 11111111, so the original code range is 0---255 (that is, 0 ~~ 2 ^ 8-1), with a total of 256 numbers.

For byte values of the symbolic type, the maximum value of the original code should be 01111111 (the first 1 represents a negative number), and the minimum value should be 11111111, so the original code range is (-127 ~ -0 + 0 ~ 127), a total of 256. However, when adding or subtracting a signed original code (the original code operation is incorrect and the reverse code is + 0 or-0), a complementary code is generated. For decimal: 127 + 1 =-128, the Complement Algorithm: 01111111 + 00000001 = 10000000, here the 10000000 original code is-0, so in the computer, the 1000000 complement represents-128. It should be noted that-128 does not have the original code or reverse code, but only the complement code!

Summary:

1> the purpose of code complementing is:

(1) Enable the symbol bit to join the operation with the valid value to simplify the operation rules. The sign bit in the number of complement machines is not imposed. It is a natural component of the data and can be involved in operations normally.

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

2> the reverse code is an intermediate transition between the original code and the complement code, and is rarely used.

3> all these conversions are performed at the very bottom of the computer, and all the original codes are used in the assembly, C, and other advanced languages we use.

 

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.