Recognize the original code, inverse code and complement code

Preface

In the computer world, all data ultimately exists in binary form. For example, the decimal numbers used in our human lives can be converted into binary according to certain rules for storage. In addition, there are octal and hexadecimals. Converted to binary.

Many other characters like punctuation marks, letters and Chinese are how they are represented in the computer? A code table is stored inside the computer, and different binary numbers correspond to only one character, such as 0100 0001, this string of numbers represents uppercase letters A.

So how does the computer store pictures and videos? A picture is composed of many pixels on the screen, and each pixel can be represented by a binary number. For example, a 4-bit binary number represents a pixel. , Then there are 8 combinations of 4-bit binary numbers, and the pixel can also represent eight colors. If one byte (8-bit binary number) is used to represent a pixel, then each pixel has a total of 256 kinds The choice of color. The more pixels and denser, the richer the color of the picture, and the clearer the final effect. The video is formed by the rapid playback of the picture, and the principle is similar.

It can be seen from the above overview that the bottom layer of the computer can represent everything with only a binary number composed of only 0 and 1, and binary is a bunch of numbers after all, and arithmetic operations can be performed by numbers. We have huge and complex information today. The operation of the bottom of the society has been transformed into a bunch of binary digit calculations. This shows how important binary calculations are.

 

Base conversion

 

Convert decimal to binary

 

Converting a decimal integer to a binary integer uses the method of "divide by 2 and take the remainder, and arrange in reverse order". Divide the decimal by 2 to get the quotient and remainder; divide the quotient by 2 again to get a quotient and remainder, and do so until the quotient is less than 1. So far, the last quotient is gradually combined with the remainder from bottom to top. The decimal number 173 is converted to the binary number 010101101, and the first 0 can be omitted. As follows:

                                            

                                             

 

Convert binary to decimal

 

  • First write the binary numbers as weighted coefficient expansions, and then sum them according to the decimal addition rules. This method is called "addition by power" method
  • For example, 1101, the weighting coefficient corresponding to the first place on the far right is the zero power of 2, the second place is the first power of 2, and the third place is the second power of 2, and so on
  • Each number is multiplied by its weighting factor and added together to get the sum equal to the value of the decimal number.
  • 1*2^3 + 1*2^2 +0*2^1 + 1*2^0 = 8 + 4 + 0 + 1 = 13

 

Original code

Decades ago, many scientists in the military and aerospace fields needed to perform large-scale calculations of large numbers, such as how many tens of millions by how many tens of millions. Artificial calculations of these numbers are not only error-prone but also very inefficient. Computers therefore It came into being. The purpose of its birth is to help humans quickly calculate the result. If we need to create a computer now, we enter the decimal addition 10 + 10 for it, and it should return a result of 20, then how is this process achieved? What?

  • It will first convert 10 into a binary number 0000 1010
  • Add two binary digits to get the result 0001 0100, and then convert it to decimal to equal 20. 
  •  

       

From the above calculation process, it can be seen that the computer we created currently has no problem with addition. The problem of addition is solved, and there is another problem. In our commonly used decimal system, the numbers are divided into positive sums. Negative such as +10 and -10, then how do you indicate whether a number is positive or negative in binary?

In the decimal system, we use + to represent positive and-to represent negative numbers. Similarly, in binary, 0 can represent + and 1 can represent -. For example, an 8-bit binary number 0000 0001. The highest bit is the eighth bit as the sign bit. , 0 means a positive number, and 1 means a negative number. So 0000 0001 is equivalent to decimal +1. And 1000 0001 means decimal -1. The binary data defined in this method is called the original code.

We have now created an encoding method called the original code. It is okay to use the binary data represented by the original code to perform addition operations, and it can also represent positive and negative numbers. Since it can represent negative numbers, let's test the subtraction operation .

         

  • 0000 0001 is equal to decimal +1, and 1000 0001 is equal to decimal -1. 1 + (-1) is equivalent to 1-1

  • The result of 1-1 is equal to 0, but the result of binary calculation is 1000 0010, which is equal to -2 in decimal. The calculation result is obviously wrong.

  • The drawbacks of this original code are revealed. It can only be used for addition but not for subtraction. So what is the use of it. So next, we need to find a new encoding method that allows binary numbers to be used for addition and subtraction. .

 

Derivation of complement

Now we need to find a new encoding method to enable binary subtraction. We can start with the most essential operation process of mathematics to see if we can find the law. For example, the decimal operation 100-10 can be operated in the following way:

100 -10 = 

100 -10 + 100 -100 = 

100 - 10 + 99 + 1 - 100 = 

100 + (99 - 10) + 1 - 100
  • 100-10 is equivalent to 100 + (-10), -10 is converted to (99-10) + 1, what are the benefits of doing this? 10 is a two-digit number, and 99 is the largest number of two-digits. If the digits of the two numbers are the same and the subtracted number is the largest number, then this subtraction will not take place. The reason why the subtraction is troublesome is because the decimal number needs to be borrowed from the previous one. Now it is converted into this The form avoids borrowing.
  • The key to doing subtraction to avoid borrowing is to see how many digits are in the subtraction and add a number that is one bit larger than the number of digits in the subtraction. For example, if the subtraction is 10, then add 100 afterwards. Subtract 100. If the subtraction is 100, add 1000 to the end and then subtract 1000.
  • 100 can be converted to 99 + 1, so -10 is converted to (99 -10) + 1
  • Some enlightenment can be gained from the above derivation process. If a number is positive, such as 100, then it is still 100 without any processing. But if a number is negative, such as -10, it can be transformed into the largest number of digits in the number. Subtract this number from the number and add 1.
  • The largest number minus a number such as 99 -10 is not the complement of 10? And the complement plus 1 is not the complement. In order to further clarify, we convert the above calculation process into binary.
100 - 10 =

01100100 - 00001010 = 

01100100 - 00001010 + 100000000 - 100000000 = 

01100100 - 00001010 + 11111111 + 1 - 100000000 = 

01100100 + (11111111 - 00001010) + 1 - 100000000
  • We have often seen this conclusion in textbooks before: the one's complement and complement of a positive binary number are equal to the positive number itself. The one's complement of a negative binary number is equal to the sign bit unchanged, and the other bits are the result of bit inversion. The complement of a negative binary number is equal to the complement of the negative number plus 1.
  • For example, 1000 1101, its inverse code can be transformed like this: the most significant bit of the sign bit 1 remains unchanged, the other bits are inverted by bit 1 to become 0, 0 becomes 1, and the result is equal to 1111 0010. For example, a three-digit unsigned number 001 it The result of bit inversion is 110. Then 111-001 is equal to 110. In other words, the largest number minus this number is its inverse code.
  • Let's look back at the above calculation process (11111111-00001010) is the inverse of -00001010, and add 1 to become the complement.

Continue to calculate

01100100 + (11111111 - 00001010) + 1 - 100000000 = 

01100100 + 11110110 - 100000000 = 

  • 01100100 + 11110110 = 1 01011010-100000000 = 01011010, converted to decimal equal to 90, the calculation result is correct.
  • The result of the above calculation of 01100100 + 11110110 produces a carry that cancels out the following 1 0000 0000. 0110 0100 is the complement of decimal 100, and 1111 0110 is the complement of decimal -10. If a positive and a negative complement are compared Add, if it generates a carry, the highest bit is equal to 0, it is a positive number, the original code of the positive number = inverse code = complement. The result of adding the above two complements is 1 0101 1010, which produces a Carry 1 (assuming that the arithmetic unit stores up to 8 bits) overflows, and the result of discarding becomes 01011010. It is a positive number, so its original code is equal to the complement, and the decimal size can be calculated from the original code.
  • Imagine a special situation where the addition of two complements does not produce a carry, then the data obtained by adding these two numbers is assumed to be x, and the highest bit of x must be 1. The conversion can be continued later. x-1 0000 0000 = x- 1111 1111-1 = -(1111 1111-x + 1). x is definitely a negative number, so the sign bit is proposed, and the negation code of x is added to get the final value. From here, the sign bit Keep it unchanged, and the complement of the complement is its original code.

 

to sum up

1. Through unremitting exploration in the world of mathematics, we finally found a coding method (that is, complement code) to achieve subtraction. Now input two decimal data to the computer, such as 100 -10, and hope that the computer will eventually return 90 Give it to me. First, it will calculate the complement of 100 and the complement of -10 respectively, and add them together, and the sum is still the complement, and then complement the complement to get the original code of the data. The corresponding decimal value can also be calculated through the original code, and this value can be returned to the user at this time.

2. How to find the complement of a binary data?

If the highest bit of the binary data (original code form) is 0, then the data is a positive number. Complement code = inverse code = the data itself.

If the highest bit of the binary data (in the original code form) is 1, then the data is a negative number. The sign bit of the highest bit remains unchanged, and the other bits are inverted bit by bit. Add 1 to get the complement.

 3. The meaning of complement

The original code is friendly to humans. We can easily convert decimal data into binary original code, and we can also calculate the corresponding decimal data through the original code. Although the original code is good, it can’t do subtraction. This is the complement The reason for the birth of the code. For the computer, the original code and the inverse code are of little significance. They are all to welcome the birth of the complement. The binary data at the bottom of the computer is stored in the form of the complement, which can be more convenient Perform arithmetic operations. At this point, we also understand that in the end, any external input data is converted into a complement in the computer for calculation and storage, and the complement is the representation of binary data.

 

 

 

Guess you like

Origin blog.csdn.net/brokenkay/article/details/107381271