The original code, anti-code and complement the knowledge to explain in detail

A. Machine number and the true value

Before learning the original code, anti-code and complement, the number of machines needed to understand the concept and the true value.

1, machine number

A binary number in the computer representation, the number of machines called the number. Machine number is signed, in a computer with a number of sign bits to store the most positive number of 0, a negative number.

For example, the decimal number of +3, the computer word length is 8 bits, is converted into binary 00000011. If -3 is 10000011.

Well, here's 00000011 and 10000011 is the number of machines.

2, the true value

Because the first one is the sign bit, so the number of machines in the form of value is not equal to the true value. The above example has signed number 10000011, which represents the most significant bit negative, its true value is not in the form -3 value 131 (decimal conversion equal to 131 10000011). The real value of the number corresponding to the machine so that, for the sake of distinction, with the sign bit is referred to as the true value of the machine.

Example: true value = 0000 0001 +000 0001 0001 +1,1000 = true value = -1 = -0000001

 

II. Basic concepts and calculation methods original code, the inverted, complementary codes.

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:

[L] Original  = 0000 0001

[-1] Original  = 10000001

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.

[L] = [00000001] Original  = [00000001] trans

[-1] = [10000001] Original  = [11111110] trans

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)

[L] = [00000001] Original  = [00000001] trans  = [00000001] Complement

[-1] = [10000001] Original  = [11111110] trans  = [11111111] Complement

For negative two's complement representation of the human brain it is 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:

[L] = [00000001] Original  = [00000001] trans  = [00000001] Complement

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

[-1] = [10000001] Original  = [11111110] trans  = [11111111] Complement

It 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] Hara  + [10000001] Hara  = [10000010] Hara  = -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] Original  + [1000 0001] Original = [0000 0001] trans  + [1111 1110] trans  = [1111 1111] trans  = [1000 0000] Original  = -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] Original  + [1000 0001] Original  = [0000 0001] Complement  + [1111 1111] Complement  = [0000 0000] Complement = [0000 0000] Original

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] Original  + [1111 1111] Original  = [1111 1111] Complement  + [1000 0001] Complement  = [1000 0000] Complement

-1-127 results should be -128, in complement with the result of the operation, the [1000 0000] Complement  is -128 but be careful because 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] the original , which is not correct)

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 is 2 31 is -1] Because the sign bit is represented by a two's complement representation is used Shiyou you can save more than a minimum.

Guess you like

Origin www.cnblogs.com/helloworld2048/p/10978900.html