# Cyclic redundancy check

## This section does not cite any sources. Please help improve this section by adding citations to reliable sources. Unsourced material may be challenged and removed. (July 2016) (Learn how and when to remove this template message) Main article: Computation of cyclic redundancy checks To compute an n-bit binary CRC, line the bits representing the input in a row, and position the (n + 1)-bit pattern representing the CRC's divisor (called a "polynomial") underneath the left-hand end of the row. In this example, we shall encode 14 bits of message with a 3-bit CRC, with a polynomial x3 + x + 1. The polynomial is written in binary as the coefficients; a 3rd-degree polynomial has 4 coefficients (1x3 + 0x2 + 1x + 1). In this case, the coefficients are 1, 0, 1 and 1. The result of the calculation is 3 bits long. Start with the message to be encoded: ```11010011101100 ``` This is first padded with zeros corresponding to the bit length n of the CRC. Here is the first calculation for computing a 3-bit CRC: ```11010011101100 000 <--- ------------------="" 000="" 01100011101100="" 1011="" 1="" 3="" bits="" by="" divisor="" input="" padded="" pre="" result="" right="" x=""> The algorithm acts on the bits directly above the divisor in each step. The result for that iteration is the bitwise XOR of the polynomial divisor with the bits above it. The bits not above the divisor are simply copied directly below for that step. The divisor is then shifted one bit to the right, and the process is repeated until the divisor reaches the right-hand end of the input row. Here is the entire calculation: 11010011101100 000 <--- -----------------="" ...="" 00000000000000="" 00000000000101="" 00000000001110="" 00000000011000="" 00000000110100="" 00000001101100="" 00010111101100="" 000="" 00111011101100="" 01100011101100="" 100="" 1011="" 101="" 1="" 3="" algorithm="" align="" are="" as="" beneath="" bit="" bits="" by="" dividend="" division="" divisor="" doesn="" equal="" first="" for="" four="" here="" in="" input="" is="" it="" iteration="" move="" moves="" necessarily="" next="" note="" of="" one="" other="" over="" padded="" per="" pre="" quotient="" remainder="" rest="" result="" right="" since="" step="" stops="" t="" that="" the="" to="" unchanged="" was="" with="" words="" xor="" zero.="" zero=""> Since the leftmost divisor bit zeroed every input bit it touched, when this process ends the only bits in the input row that can be nonzero are the n bits at the right-hand end of the row. These n bits are the remainder of the division step, and will also be the value of the CRC function (unless the chosen CRC specification calls for some postprocessing). The validity of a received message can easily be verified by performing the above calculation again, this time with the check value added instead of zeroes. The remainder should equal zero if there are no detectable errors. 11010011101100 100 <--- ------------------="" ......="" ...="" 00000000000000="" 00000000000101="" 00000000001110="" 000="" 00111011101100="" 01100011101100="" 100="" 1011="" 101="" 1="" check="" divisor="" input="" pre="" remainder="" result="" value="" with=""> The following Python code outlines a function which will return the initial CRC remainder for a chosen input and polynomial, with either 1 or 0 as the initial padding. Note that this code works with string inputs rather than raw numbers: def crc_remainder(input_bitstring, polynomial_bitstring, initial_filler): ''' Calculates the CRC remainder of a string of bits using a chosen polynomial. initial_filler should be '1' or '0'. ''' polynomial_bitstring = polynomial_bitstring.lstrip('0') len_input = len(input_bitstring) initial_padding = initial_filler * (len(polynomial_bitstring) - 1) input_padded_array = list(input_bitstring + initial_padding) while '1' in input_padded_array[:len_input]: cur_shift = input_padded_array.index('1') for i in range(len(polynomial_bitstring)): input_padded_array[cur_shift + i] = str(int(polynomial_bitstring[i] != input_padded_array[cur_shift + i])) return ''.join(input_padded_array)[len_input:] def crc_check(input_bitstring, polynomial_bitstring, check_value): ''' Calculates the CRC check of a string of bits using a chosen polynomial. ''' polynomial_bitstring = polynomial_bitstring.lstrip('0') len_input = len(input_bitstring) initial_padding = check_value input_padded_array = list(input_bitstring + initial_padding) while '1' in input_padded_array[:len_input]: cur_shift = input_padded_array.index('1') for i in range(len(polynomial_bitstring)): input_padded_array[cur_shift + i] = str(int(polynomial_bitstring[i] != input_padded_array[cur_shift + i])) return ('1' not in ''.join(input_padded_array)[len_input:]) >>> crc_check('11010011101100','1011','100') True >>> crc_remainder('11010011101100','1011','0') '100' ```

#### SHREY KAPOOR

Shrey Kapoor is a Tech-Enthusiast, Harvard certified Cyber Security and Cyber Forensics Expert. He Founder Techphlie.com, which is one of the India's Top Tech News Website. Even Forbes and many other renowned publishers took his articles reference. Shrey is a Technology analyst, strategic thinker and creative writer who is passionate to deliver the best, latest possible Tech-News to his followers and subscribers. He completed his masters in Artificial Intelligence & Robotics, certified in IPR, T.Q.M. & ISO 9001:2008 In Quality Management Systems.