Talking about the design and some understanding of the "code" in the computer from the source of calculation

A superficial understanding of the design roots of "code" in computers

Whether in the course of studying university computers or in the early days of studying computer systems, we will repeatedly mention the definition of several codes. Here I only refer to the original code, the inverse code and the complement code, but does not include the national standard code and the internal code. Trying to understand why designers designed the original code, the inverse code and the complement code in the early stage, it will definitely give us a better understanding of these three good brothers (gui).
After reading this article completely, your understanding of the code will definitely improve to a very high level.
Expressing opinions only represents personal understanding. If there are some errors and understanding limitations, please feel free to enlighten me.

Design from the perspective of calculation

Regarding why computers use binary, a highly-read article on this site is quoted, and I won’t repeat it here. [Why the computer uses binary]
Here we start from how to calculate, trying to teach a computer to do math problems from scratch.
As we all know: the computer is a very "stupid" guy, but he is the most industrious. We always give him the simplest possible methods and actions to drive him to work with maximum efficiency to meet our own needs. Therefore, designing a universal and simple logic is always our core purpose.

addition

Let's first create two non-negative integers with only 16-bit binary (the word length of ancient computers), and do an addition.

num1 num2 sum
0000000000000001 0000000000000011 0000000000000100

Apart from the difference between the decimal system and our decimal addition, the calculation method and rules are actually the same. It is also very "natural" for the "carbon-based organisms" who are accustomed to the decimal system (referring to the natural language of nature).
And because of the binary nature, the Turing machine logic of addition has become very simple. Therefore, the addition calculation has been retained to this day, and countless cycles are carried out in the computer every day.
Therefore, we are willing and can more should the positive number means in the computer ordinary binary.

Subtraction

What is subtraction? Is it ab?
What is ab? So what exactly is -b?
You may be wondering why I asked this question. You can take a minute to think carefully about what -b is, or what -1 is.
Regarding negative numbers, here is a quote from Baidu Baike's explanation:

Negative numbers are mathematical terms. Numbers smaller than 0 are called negative numbers. Negative numbers and positive numbers indicate quantities with opposite meanings. Negative numbers are marked with a minus sign (Minus Sign, which is equivalent to minus sign) "-" and a positive number, such as −2, which represents the opposite of 2. Therefore, any positive number with a negative sign becomes a negative number. A negative number is the opposite of its absolute value. On the number axis, negative numbers are all on the left side of 0. The earliest record of negative numbers is the ancient mathematics book "Nine Chapters of Arithmetic". In the counters, it is stipulated that "positive counts as deficits, negative counts as blacks", that is, red counters are used to represent positive numbers and black ones to represent negative numbers. Compare the magnitude of two negative numbers, and the larger the absolute value is, the smaller.

We pay attention to a few key sentences in this paragraph:

Negative and positive numbers indicate quantities with opposite meanings. Negative with minus sign (Minus Sign, which is equivalent to minus) "-" and a positive mark , such as -2, is the opposite of the representative 2. Therefore, any positive number with a negative sign becomes a negative number.

Negative numbers are marked with a negative sign "-" and a positive number, which represents the opposite of 2. That is to say, when we expand the negative number, it is just to introduce a number or a "thing" that can be added to its corresponding positive number, thereby offsetting the result of zero.
So, if we forget from now on the expression of negative numbers in normal days (it is just the "name" of an abstract unit)—adding a negative sign, how do we design a so-called negative number for the computer to replace the natural language -a?
We still proceed from our original intention of designing the representation in the negative number machine: a number or a thing can add to its corresponding positive number to equal zero.
Let's take a look at the table below.

num1 num2 sum
0000000000010101 0000000000000000

Use the binary calculation method we have learned to tell yourself how much num2 should be:
1111111111101011
If you are looking for an answer with a question, you should have already thought of it here: So why do we need to invert and add one to the complement?
First of all, binary numbers are "black or white", which means that a digit is either 1 or 0.
For a computer, the task of negation is very simple. We only need to use each bit of negation operation to create a new number, add it to the original number, and the result is equal to a string of maximum values ​​of all 1, which is Our inverse code.
Then we add one to the inverse code, that is, the complement: let our complement exactly "complement" the corresponding positive number and get the sum to 0, that is, add 1 to this "1111111111111111", and you can combine everything like a domino The 1 becomes 0 (the most significant 1 overflows the range of byte storage), thereby turning the entire number string into "0000000000000000".
So far, our subtraction needs have been met, right?
Obviously not, we will realize a problem: According to our logic above, is 1100100000011001 a positive number or a "negative number"?

Bijection of calculation code and natural number

As long as we use one's complement calculation, we must not escape this problem: the end of our calculation is to use the calculation result: we almost certainly need to convert the number into our natural decimal system for use, if we can't distinguish a computer Whether the numbers in are used to represent positive or negative numbers is naturally not feasible.
So how to use the simplest logic to distinguish between the positive and negative numbers represented in the computer, so that they can be converted into natural numbers one-to-one without confusion.
It's very simple, everyone can easily think of it, and we also remember that it is to use the front sign bit as a "mark", that is, to divide the territory of positive and negative.
Binary is either black or white. Should we use 0 for positive numbers or 1 for negative numbers?
I have made a picture in my related studies in the past few days, which can make everyone more clearly understand the representation of the whole integer.
Insert picture description here

Why choose the first digit to be 0 for "positive" and 1 for "negative"? This starts with the need for us to create this rule: can we use such a corresponding rule to make it satisfy all integer addition (subtraction has been transformed into addition) calculations?
When we use any number to add another number: we find the position of the first number on the graph, and then we express the addition of the second number by transforming the position of the first number: that is, positive The number is the angle corresponding to the clockwise movement, and the negative number is also moved clockwise by a larger angle, and this angle in the circle can be regarded as the positive angle corresponding to the negative number moving counterclockwise (we can think of this addition as A vector addition).
Therefore, whether we add a positive number or a negative number, the problem can be solved easily.
And the value corresponding to 00000000 in our picture is 0, which is divided into positive and negative on the left and right.
When we add any positive or negative numbers, crossing the limit of 00000000 within the range is just a normal change of the sign of the sum.
We may also think about a question: You said it is on the right and negative on the left. What will happen if positive plus plus or minus plus minus crosses the boundary between plus and minus, that is, 10000000?
Answer: It will go wrong. . .
So our so-called numerical overflow (here refers to the value that is too large to exceed the range of our int or float) is also generated in this way, and the values ​​of two positive numbers are added to the first place ------ Affects the digits that we use to distinguish between positive and negative ranges, which will naturally cause wrong reactions.

Overflow (more than the range that can be represented by one byte)
is an art of computer modulo operation. We use the limitation of byte storage to successfully turn our one-dimensional number axis that originally represented positive and negative numbers into a circle as shown in the figure above.
We will see in many blogs and articles the analogy of the "dial" of model taking, I think it is very subtle (and also inspired me to do the source of the circle above). Overflow is a physical necessity, to a certain extent, it is actually the shackles of computer computing.
But we can use this shackle to create a certain kind of calculation we want, and there is no need to create new rules.
So I think it is an art. According to Wang Jinxi's words, it is a kind of spirit that has the conditions, but also creates the conditions without the conditions.
I won't talk more about overflow here, because I don't know enough about it, and interested students can check related articles by themselves.

Something we should be able to think of

Essentially, the so-called original code, complement, and one's complement are all so-called name problems: our only and ultimate goal is to create a universal binary representation that can be used for "addition".
The original code is the true value of the number, the inverse code is a tool code (complete intermediate product) for the complement code, and the complement code is our final product.
So far, we have figured out the most basic codes of our computer. Integer multiplication and division on this basis are inseparable from addition.
It's over!

Some things to say at the end of the article

This article will not talk about every kind of code, one is that I have not done too much understanding for the time being, and the other is space and energy limitations (this article has been delayed for a week).
I just want to talk about some small issues.

What we should think about if you have read such a long content above

Why do we have to act according to some IEEE rules that seem to be inconsistent with our logic?
In IEEE754, why does the end code of a floating-point number shift code need not be expressed in complement? Why should the order of shift code use the true value plus 127 Isn't it more intuitive to use the sign's complement instead of the conversion?
What is the difference between the calculation of floating-point numbers and the calculation of integers, so what changes have been made in the representation of floating-point numbers, and what are the purposes of these changes? What are some of the subtle designs that require some time for us to understand?
And what are the differences between more and more numerical calculations and logical calculations? What are the differences between them?
I will not give my own understanding of these questions. If there are judges who want to confirm their own thoughts, they can also give their own understanding in the comment area, so that more people of insight can appreciate and comment together, which will definitely help fill the gaps and be of great benefit.

"It is often more important to ask a problem than to solve a problem, because solving a problem may only be a mathematical or laboratory skill. To raise new questions, new possibilities, and look at old problems from a new perspective, Both require creative imagination, and sign the true progress of science." ------Einstein

I always believe that the problem or demand-oriented solution is the most correct and not redundant. Before we have the ability to ask unanswered questions, we can try to recover the original demands from the existing answers. We will certainly be able to improve our ability to create and solve problems ourselves.
Thank you everyone for reading the best, and I wish you all the best!

Guess you like

Origin blog.csdn.net/weixin_50282315/article/details/114866807