Do you understand the original code, inverse code, and complement code in C language programming?

1. Machine numbers and truth values

Before learning the original code, inverse code and complement code, you need to understand the concepts of machine numbers and truth values.

1. Number of machines

The binary representation of a number in a computer is called the machine number of the number. The machine number is signed, and the highest bit of a number is used to store the sign in the computer, the positive number is 0, and the negative number is 1.

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

Then, 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, in 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 is equal to 131). Therefore, for the sake of distinction, the real value corresponding to the machine number with the signed bit is called the real value of the machine number.

Example: True value of 0000 0001 = +000 0001 = +1, True value of 1000 0001 = –000 0001 = –1

2. Basic concepts and calculation methods of original code, inverse code and complement code.

Before exploring why the machine uses the complement code, let us first understand the concept of the original code, the complement code and the complement code. For a number, the computer must use a certain encoding method to store. The original code, complement code, and complement code are machine storage An encoding method for a specific number.

1. Original code

The original code is the absolute value of the sign bit plus the true value, that is, the first bit is used to represent the sign, 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]

Right now

[-127 , 127]

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

2. Inverse code

The inverse representation method is:

The one's complement of a positive number is itself

The inverse code of a negative number is based on the original code, the sign bit remains unchanged, and the rest of the bits are reversed.

[+1] = [00000001] Original = [00000001] Anti

[-1] = [10000001] original = [11111110] anti

It can be seen that if a negative code represents a negative number, the human brain cannot intuitively see its value. Usually it needs to be converted into the original code before calculation.

3. Complement code

The two's complement notation is:

The complement of a positive number is itself

The complement code of a negative number is based on the original code, the sign bit remains unchanged, and the rest of the bits are reversed, and finally +1. (That is, +1 on the basis of the complement code)

[+1] = [00000001] original = [00000001] inverse = [00000001] complement

[-1] = [10000001] original = [11111110] inverse = [11111111] complement

For negative numbers, the complement code representation is also impossible for the human brain to intuitively see the value. Usually it needs to be converted into the original code to calculate the value.

3. Why use original code, inverse code and complement code

Before starting to study in depth, my learning suggestion is to "memorize" the original code above, the expression and calculation methods of the inverse code and complement code.

Now we know that the computer can express a number in three encoding ways. 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

It can be seen that the original code, inverse code and complement code are completely different. Since the original code is directly recognized by the human brain and used to calculate the representation, why are there still inverse code and complement code?

First of all, because the human brain can know that the first bit is the sign bit, we will choose the addition and subtraction of the true value area according to the sign bit when calculating. (The concept of true value is at the beginning of this article). But for computers, addition and subtraction Multiplication is already the most basic operation, and it should be designed as simple as possible. The computer's identification of the "sign bit" will obviously make the basic circuit design of the computer very complicated! So people came up with a method to use the sign bit to participate in the operation. We know , 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 only add but not subtract, so that the design of computer operations is simpler.

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

Calculate the decimal expression: 1-1=0

1 - 1 = 1 + (-1) = [00000001] raw + [10000001] raw = [10000010] raw = -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:

Calculate the decimal expression: 1-1=0

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

It is found that the truth value of the result is correct when using one’s complement to calculate the subtraction. The only problem is actually the special value "0". Although people understand that +0 and -0 are the same, but 0 has a sign It has no meaning. And there will be [0000 0000] original and [1000 0000] original two codes to represent 0.

So the emergence of the complement code solves the problem of the sign of 0 and 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 it can be represented by [1000 0000] -128:

(-1) + (-127) = [1000 0001] original + [1111 1111] original = [1111 1111] complement + [1000 0001] complement = [1000 0000] complement

The result of -1-127 should be -128. In the result of the complement code operation, [1000 0000] complement is -128. But note that because the previous -0 complement code is actually used to represent -128, so- 128 has no original code and inverse code representation. (The complement code representation of -128 [1000 0000] complements the original code is [0000 0000] original code, which is incorrect)

Using complement code, not only fixes the sign of 0 and the problem of two encodings, but also can express a minimum number. This is why the range of 8-bit binary, using the original code or inverse code, is [-127, + 127], while the range represented by two’s complement is [-128, 127].

Because the machine uses complement code, 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. And when using complement code, it can be expressed more Save a minimum value.

4. Original code, inverse code, complement code and then go deeper

The computer cleverly uses the sign bit to participate in the operation, and turns subtraction into addition. What kind of mathematical principle is behind it?

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

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

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

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

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

So the result of the clock turning back (subtraction) can be replaced by turning it forward (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 discover some laws. But mathematics is rigorous, and we cannot rely on feelings.

First introduce a related concept in mathematics: congruence

concept of congruence

Two integers a, b, if they divide the remainder by the integer m are equal, then a, b are said to be congruent modulo m

Write a ≡ b (mod m)

Read as a and b congruent modulo m.

for example:

4 mod 12 = 4

16 mod 12 = 4

28 mod 12 = 4

so:

7 ≡ 7 (mod 12)

(-2) ≡ 10 (mod 12)

7 -2 ≡ 7 + 10 (mod 12)

Now we find its positive congruent number for a negative number. But it is not 7-2 = 7+10, but 7 -2 ≡ 7 + 10 (mod 12), that is, the remainder of the calculation result is equal.

Next, let’s go back to the problem of binary, let’s take a look: the problem of 2-1=1.

2-1=2+(-1) = [0000 0010] raw + [1000 0001] raw = [0000 0010] reverse + [1111 1110] reverse

Come to this step first, the inverse code of -1 means 1111 1110. If [1111 1110] is considered as the original code here, then the original code of [1111 1110] = -126, and the sign bit is removed here, that is, it is considered to be 126.

The following rules were found:

(-1) mod 127 = 126

126 mod 127 = 126

Right now:

(-1) ≡ 126 (mod 127)

2-1 ≡ 2+126 (mod 127)

2-1 is the same as the remainder of 2+126! And this remainder is the calculation result of our expectation: 2-1=1

Therefore, the inverse code of a number is actually the congruent number of this number with respect to a membrane. And this membrane is not our binary system, but the maximum value that can be represented! A correct value within the representable range can be found!

And 2+126 is obviously equivalent to a round of the clock, and because the sign bit is involved in the calculation, it happens to form the correct operation result with the highest overflow bit.

Since the complement code can turn subtraction into addition, what about the complement code used by computers now? Why can adding 1 to the complement code still get the correct result?

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 inverse 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 turning once every 128 scales. Therefore, the minimum and maximum values ​​of the operation results expressed in complement code should be [-128, 128].

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

Look at me for a full set of source code material: 725022484 Come on Xiaoyu's personal space-Come on come on Xiaoyu Personal Homepage-哔哩哔哩Video哔哩哔里 Come on Xiaoyu's personal space, provide Xiaoyu Come on and share For videos, audios, articles, dynamics, favorites and other content, follow the Xiaoyu Come on account, and be the first to know about UP’s dynamics. Programming learning group: 725022484 Share a small programming game every day~C/C++ game source code materials and various installation packages, private messages are not often seen! https://space.bilibili.com/1827181878?spm_id_from=333.788.0.0

Guess you like

Origin blog.csdn.net/yx5666/article/details/129086086