Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Cyclic redundancy check
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Computation == {{unreferenced section|date=July 2016}} {{Main|Computation of cyclic redundancy checks}} To compute an ''n''-bit binary CRC, line the bits representing the input in a row, and position the ({{nowrap|''n'' + 1}})-bit pattern representing the CRC's divisor (called a "[[polynomial]]") underneath the left end of the row. In this example, we shall encode 14 bits of message with a 3-bit CRC, with a polynomial {{nowrap|''x''<sup>3</sup> + ''x'' + 1}}. The polynomial is written in binary as the coefficients; a 3rd-degree polynomial has 4 coefficients ({{nowrap|1''x''<sup>3</sup> + 0''x''<sup>2</sup> + 1''x'' + 1}}). In this case, the coefficients are 1, 0, 1 and 1. The result of the calculation is 3 bits long, which is why it is called a 3-bit CRC. However, you need 4 bits to explicitly state the polynomial. Start with the message to be encoded: <pre> 11010011101100 </pre> This is first padded with zeros corresponding to the bit length ''n'' of the CRC. This is done so that the resulting code word is in [[systematic code|systematic]] form. Here is the first calculation for computing a 3-bit CRC: <pre> 11010011101100 000 <--- input right padded by 3 bits 1011 <--- divisor (4 bits) = xΒ³ + x + 1 ------------------ 01100011101100 000 <--- result </pre> 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 right to align with the highest remaining 1 bit in the input, and the process is repeated until the divisor reaches the right-hand end of the input row. Here is the entire calculation: <pre> 11010011101100 000 <--- input right padded by 3 bits 1011 <--- divisor 01100011101100 000 <--- result (note the first four bits are the XOR with the divisor beneath, the rest of the bits are unchanged) 1011 <--- divisor ... 00111011101100 000 1011 00010111101100 000 1011 00000001101100 000 <--- note that the divisor moves over to align with the next 1 in the dividend (since quotient for that step was zero) 1011 (in other words, it doesn't necessarily move one bit per iteration) 00000000110100 000 1011 00000000011000 000 1011 00000000001110 000 1011 00000000000101 000 101 1 ----------------- 00000000000000 100 <--- remainder (3 bits). Division algorithm stops here as dividend is equal to zero. </pre> 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. <pre> 11010011101100 100 <--- input with check value 1011 <--- divisor 01100011101100 100 <--- result 1011 <--- divisor ... 00111011101100 100 ...... 00000000001110 100 1011 00000000000101 100 101 1 ------------------ 00000000000000 000 <--- remainder </pre> The following [[Python (programming language)|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: <syntaxhighlight lang="python"> def crc_remainder(input_bitstring, polynomial_bitstring, initial_filler): """Calculate 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 = (len(polynomial_bitstring) - 1) * initial_filler 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): """Calculate 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:]) </syntaxhighlight> <syntaxhighlight lang="pycon"> >>> crc_remainder('11010011101100', '1011', '0') '100' >>> crc_check('11010011101100', '1011', '100') True </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)