Re-probing the CRC (turn)

Source: Internet
Author: User
Tags crc32 crc32 checksum

Source: http://hi.baidu.com/skystalker/item/228a263147f74e87f5e4ad8d

Previously wrote CRC16 program, although can use, but do not know its heart, now to use CRC32, relive again, once on the pass. Notes are as follows
CRC I'm not mistaken. Cyclic redundancy code,cyclic and redundancy very expressive, so-called redundancy is the additional information, this is the calculation of the raw data below why the original data to the left four-bit reasons,


///
The simplest CRC implement algorithm.
///
/*
Load The register with zero bits.
Augment the message by appending W zero bits to the end of it.
While (more message bits)
Begin
Shift The register left by one bit, reading the next bit of the augmented message into register bit position 0.
If (a 1 bit popped out of the register during step 3)
Register = Register XOR Poly.
End
The register now contains the remainder.
*/

#include <stdio.h>

#define POLY 0x13

int main ()
{
The data
unsigned short data = 0x035b;
Load the register with zero bits
unsigned short regi = 0x0000;
Augment the data by appending W (4) zero bits to the end of it.
Data <<= 4;
We do it bit after bit
for (int cur_bit = cur_bit >= 0;--cur_bit)
{
Test the highest bit which would be poped later.
In fact, the 5th bit from the right was the hightest bit here
if (((Regi >> 4) & 0x0001) = = 0x1)//gather enough 5 digits (as with dividend, the number of bits that generate the polynomial), modulo 2 except
{
Regi = regi ^ POLY;
}
Shift the Register Regi <<= 1;
Reading the next bit of the augmented data
unsigned short TMP = (data >> cur_bit) & 0x0001;
Regi |= tmp;

}
And now, register contains the remainder which is also called CRC value.
return 0;
}
The above procedure is the simulation of the algorithm in the above picture, the steps are exactly the same.
Some Popular Polys is:
Bits: (16,12,5,0) [X25 Standard]
(16,15,2,0) ["CRC-16"]
Bits: (32,26,23,22,16,12,11,10,8,7,5,4,2,1,0) [Ethernet]
We commonly use CRC to generate polynomial as above, if the CRC32 checksum is to be calculated by bit, it will be a big project. Therefore, it is generally calculated as a byte, because the number of registers in the computer is 8 bits.
Let's start by looking at a feature of XOR, which is the basis for the following description:
Or the example of the calculation in the photo, where the first place is 0.
Original message:1101011011
poly:10011
Message after appending W zeros:11010110110000
Now we simply divide the augmented message by the poly using CRC
Arithmetic. This is the same division as before:
1100001010 = Quotient (nobody cares about the quotient)
_______________
10011) 11010110110000 = Augmented message (1101011011 + 0000)
=poly 10011,,.,,....
-----,,.,,....
10011,.,,....//The remainder of each time is the current value in the register, where the register has shifted left one bit,//read into a new data
10011,.,,....
-----,.,,....
00001.,,....//First is 0, the register value is smaller than the divisor, continue reading into the next
00000.,,....
-----.,,....
00010,,....
00000,,....
-----,,....
00101,....
00000,....
-----,....
01011 .....
00000 .....
-----....
10110 ...
10011 ...
-----...
01010..
00000..
-----..
10100.
10011.
-----.
01110
00000
-----
1110 = remainder = the CHECKSUM!!!!

Let's try another algorithm that separates data 1101011011 in 5 bits: 11010,11011
First of 11010 to do a CRC check on the Poly, that is, 110100000 modulo 2 in addition to poly the result is 1000, 1000 0000 and 110110000 XOR, get 10110000 re-modulo 2 in addition to poly, the result is 1110 and the previous calculation results.

See here, you may think of, the data is divided into 8 bits, the highest bit of the first byte of the CRC check, check the value of the next byte xor or check .... , and finally we get the CRC check value.

Re-probing the CRC (turn)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.