On the Primitive Code, Inverse Code, Complement Code and Shift Code of Operating System



This article casually talks about the way computers represent data, which is also a summary for future review. If there are any mistakes, please give your advice and make progress together.

  • Numerical representation in computer
    Any data exists in the form of binary machine code in a computer. For example, +2 in real life is represented as 00000010 in a machine (let’s assume that the word length of this computer is defined as 8 bits). But this is the machine code corresponding to + 2, because the first is used to represent the positive and negative values: 0 is positive and 1 is negative. So if you want to express – 2, the corresponding machine code is 10000010, so when you see a series of machine codes, you should first consider the positive and negative values. It is wrong to calculate the true value of 10000010 directly by weight expansion, if it is directly by weight expansion.1*2^7 + 1*2^1 = 130 Obviously not 2. Because the first bit is 1, it is negative, and then the weight expansion of the latter bit is calculated to be -2.
  • Original code
    As an example in the previous paragraph, the value represented by machine code is called the original code, the original code of + 2 is 00000010, and the original code of – 2 is 10000010.
  • Inverse code
    Inverse code is generally the least useful representation of computer. Why do we talk about it a little? First, how to express inverse code: if it is positive, it is the original code itself, and there is no change. If it is the inverse code of negative numbers, then the number of the sign bits, that is, the first bit, remains unchanged, and the rest of the bits are inversed. For example, if the original code of + 2 is 00000010, then the inverse code of + 2 is 00000010. – The original code of 2 is 10000010, and the inverse code is 1111101.
  • Complement code
    Complement is one of the most frequently used encoding methods in computer numerical operations. The specific reasons are discussed a little. First, let’s talk about the representation of complement. The positive complement is the original code itself, which is the same as the inverse code. The complement of negative number is the result of its inverse code + 1. For example, the complement of + 2 is 00000010. The complement of – 2: 11111111101 (counter code) +1 = 11111111110.
  • Frameshift
    Shift code is to add a bias constant to the value. When the number of coding bits is n, it usually shifts to the N-1 power of 2. For example, the shift code of – 2 = the 7th power of the original code + 2 = 00000010 + 10000000 = 10000010, which can be used as the complement of – 2 11111110Sign bits remain unchanged and the remaining bits are reversed + 1
  • Why are there complements and countercodes?
    Modular operation system
    Take clocks and watches in life. If 10 o’clock becomes 6 o’clock, you can call back 4, 10-4 = 6. You can also dial 8 clockwise, 10 + 8 = 18 modulus 12 = 6. It can be seen that for module 12, – 4 and + 8 are equal. 8 is the complement of 4 for module 12. In this way, all subtraction calculations can be converted into addition operations.
    If it is a “4-bit decimal” module system operation, 9828-1928 operation is equivalent to 9828 + (10 ^ 4-1928) = 9828 + 8072 = 17000, take 10 ^ 4 module = 7000. So when subtracting in computer, it is equivalent to adding – 1928 complement.
    In fact, it is the most intuitive and simple for our brain to use the original code directly for numerical calculation. Why can’t computers use the original code directly for numerical calculation, because there is a very important operational factor – negative number. We know that the first bit of the original code is the symbol bit 0 for the positive number 1 for the negative number, but the computer does not know.If 1+(-1) is calculated, the result is 00000001 (+1 primitive code) +10000001 (-1 primitive code) = 10000010 (primitive code) =-2.Obviously the result is wrong.
    So does the birth of inverse code solve this kind of problem? Come and try.1+(-1) = 00000001 (original) + 10000001 (original) = 00000001 (negative) +111111111110 (negative) = 11111111111 (negative) = 10000000 (original) = - 0Notice here0 is a signed bit and a negative value。 This is inconsistent with our mathematical operation rules (although zero is positive and negative in reality, the correct result in the computer should be + 0)
    At last, the encoding method of complement code is used to compute. The result is correct: 1+(-1) = 00000001 (original) + 10000001 (original) = 00000001 (inverse) +111111110 (inverse) = 00000001 (complement) + 11111111111 (complement) = 00000000000 (complement) = 0.
  • Why is there a code shift?

    Code-shifting is mainly introduced for the convenience of computer operation index.That’s one more addition than we do.1.1*2^-1 + 1.2*2^3According to the operation rules, when the base number 2 is the same, the addition of 1.1 and 1.2 can be performed only when the exponents are treated as the same.0.00011*2^3 + 1.2*2^3This makes the operation much simpler. Therefore, in order to perform the operation, we need to compare the exponential part. If the exponential-1 and 3 are compared by the original code coding, the results of the comparison between 10000001 and 00000011 will be formed. Obviously, the ratio is – 1 > 3, and the computer can not perform the accurate operation. If we use the form of shift code, for example, let the exponent-1 and 3 + 4 at the same time, it will be a comparison between 3 and 7. It is easy to compare the size of the comparison, and it is easy to align the maximum power for calculation. So the idea of code shifting comes from this.

Recommended Today

Libp2p RS version 0.3.0 introduction

V0.3.0 released on 4.23, usingAsyncRead & AsyncWriteTo replace ourReadEx & WriteEx & SplitEx; SimplifiedKad/DHTImplementation logic. modify ReadEx & WriteEx & SplitEx: At first we tried to useasync-traitTo define their own IO operationsTraitFor more pure useasync/awaitTo write code. withReadExFor example, it is roughly as follows: #[async_trait] pub trait ReadEx { async fn read(&mut self, buf: &mut […]