Current location - Training Enrollment Network - Mathematics courses - Complementary mathematical representation
Complementary mathematical representation
The original code, complement, complement and subtraction are introduced as follows:

As we all know, computers store data in binary, and different data types may store different formats. For example, a signed integer, the highest bit is the sign bit, and the rest represents the numerical value; So how to express it in the computer? Let's say it step by step:

How to express 1+ 1?

The binary expression of 1 is (1 byte for convenience): 0000000 1.

Then 1+ 1 is expressed as: 0000001+0000001= 000000/kloc-0 = 2.

Right? Yes, it's so easy.

2.? Then look at the following, how to express 2- 1

The binary of 2 is: 00000 10.

The binary of-1 is 10000 1.

Then: 2-1= 2+(-1) = 0000010+100001=10000/kloc.

3.? What is the original code?

Definition of Wikipedia: Real form is a term of computer operation, which refers to "unaltered" code. For example, the binary expression of 2 is 000000 10, and 00000 10 is the original code.

4. What is complement?

Wikipedia's definition: two's complement (English: two's complement) is a method of expressing signed numbers in binary.

Complement is calculated according to the original code rules:

The complement of positive numbers and 0: the number itself.

Complement of negative number: divided by most significant bit (i.e. sign bit) and added with 1.

So what is inversion? 0 becomes 1, 1 becomes 0, which is the code.

For example,-1, the original code is: 10000 1.

Step 1, most significant bit is removed to obtain:1111110.

Step 2: Add1:11111.

The last complement of-1 is:11111.

By the way, in the process of transforming complement, there are still steps: reverse order. The number obtained by inversion is called inverse code.

5. What is anti-code?

Anti-code is the machine code representation of numbers in a computer. For a single numerical value (binary 0 and 1), the inversion operation is to change 0 to 1 and 1 to 0.

The inverse code is the same as the original code when it is positive; When the number is negative, the inverse code is the sign bit of the original code, and the other bits are inverted bit by bit.

Anticode is only a transitional operation in the process of conversion between original code and complement. Know the original code and complement

In fact, we know what anti-code is, just abstracting it.

6. How exactly is subtraction done?

Having finished the original code, complement, complement, let's go back to the original question. What exactly is the correct operation? It's a pity to miscalculate single digits.

Complement calculation is used inside the computer.

First, we need to convert what needs to be calculated into complement.

The original code of 2 is: 000000 10. Since it is a positive number, its complement is itself: 00000 10.

The original code of-1 is 100000 1, and its complement is111/65438.

Two. Complement addition is adopted in the computer to unify the operation rules. For example, consider 2- 1 as 2+(- 1). So 2- 1 is actually:

000000 10 + 1 1 1 1 1 1 1 1 = 0000000 1 = 1

Three. The final calculation result is normal, which is worthy of the primary school mathematics I have studied.

7.? The final reason: why do you have to fill the code?

When I read and think, many ideas express their essence through clock cycles.

I personally understand it as a transformation of the formula, which is relatively easy to understand. How can I put it? Let's deduce the following formula.

10-8= 10+(-8)= 10+(- 1-8)+ 1= 10+[(- 1-8)+ 1]

Come here, let's stop. This formula is equivalent. Let's see if the square brackets are similar to the internal calculation of+1

-1 is binary:111111,and the original code of -8 is:1000/. The inverse code is111kloc-0/1, (-1-8) The high-order division operation is:111111-00654438 The combined high-order is:/kloc.

The essence in square brackets is [(reciprocal)+1]=[ complement], which is why it can be calculated with complement.