Detailed explanation of original code, inverse code, complement code

Source: http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
Comment:
This article explains the original code, inverse code and complement code of the computer. It is necessary to use the complement and complement, and further demonstrate why the addition of the complement and complement can be used to calculate the subtraction of the original code. If there is something wrong with the argument, please correct me! I hope this article will help you learn computer basics Help!



1. Machine numbers and truth values
​​Before learning the original code, one's complement and complement, you need to understand the concepts of machine numbers and truth values.

1. Machine numbers The binary representation of

a number in a computer is called this number of machines. Machine numbers are signed, and the computer uses the highest bit of a number to store the sign, a positive number is 0, and a negative number is 1. For

example, the number in decimal +3, the computer word length is 8 bits, and the conversion into binary is 00000011. If it is -3, it is 10000011.

So, 00000011 and 10000011 here are the machine numbers.

2. True value

Because the first bit is the sign bit, the formal value of the machine number is not equal to the real value. For example, the above signed number 10000011, the highest bit 1 represents negative, and its real value is -3 instead of the formal value 131 (10000011 converted to decimal equals 131). Therefore, for the sake of distinction, the true value corresponding to the machine number with the sign bit is called the true value of the machine number.

For example: the true value of 0000 0001 = +000 0001 = +1, the true value of 1000 0001 = –000 0001 = –1



two. The basic concepts and calculation methods of the original code, the inverse code, and the complement code.
Before exploring why the machine uses the complement code, let us first understand the concepts of the original code, the complement code and the complement code. For a number, the computer needs to use a certain encoding method for storage. The original code, the complement code, and the complement code are machine storage The encoding method of a specific number.

1. Original code

The original code is the sign bit plus the absolute value of the true value, that is, the first bit represents the symbol, and the remaining bits represent the value. For example, if it is 8-bit binary:

[+1]Original = 0000 0001

[-1] Original = 1000 0001

The first bit is the sign bit. Because the first bit is the sign bit, the value range of the 8-bit binary number is:

[1111 1111 , 0111 1111]

that is

[-127 , 127]

The original code is the easiest way for the human brain to understand and calculate. 2.

One’s complement The one’s complement is expressed as: the complement of a positive number is itself, and the complement of a negative number is based on its original code, and the sign bit remains unchanged , the other bits are inverted. [+1] = [00000001] Original = [00000001] Inverse [-1] = [10000001] Original = [11111110] Inverse It can be seen that if an inverse code represents a negative number, the human brain cannot intuitively See its value. It is usually converted into the original code and then calculated. 3. Complement code The representation method of the complement code is: the complement of a positive number is itself, and the complement of a negative number is based on its original code. The bit remains unchanged, the rest of the bits are inverted, and the last +1. (that is, +1 on the basis of the complement)





















[+1] = [00000001] Original = [00000001] Inverse = [00000001] Complement

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

For negative numbers, the complement representation is also incomprehensible to the human brain You can see its value. Usually you need to convert it into the original code to calculate its value.



3. Why use the original code, one’s complement and one’s complement
Before starting in-depth study, my learning suggestion is to “memorize” the above first The representation and calculation method of the original code, one’s complement and one’s complement.

Now we know that the computer can represent a number in three encoding methods. For positive numbers, the results of the three encoding methods are the same:

[+1] = [00000001] Original = [00000001] Inverse = [00000001] Complement

So no need to explain too much. But for negative numbers:

[-1] = [10000001] Original = [11111110] Inverse = [11111111] Complement You

can see the original code, complement and complement is completely different. Since the original code is directly recognized by the human brain and used to calculate the representation, why is there still a complement code and complement code?

First, because the human brain can know that the first bit is the sign bit, when calculating We will choose the addition and subtraction of the truth value area according to the sign bit. (The concept of truth value is at the beginning of this article). But for computers, addition and subtraction multipliers are already the most basic operations, and they should be designed as simple as possible. Computer identification" The "sign bit" obviously makes the basic circuit design of the computer very complicated! So people came up with a way to include the sign bit in the operation. We know that according to the algorithm, subtracting a positive number is equal to adding a negative number, that is: 1 -1 = 1 + (-1) = 0 , so the machine can have only addition without subtraction, which makes the design of computer operations simpler.

So people began to explore the method of involving the sign bit in the operation, and only retaining the addition method. First look at the original code:

the expression to calculate the decimal: 1-1=0

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

If it is represented by the original code and the sign bit is also involved in the calculation, obviously for the subtraction, the result is incorrect. This is why the computer does not use the original code to represent a number. .In

order to solve the problem of subtracting the original code, there is an inverse code:

the expression to calculate the decimal: 1-1=0

1 - 1 = 1 + (-1) = [0000 0001] Original + [1000 0001] Original = [ 0000 0001] Inverse + [1111 1110] Inverse = [1111 1111] Inverse = [1000 0000] Original = -0

It is found that the subtraction is calculated using the inverse code, and the true part of the result is correct. The only problem actually occurs in " 0" is a special numerical value. Although people understand that +0 and -0 are the same, but the sign of 0 is meaningless. And there will be two codes [0000 0000] original and [1000 0000] original to represent 0 .So

the emergence of the complement code solves the problem of the symbol of 0 and the two encodings:

1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] complement + [1111 1111] Complement = [0000 0000] Complement = [0000 0000] Original In

this way, 0 is represented by [0000 0000], and the -0 that had problems before does not exist. And you can use [1000 0000] to represent -128:

(-1) + (-127) = [1000 0001] Original + [1111 1111] Original = [1111 1111] Complement + [1000 0001] Complement = [1000 0000] Complement

-1-127 The result should be -128, In the result of the complement operation, the complement of [1000 0000] is -128. But note that because the previous -0's complement is actually used to represent -128, -128 does not have the original code and the complement code. ( The complement of -128 represents [1000 0000] and the original code is [0000 0000], which is incorrect)

using complement not only fixes the sign of 0 and the problem of two encodings, but also It can also represent one more lowest number. This is why the range of 8-bit binary representation is [-127, +127] using the original code or one’s complement, and the range using the complement code is [-128, 127].

Because the machine Use complement, so for the 32-bit int type commonly used in programming, the range that can be represented is: [-231, 231-1] because the first bit represents the sign bit. When using complement representation, you can save one more The minimum value.



Four original code, one’s complement code, and one’s complement code goes deeper. The
computer cleverly involves the sign bit in the operation, and turns the subtraction into an addition. What is the mathematical principle behind it?

Think of a clock as a 1-bit 12 Base number. If the current time is 6 o'clock, I want to set the time to 4 o'clock, what should I do? We can:

1. Set back 2 hours: 6 - 2 = 4

2. Set the time forward 10 hours : (6 + 10) mod 12 = 4

3. Dial forward 10+12=22 hours: (6+22) mod 12 =4

2,3 The mod in the method refers to the modulo operation, 16 mod 12 =4 That is, the remainder after dividing 16 by 12 is 4.

So the result of dialing back (subtraction) of the clock can be replaced by dialing back (addition)!

Now the focus is on how to use a positive number to replace a negative number. In the above example, we can feel some clues and find some Law. But mathematics is rigorous. It cannot rely on feeling.

First, introduce a related concept in mathematics



: congruence The concept of congruence

Two integers a, b, if they are divided by an integer m, the remainders are equal, then they are called a, b For congruence modulo m

written as a ≡ b (mod m)

read as a and b congruence modulo m.

For example:

4 mod 12 = 4

16 mod 12 = 4

28 mod 12 = 4

so 4, 16, 28 are congruent modulo 12.



Taking modulo a negative number modulo

a positive number is easy. But what

about a negative number? Mathematical definition of mod operation:

clip_image001 The

above is a screenshot, the "remove the lower bound" symbol cannot be found and how to input it (garbled after pasting it in the word). The following is to use "L" and "J" to replace the "remove the lower bound" symbol in the picture above:

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

The above formula means:

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

Take -3 mod 2 as an example:

-3 mod 2

= -3 - 2xL -3/2 J

= -3 - 2xL - 1.5J

= -3 - 2x(-2)

= -3 + 4 = 1

so:

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

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

(-5) mod 12 = 12 - 5 = 7



to start the proof Let's

go back to the problem of the clock:

dial back 2 hours = dial back 10 hours

dial back 4 hours = dial back 8 hours

dial back 5 hours = dial back 7 hours

Note ,

the pattern found here! Learned the concept of congruence. In fact:

(-2) mod 12 = 10

10 mod 12 = 10

-2 and 10 are congruent.

(-4) mod 12 = 8

8 mod 12 = 8

-4 and 8 It is congruential.

It is getting closer and closer to success. To realize the replacement of negative numbers with positive numbers, it is only necessary to apply two theorems of congruence numbers:

Reflexivity:

a ≡ a (mod m)

This theorem is obvious.

Linear operations 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 the proof of this theorem, please see: http://baike.baidu.com/view/79282.htm

So:

7 ≡ 7 (mod 12)

(-2) ≡ 10 (mod 12)

7 -2 ≡ 7 + 10 (mod 12)

Now for a negative number, we have found its positive congruence. But not 7-2 = 7+10, but 7 -2 ≡ 7 + 10 (mod 12) , which is the result of the calculation The remainders are equal.

Next, back to the binary problem, look at: 2-1=1 problem.

2-1=2+(-1) = [0000 0010] original + [1000 0001] original = [0000 0010] Inverse + [1111 1110] Reverse

First , the inverse code of -1 is 1111 1110. If [1111 1110] is considered to be the original code, then [1111 1110] Original = -126, where the sign bit is removed, That is, it is considered to be 126. The

following rules are found:

(-1) mod 127 = 126

126 mod 127 = 126

That is:

(-1) ≡ 126 (mod 127)

2-1 ≡ 2+126 (mod 127)

2-1 and The result of the remainder of 2+126 is the same! And this remainder is our expected calculation result: 2-1=1

, so the one's complement of a number is actually the congruence of this number to a membrane. And this membrane It's not our binary, but the maximum value that can be represented! This is like a clock, after one turn, you can always find a correct value within the representable range!

And 2+126 is obviously equivalent to one turn of the clock, and because the sign bit is involved in the calculation, it just forms the correct operation result with the highest bit of the overflow.

Since the inverse code can turn the subtraction into an addition, then now the computer uses What about the complement of ? Why can we get the correct result by adding 1 to the complement?

2-1=2+(-1) = [0000 0010] original + [1000 0001] original = [0000 0010] complement + [1111 1111] Complement

If [1111 1111] is regarded as the original code and the sign bit is removed, then:

[0111 1111] Original = 127

In fact, +1 on the basis of the complement code is just equivalent to increasing the value of the membrane:

( -1) mod 128 = 127

127 mod 128 = 127

2-1 ≡ 2+127 (mod 128)

At this time, the dial is equivalent to one rotation every 128 scales. Therefore, the minimum and maximum values ​​of the operation results expressed in complement code It should be [-128, 128].

But due to the special case of 0, there is no way to represent 128, so the value range of the complement code is [-128, 127]

I have always been bad at mathematics, so if there is something wrong in the text, please everyone A lot of inclusions, a lot of pointers!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326329217&siteId=291194637