The original code, anti-code complement Detailed (fully do understand)

A. Machine number and the true value

在学习原码,反码和补码之前,需要先了解机器数和真值的概念.

1. The number of machines

一个数在计算机中的二进制表示形式, 叫做这个数的机器数。机器数是带符号的,在计算机用一个数的最高位存放符号, 正数为0, 负数为1.

比如,十进制中的数 +3 ,计算机字长为8位,转换成二进制就是00000011。如果是 -3 ,就是10000011 。

那么,这里的00000011和10000011就是机器数。

2. The true value

因为第一位是符号位,所以机器数的形式值就不等于真正的数值。例如上面的有符号数 10000011,其最高位1代表负,其真正数值是 -3 而不是形式值131(10000011转换成十进制等于131)。所以,为区别起见,将带符号位的机器数对应的真正数值称为机器数的真值。

例:0000 0001的真值 = +000 0001 = +1,1000 0001的真值 = –000 0001 = –1

II. The original code based on the concept, the inverted, and the calculation method of complement

Before exploring why the machine you want to use complement, let us first understand the concept of the original code, anti-code and complement for a number, the computer you want to use a certain encoding for storage. Original code, anti-code complement machine storage encoding a specific number.

1. The original code

Original code is the sign bit plus the absolute value of the true value, i.e. with a first symbol representing the remaining bits represent a value, such as 8-bit binary if:

[+1]原 = 0000 0001
[-1]原 = 1000 0001

The first is the sign bit as the first bit is the sign bit, so the range of 8-bit binary number is:

[1111 1111,0111 1111]

which is

[-127 , 127]

Original code of the human brain is the most easily understood and calculated representation.

2. anti-code

Anti-code representation are:

  • Positive anti-code is itself
  • Inverted negative which is based on the original code, change the sign bit, each bit is inverted to rest.
[+1] = [00000001]原 = [00000001]反
[-1] = [10000001]原 = [11111110]反

If a visible anti-code representation is negative, the human brain can not intuitively see its value. To usually converted into the original code recalculation.

3. Complement

Complement representation is:

  • Complement is a positive number of its own
  • Negative complement thereof is based on the original code, change the sign bit, the remaining bits inverted, and finally +1. (I.e., on the basis of the inverted +1)
[+1] = [00000001]原 = [00000001]反 = [00000001]补
[-1] = [10000001]原 = [11111110]反 = [11111111]补

For negative two's complement representation of the human brain are not visually see its value is usually also need to convert the original code to calculate its value.

III. Why use the original code, anti-code and complement

Before starting depth study, my suggestion is to learn "rote" above the original code, anti-code representation and complement and calculation methods.

Now we know that the computer can have three encoding represents a positive number for the number three encoding methods because the results are the same:

[+1] = [00000001]原 = [00000001]反 = [00000001]补	

There is no need to explain too much but for negative numbers:

[-1] = [10000001]原 = [11111110]反 = [11111111]补

Shows that the original code, anti-code and complement are completely different. Since the original code is directly identify the human brain and used to calculate representation, why does it have anti-code and complement it?

First, because the human brain can know the first one is the sign bit, when we calculated based on the sign bit, select the subtraction of the true values ​​of the region. (The concept of true value most in the beginning of this article). But for the computer, add and subtract multiplier already is the most basic operation as simple as possible to design a computer to identify "sign bit" to make clear that the basis of computer circuit design becomes very complicated! so people came up with the sign bit is also involved in computing. we know the algorithm subtracts a positive number equal to add a negative, namely: 1-1 = 1 + (-1) = 0, so that the machine can not be in the subtraction-addition, the design of such computer operation is even simpler.

Then people began to explore involved in computing the sign bit, and to retain only the first method of addition of the original code view:

Calculates a decimal-expression: 0 = 1-1

1 - 1 = 1 + (-1) = [00000001]原 + [10000001]原 = [10000010]原 = -2

If you said, so the sign bit is also involved in the calculation, apparently for subtraction, the result is not correct. This is why the internal computer does not use the original code is expressed by a number of the original code.

In order to solve the original code to do subtraction problems, there has been inverted:

Calculates a decimal-expression: 0 = 1-1

1 - 1 = 1 + (-1) = [0000 0001]原 + [1000 0001]原= [0000 0001]反 + [1111 1110]反 = [1111 1111]反 = [1000 0000]原 = -0

Find calculated using the inverted subtraction, the true value of the partial results are correct. The only problem is actually appeared on the "0" this special value. Although people understand the +0 and -0 is the same, but signed 0 there is no sense. and there will be [0000 0000] original and [1000 0000] original two coded representation 0.

So there complement solve the problem of symbol 0 and two encoded:

1-1 = 1 + (-1) = [0000 0001]原 + [1000 0001]原 = [0000 0001]补 + [1111 1111]补 = [0000 0000]补=[0000 0000]原

Such is represented by 0 [0000 0000], and no problem existed before -0 and can be used [1000 0000] -128 represents:

(-1) + (-127) = [1000 0001]原 + [1111 1111]原 = [1111 1111]补 + [1000 0001]补 = [1000 0000]补

-1-127 results should be -128, in complement with the result of the operation, the [1000 0000] Note that because the supplement is -128 but actually using the previous complement -0 to represent -128, so - 128 and does not represent the original code and inverted. (-128 complement representation of [1000 0000], which make up the original code is out of [0000 0000] Originally, this is not true)

Use complement, not just to repair the 0 symbol and problems of two coding, but also represent more than a minimum number. This is why the 8-bit binary, use the original code or anti-code representation of the range of [-127, + 127], and the scope of use complement representation is [-128, 127].

Because the machine's complement, so the 32-bit programming used to type int, may indicate the range: [-2 ^ 31, 2 ^ 31-1] as is the sign bit represented by a two's complement representation is used time and can save more than a minimum.

Four original code, anti-code and complement further in-depth

Computer skillfully involved in computing the sign bit, and the subtraction becomes addition, the implication behind what mathematical principles it?

The watch is thought of as a one of 12 hexadecimal numbers. If the current time is 6:00, I want to set the time to 4:00, you need to how to do it?

We can:

1. 往回拨2个小时: 6 - 2 = 4
2. 往前拨10个小时: (6 + 10) mod 12 = 4
3. 往前拨10+12=22个小时: (6+22) mod 12 =4

2,3 method mod means a modulo operation, 16 mod 12 = 4 divided by 16 i.e. with the remainder 12 is 4.

Therefore, the timepiece dial back (subtraction) can result clocks forward (addition) instead!

The focus now falls on how to use a positive number, instead of a negative. The above example we can feel it some clues, find some rules, but mathematically rigorous. Can not rely on feel.

Introduces a mathematics related concepts: congruence

The concept of congruence

Two integers a, b, when they are obtained by dividing the integer m is equal to I, called a, b for congruent modulo m

Referred to as a ≡ b (mod m)

A and b read as congruent modulo m.

for example:

4 mod 12 = 4
16 mod 12 = 4
28 mod 12 = 4

So 4, 16, 2812 congruence on mold.

Take a negative touch

Positive conduct mod operation is very simple, but it negative?

Here is the definition of mod mathematical operations:

Here Insert Picture Description
The above is a screenshot "Remove sector" symbol can not find how to enter (the word pasted after distortion) The following is an "L" and "J" replace "remove bound" symbol on the map:

x mod y = x - y L x / y J

The above equation means:

x mod y equals x minus y multiplied by the quotient of the lower bound of x and y.

In -3 mod 2 Example:

-3 mod 2
= -3 - 2xL -3/2 J
= -3 - 2xL-1.5J
= -3 - 2x(-2)
= -3 + 4 = 1

and so:

(-2) mod 12 = 12-2=10
(-4) mod 12 = 12-4 = 8
(-5) mod 12 = 12 - 5 = 7

He began to prove

Go back to the issue of the clock:

回拨2小时 = 前拨10小时
回拨4小时 = 前拨8小时
回拨5小时= 前拨7小时

Note that the law found here!

Combined with the concept of congruence of the above actually learned:

(-2) mod 12 = 10
10 mod 12 = 10

2 and 10 is a congruence.

(-4) mod 12 = 8
8 mod 12 = 8

4 with 8 congruence.

The distance is getting closer to successfully achieve positive alternative to negative numbers, we only need to use two theorems with a remainder:

Reflexivity:

a ≡ a (mod m)

This theorem is very obvious.

A linear operation Theorem:

If a ≡ b (mod m), c ≡ d (mod m) then:

(1)a ± c ≡ b ± d (mod m)

(2)a * c ≡ b * d (mod m)

If you want to see proof of this theorem, see: http: //baike.baidu.com/view/79282.htm

and so:

7 ≡ 7 (mod 12)
(-2) ≡ 10 (mod 12)	
7 -2 ≡ 7 + 10 (mod 12)

Now we as a negative number, with the remainder found it positive, but not 7-2 = 7 + 10, but 7 -2 ≡ 7 + 10 (mod 12), that is equal to the remainder of the results.

Then back to the binary problem, look: Question 2-1 = 1.

2-1=2+(-1) = [0000 0010]原 + [1000 0001]原= [0000 0010]反 + [1111 1110]反

First this step, the inverted-1 is represented by 1111 to 1110. If there is [11111110] that is the original code, then the [1111 1110] = -126 original, where the sign bit is removed, i.e., considered to be 126.

Found to have the following rules:

(-1) mod 127 = 126
126 mod 127 = 126

which is:

(-1) ≡ 126 (mod 127)
2-1 ≡ 2+126 (mod 127)

2-1 with the remainder the result of 2 + 126 is the same and the remainder, our expectations of the official results:! 2-1 = 1

So a number of anti-code, this is actually the number for the same number more than a film. And this film is not our binary, but the maximum that can be represented! This and watches as a circle after total to find a correct value can be expressed within the range!

And it is clear that the equivalent of 126 2 + watch turned round, and because the sign bit is involved in the calculation, the highest bit overflow and just form a correct calculation results.

Since the anti-code can become an addition subtraction, then use the computer now complement it? Why plus 1 on the basis of the anti-code, but also get the right results?

2-1=2+(-1) = [0000 0010]原 + [1000 0001]原 = [0000 0010]补 + [1111 1111]补

If [11111111] as the original code, removing the sign bit, then:

[0111 1111]原 = 127

In fact, on the basis of the inverted + 1, only the equivalent of increasing the value of the film:

(-1) mod 128 = 127
127 mod 128 = 127
2-1 ≡ 2+127 (mod 128)

At this time, the dial scale corresponding to 128 revolutions per round, so the calculation result indicated by the minimum and maximum complement should be [-128, 128].

However, due to the special circumstances of 0, there is no way represents 128, the complement is in the range [-128, 127]

Published 60 original articles · won praise 1 · views 3333

Guess you like

Origin blog.csdn.net/qq_16438883/article/details/103588511