In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >
Share
Shulou(Shulou.com)11/24 Report--
Today's article starts with the plane used in the sneak attack 80 years ago.
The sneak attack was made into a film "Pearl Harbor" as early as 2001, which is definitely a wonderful air combat movie. But in the process of watching the movie, have you ever thought about this question: in World War II aircraft, will the bullets fired by their own aircraft not hit the blades of their own aircraft during air combat? In fact, when I was a child, I had this question when I watched this movie, until later, after I learned "Mechanical principles" in college, I understood the principle behind it:
Machine gun shooting coordinator: it was invented by Anthony Fogg, a Dutchman who served in the allies in World War I. he installed the cam on the propeller shaft, and the three protrusions of the cam were exactly at an angle from the blade. When the protruding part touches the metal rod, the machine gun launcher connected to the back end of the metal rod will be activated, and the bullet will be fired, otherwise, when the blade and the barrel form a straight line. The machine gun stops firing automatically. This ingenious design perfectly avoids the self-harm accident of the fighter plane.
When I knew how it worked, the reaction went like this:
Shit, there is still this kind of operation!
Shit, why didn't I think of it so simple!
Indeed, inventions that shock the body of a tiger often look very simple, and you often think, it's so simple, well, why didn't I think of it?
I still say that simplicity is beauty, but simplicity is often much more difficult than complexity.
In fact, computer design is also an artistic game, the topic we are talking about today is the operation of computer complement, it seems simple, but this design is also exquisite.
First, how logic circuits calculate addition in 1938, Shannon (needless to say, no one in the computer industry has never heard of him) published a famous master's thesis entitled "symbolic Analysis of Relays and switching Circuits" (A Symbolic Analysis of Relay and Switching Circuits) at the Massachusetts Institute of Technology. He made it clear in the article that electronic engineers can use all the tools of Boolean algebra to design switch circuits. That is to say, logic operation can be realized by circuit. Then according to this theory, people design various logic gates (Logic Gate) to carry out data operation. The operation principles of later electronic computers are all based on this theory. For example, people design XOR gates according to the characteristics of relays or transistors. (for the nature of XOR operations, please refer to how to understand XOR operations.):
When switch An is closed, the coil produces magnetism to close the switch M and turn on the circuit of the light bulb, which is the simplest logic circuit. Can you imagine that the CPU or even all the storage devices invented by humans are actually made up of these piles of switches? although modern CPU uses transistors (faster and smaller), the principle is the same. For example, Apple's newly released M2 chip integrates 20 billion transistors, which translates to 20 billion switches.
This is just like what our ancestors said: one gives birth to two, two gives birth to three, and three gives birth to all things.
Later, people made a simple transformation according to the above circuit, which is nothing more than the switch's normally open or normally closed, and so on, and invented a variety of different logic gates, which can achieve more logic circuits, such as AND, NAND, OR, NOR, XOR and so on.
For example, the following is connected to the door switch An and B must be closed at the same time to light up:
People did not expect that such a circuit would have some connection with binary addition, for example, the carry of binary 1x1x10 is 1, and the double switch of this and gate circuit must be closed at the same time to be bright. If closing represents 1 and disconnection represents 2, then the logical relationship is 1 AND 1.
One day, people were surprised to find that an XOR door in parallel with the door could actually do a simple binary addition operation, which was named a semi-adder. It is called a semi-adder because it has no way to include the carry output into the next operation, such as 1-1-10, and the carry to the right of the equal sign cannot be included in the next operation for the time being.
Let's put this pile of symbols into a whole:
Later, people improved this circuit and used two half adders and one OR gate to form a full adder. This time, the full adder made up for the disadvantage that the semi adder could not calculate and allow carry to participate in the operation. The carry of the first bit can be included in a calculation, so the input of the full adder has three inputs:
Let's put the above pile of symbols into a whole:
Full adders multiple full adders can be combined to calculate multi-bit binary additions, and the following set of adders can calculate four-bit binary additions:
Through the combination of this set of adders, we can calculate the decimal 5'3'8 operation. It is hard to imagine that this kind of operation is realized by us through several switches! In fact, this is the principle of addition calculation by modern computers.
Here, did you exclaim:
Shit, there is still this kind of operation!
Shit, why didn't I think of it so simple!
But don't be surprised too early, there will be more surprises to you later.
At this point, we have been able to calculate addition through the logic circuit we designed, but there is still an important question: how to calculate subtraction? Because computational subtraction involves the tedious operation of borrowing, and the circuit we designed above can only carry, do we have to design a specific logic circuit for subtraction? the answer must be no, so our circuit will be very complicated. what we are thinking about is how to calculate subtraction through the existing logic circuit, that is, how to calculate subtraction through addition.
This question is particularly interesting, some people will say, minus a number equals to add the negative number of this number, such as 5-3 times 2, but the question is that this statement is actually calculating subtraction, according to our current design of the switch circuit can not be achieved, so what are we going to do?
Imagine that when we were in primary school, when we first began to learn three-digit subtraction, we didn't like some subtraction with borrowings. For example, this formula makes us very uncomfortable to calculate. First of all, from the individual position, 3 is less than 7, so we have to carry it from ten digits, while the ten digits are less than 4 after borrowing, and we have to borrow from 100 digits.
Let's use a technique here, subtract 147 from 999, and obviously this formula will not generate borrowings: this complement, which we call 9, adds this result to the subtraction 213 and then subtracts 1000: we get the answer we want, and we don't borrow.
Why is this indirect operation correct? This is because the original problem can be transformed into the following operation: you see, it is actually added by 1000 and finally subtracted, and let's combine the above formula again: in fact, the calculation result is the same, and the borrowing operation is avoided.
At this point, you may wonder: but this formula also uses subtraction, and it is twice. Will the computer still have the skill to skip this subtraction when calculating?
Here, the amazing thing happened, because the computer uses binary, the first subtraction, that is, the complement is subtracted from a string of 1 numbers, and the binary complement operation is not like the decimal system, the former does not need to do subtraction at all, but changes the number 1 in the original binary system into 0, 0 into 1 (this is the same as directly calculating the result of subtraction. But this technique saves the computer the operation of subtraction), which can be called inverse code, which can be realized by the inverter in the logic circuit, and the second subtraction is the highest bit in the binary system. For computers, we only need to use a logic gate circuit to limit the highest output.
Let's take a look at how wonderful it is to use binary computing.
The first step is to find the complement operation:
The second step is to add the result and subtract 213:
Step 3, add the result of step 2 by 1:
The fourth step is to reverse the highest level of the result of the third step, which is equivalent to subtracting 256:
In this way, we finally get the desired result: 66, although the whole process uses subtraction twice, but from the binary point of view, there is no subtraction at all.
Second, why use complement to store integers? however, the above circuit has limitations. It can only calculate operations where the subtraction is greater than the subtraction, and it cannot represent negative numbers. What we want is to use the existing circuit to enable it to calculate addition, subtraction, and negative numbers. In other words, how can all operations be implemented according to addition?
At this time, the complement operation is on the stage.
First of all, in order to distinguish between integers and negative numbers, the computer defines the symbol bits, stipulating that the highest bit is the "symbol bit", 0 represents the positive number, 1 represents the negative number, and the rest is the "digital bit". For example, for two bytes, the short type number 1 is represented inside the computer as follows:
The integer-1 is represented like this, except that the symbol bit is changed to 1:
But this comes at a cost, which means that the representation of our data bits is actually one bit less, so that the number we could have expressed is not so large. For example, a single byte could originally represent a number between 0 and 255, but because the symbol bit occupies 1 bit, we actually say that the number of bits of data has changed to 7 bits, and the maximum can only represent 127.
At this time, we introduce the concept of inverse code and complement code: the inverse code complement of a positive number is its original code, while the inverse code of a complex number is special, the symbol bit remains the same, the data bit inversion is the inverse code, and the inverse code plus 1 is the complement code:
All the operations in the computer are in the form of complement, so why is this so?
Let's first look at if we calculate in the form of the original code, suppose we want to calculate 1-3, which is actually 1 + (- 3):
The result is 1 + (- 3) =-4, which is obviously incorrect.
So what happens if we use inverse codes for calculation?
The result is correct, as we expected, but what if we calculate 3-1? try again:
Finally, the result of 3 + (- 1) = 1 is obtained, which shows that using inverse code operation, it is no problem for decimals to decrease large numbers, but there is a problem for large numbers to decrease. Intuition tells us that the result is 1. 5% worse.
Then people came up with the magical operation of complement, and let's take a look at how it turns out:
The result of this calculation is as correct as we expected.
Let's take a closer look at why the complement operation is correct. Let's analyze it carefully:
When a large number minus a decimal, the result must be a positive number. In the inverse code operation we used before, the result is always 1 less. If we use the complement to calculate, the negative number will be added from the inverse code to the complement code. After calculating the result, because the complement of the positive number is the same as the inverse code, there is no need to subtract it. So it happens to be equivalent to adding 1. Wonderful, unspeakable
When decimals subtract large numbers, the result must be negative. If you use the complement operation, the negative number from the inverse code to the complement code to add 1, and just, the result is a negative number, this negative number from the complement code to the original code to subtract 1, just offset, the result is not affected. Wonderful, unspeakable.
The invention of complement completely simplifies our hardware circuit, there is no need to design additional circuits for subtraction, so that we can calculate subtraction only through the addition circuit, which is amazing.
When you see this, do you have the first two words:
Shit, there is still this kind of operation!
Shit, why didn't I think of it so simple!
This article comes from the official account of Wechat: coding ID:gh_f65e0111d17a, author: Liu Yaxi
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.