CRC check principle

This article is a reprinted article, the original author's blog portal

CRC check principle

The principle of CRC check seems complicated and difficult to understand, because most books are basically explained in binary polynomial form. In fact, the very simple problem, the basic idea is to first add a number after the frame to be sent (this is the check code used for verification, but it should be noted that the number here is also a binary sequence, the same below), generate a The new frame is sent to the receiver. Of course, this additional number is not arbitrary, it is necessary to make the new frame generated can be divided by a specific number jointly selected by the sending end and the receiving end (note that here is not directly using binary division, but a kind of weighing It is " modulo 2 **** division "). After reaching the receiving end, the new frame received is divided by the selected divisor (also using " modulo 2 division **"). Because the data frame has been added to the sender before sending the data frame, and the "remainder" process has been done (that is, it can be divisible), so the result should be no remainder. If there is a remainder, it indicates that an error occurred during the transmission of the frame.

Explanation

"Module 2 division" is similar to "arithmetic division", but it neither borrows from the upper digit, nor compares the magnitude of the same digits in the divisor and dividend, as long as it is divided by the same number of digits. Modulus 2 addition operation is: 1 + 1 = 0, 0 + 1 = 1, 0 + 0 = 0, no carry, nor borrowing; modulo 2 subtraction operation is: 1-1 = 0, 0-1 = 1, 1-0 = 1, 0-0 = 0, no carry, no borrowing. Equivalent to logical XOR operation in binary. That is, after the comparison, if the corresponding bits of the two are the same, the result is "0", and if they are different, the result is "1". If 100101 is divided by 1110, the quotient is 11 and the remainder is 1, as shown in the left figure of Figure 5-9. For example, 11 × 11 = 101, as shown in the right figure of Figure 5-9.

imgimg

Figure 5-9 Examples of "Module 2 Division" and "Module 2 Multiplication"

Specifically, the CRC check principle is the following steps:

  1. First select (can be selected randomly, or according to the standard, as described later) A divisor used to divide the received frame when checking at the receiving end (it is a binary comparison special string, usually a multiple It is expressed in the form of a method, so the CRC is also called a polynomial coding method, and this polynomial is also called "generator polynomial").

  2. Look at the selected divisor binary digits (assumed to be k bits), then add k-1 bit "0" after the data frame to be sent (assumed to be m bits), and then add k-1 " A new frame of 0 "(a total of m + k-1 bits) is divided by the modulo 2 division by the above divisor. The resulting remainder (also a binary bit string) is the CRC check code for the frame, also known as This is FCS (Frame Check Sequence). However, it should be noted that the number of digits in the remainder must be only one less than the number of divisors, even if the previous digits are 0 , or even all 0 **** (when the divisor is attached) can not be omitted .

  3. Then append this check code to the original data frame (that is, the m-bit frame, note that it is not the m + k-1 bit frame formed later), construct a new frame and send it to the receiving end; finally at the receiving end, add this The new frame is divided by the divisor selected in the previous way by "modulo 2 division". If there is no remainder, it means that the frame has no errors during transmission, otherwise an error has occurred.

Through the above introduction, everyone can understand the principle of CRC check, and no longer think it is very complicated.

As can be seen from the above, there are two key points in the CRC check: one is to pre-determine a binary bit string (or polynomial) used by both the sending end and the receiving end as the divisor; second, the original frame is selected Divide by to perform binary division operation to calculate FCS. The former can be selected randomly or according to internationally accepted standards, but the highest and lowest bits must both be " 1 " , such as CRC-16 (that is, CRC-16) used in IBM's SDLC (Synchronous Data Link Control) procedure. This divisor is 17 bits in total) The generator polynomial g (x) = x 16 + x 15 + x 2 +1 (corresponding to the binary bit string: 11000000000000101); and in the ISO HDLC (Advanced Data Link Control) procedure, ITU's SDLC , X.25, V.34, V.41, V.42, etc. use CCITT-16 generator polynomial g (x) = x 16 + x 15 + x 5 +1 (corresponding binary bit string: 11000000000100001).

CRC check code calculation example

From the above analysis, we can see that since the divisor is random or selected according to the standard, the key to CRC check is how to find the remainder, which is the check code (CRC check code).

The following describes the whole process with an example. Now suppose that the selected CRC generator polynomial is G (X) = X4 + X3 + 1, and the CRC check code of the binary sequence 10110011 is required. The following is the specific calculation process:

  1. First convert the generator polynomial into a binary number, which can be known from G (X) = X4 + X3 + 1 (, it is a total of 5 bits (the total number of digits is equal to the power of the highest bit plus 1, that is 4 + 1 = 5), Then according to the meaning of the polynomials (the polynomial only lists the bits with a binary value of 1, that is, the 4th, 3rd, and 0th bits of this binary are all 1, and the other bits are 0). Its binary bit string is 11001 .

  2. Because the number of bits of the generator polynomial is 5, according to the previous introduction, it is known that the number of bits of the CRC check code is 4 (the number of bits of the check code is 1 less than the number of bits of the generator polynomial). Because the original data frame 10110011, add 4 0s behind it to get 10110011 0000 , and then divide this number by the "modulo 2 division" method by the generator polynomial, the resulting remainder (ie CRC code) is 0100 , as shown in Figure 5- 10 is shown. Please refer to the "modulo 2 division" algorithm introduced earlier.

img

Figure 5-10 CRC check code calculation example

  1. Replace the four "0s" behind the original frame 10110011 0000 with the CRC checksum 0100 calculated in the previous step to obtain a new frame 10110011 0100 . Then send this new frame to the receiver.

  2. When the above new frame arrives at the receiving end, the receiving end will use the divisor 11001 selected above to remove it by "modulo 2 division" to verify whether the remainder is 0. If it is 0, it proves that the frame data is being transmitted No errors occurred during the process, otherwise errors occurred.

Through the above analysis of the CRC check principle and the introduction of the CRC check code calculation example, everyone should be clear about this seemingly complicated CRC check principle and calculation method.

Guess you like

Origin www.cnblogs.com/cell-coder/p/12750115.html