<s>
Computation	O
of	O
a	O
cyclic	O
redundancy	O
check	O
is	O
derived	O
from	O
the	O
mathematics	O
of	O
polynomial	B-Algorithm
division	I-Algorithm
,	I-Algorithm
modulo	I-Algorithm
two	I-Algorithm
.	O
</s>
<s>
In	O
practice	O
,	O
it	O
resembles	O
long	B-Algorithm
division	I-Algorithm
of	O
the	O
binary	O
message	O
string	O
,	O
with	O
a	O
fixed	O
number	O
of	O
zeroes	O
appended	O
,	O
by	O
the	O
"	O
generator	O
polynomial	O
"	O
string	O
except	O
that	O
exclusive	O
or	O
operations	O
replace	O
subtractions	O
.	O
</s>
<s>
Division	O
of	O
this	O
type	O
is	O
efficiently	O
realised	O
in	O
hardware	B-Architecture
by	O
a	O
modified	O
shift	B-General_Concept
register	I-General_Concept
,	O
and	O
in	O
software	O
by	O
a	O
series	O
of	O
equivalent	O
algorithms	O
,	O
starting	O
with	O
simple	O
code	O
close	O
to	O
the	O
mathematics	O
and	O
becoming	O
faster	O
(	O
and	O
arguably	O
more	O
obfuscated	O
)	O
through	O
byte-wise	O
parallelism	B-Operating_System
and	O
space	O
–	O
time	O
tradeoffs	O
.	O
</s>
<s>
Various	O
CRC	O
standards	O
extend	O
the	O
polynomial	O
division	O
algorithm	O
by	O
specifying	O
an	O
initial	O
shift	B-General_Concept
register	I-General_Concept
value	O
,	O
a	O
final	O
Exclusive-Or	O
step	O
and	O
,	O
most	O
critically	O
,	O
a	O
bit	O
ordering	O
(	O
endianness	O
)	O
.	O
</s>
<s>
As	O
an	O
example	O
of	O
implementing	O
polynomial	O
division	O
in	O
hardware	B-Architecture
,	O
suppose	O
that	O
we	O
are	O
trying	O
to	O
compute	O
an	O
8-bit	O
CRC	O
of	O
an	O
8-bit	O
message	O
made	O
of	O
the	O
ASCII	B-Protocol
character	I-Protocol
"	O
W	O
"	O
,	O
which	O
is	O
binary	O
010101112	O
,	O
decimal	O
8710	O
,	O
or	O
hexadecimal	O
5716	O
.	O
</s>
<s>
For	O
illustration	O
,	O
we	O
will	O
use	O
the	O
CRC-8-ATM	O
(	O
HEC	B-Protocol
)	O
polynomial	O
.	O
</s>
<s>
The	O
byte	B-Application
value	O
5716	O
can	O
be	O
transmitted	O
in	O
two	O
different	O
orders	O
,	O
depending	O
on	O
the	O
bit	O
ordering	O
convention	O
used	O
.	O
</s>
<s>
This	O
is	O
just	O
like	O
decimal	O
long	B-Algorithm
division	I-Algorithm
,	O
but	O
even	O
simpler	O
because	O
the	O
only	O
possible	O
multiples	O
at	O
each	O
step	O
are	O
0	O
and	O
1	O
,	O
and	O
the	O
subtractions	O
borrow	B-Algorithm
"	O
from	O
infinity	O
"	O
instead	O
of	O
reducing	O
the	O
upper	O
digits	O
.	O
</s>
<s>
use	O
an	O
-bit	O
shift	B-General_Concept
register	I-General_Concept
to	O
hold	O
only	O
the	O
interesting	O
bits	O
.	O
</s>
<s>
Here	O
is	O
a	O
first	O
draft	O
of	O
some	O
pseudocode	B-Language
for	O
computing	O
an	O
n-bit	O
CRC	O
.	O
</s>
<s>
It	O
uses	O
a	O
contrived	O
composite	B-Language
data	I-Language
type	I-Language
for	O
polynomials	O
,	O
where	O
x	O
is	O
not	O
an	O
integer	O
variable	O
,	O
but	O
a	O
constructor	O
generating	O
a	O
Polynomial	O
object	O
that	O
can	O
be	O
added	O
,	O
multiplied	O
and	O
exponentiated	O
.	O
</s>
<s>
Note	O
that	O
this	O
example	O
code	O
avoids	O
the	O
need	O
to	O
specify	O
a	O
bit-ordering	O
convention	O
by	O
not	O
using	O
bytes	B-Application
;	O
the	O
input	O
bitString	O
is	O
already	O
in	O
the	O
form	O
of	O
a	O
bit	O
array	O
,	O
and	O
the	O
remainderPolynomial	O
is	O
manipulated	O
in	O
terms	O
of	O
polynomial	O
operations	O
;	O
the	O
multiplication	O
by	O
could	O
be	O
a	O
left	O
or	O
right	O
shift	O
,	O
and	O
the	O
addition	O
of	O
bitString[i+n]	O
is	O
done	O
to	O
the	O
coefficient	O
,	O
which	O
could	O
be	O
the	O
right	O
or	O
left	O
end	O
of	O
the	O
register	O
.	O
</s>
<s>
The	O
second	O
problem	O
could	O
be	O
solved	O
by	O
doing	O
the	O
last	O
n	O
iterations	O
differently	O
,	O
but	O
there	O
is	O
a	O
more	O
subtle	O
optimization	O
which	O
is	O
used	O
universally	O
,	O
in	O
both	O
hardware	B-Architecture
and	O
software	O
implementations	O
.	O
</s>
<s>
This	O
is	O
the	O
standard	O
bit-at-a-time	O
hardware	B-Architecture
CRC	O
implementation	O
,	O
and	O
is	O
well	O
worthy	O
of	O
study	O
;	O
once	O
you	O
understand	O
why	O
this	O
computes	O
exactly	O
the	O
same	O
result	O
as	O
the	O
first	O
version	O
,	O
the	O
remaining	O
optimizations	O
are	O
quite	O
straightforward	O
.	O
</s>
<s>
It	O
is	O
usually	O
convenient	O
to	O
perform	O
the	O
xor	O
a	O
byte	B-Application
at	O
a	O
time	O
,	O
even	O
in	O
a	O
bit-at-a-time	O
implementation	O
like	O
this	O
:	O
</s>
<s>
This	O
is	O
usually	O
the	O
most	O
compact	O
software	O
implementation	O
,	O
used	O
in	O
microcontrollers	B-Architecture
when	O
space	O
is	O
at	O
a	O
premium	O
over	O
speed	O
.	O
</s>
<s>
When	O
implemented	O
in	O
bit	B-Protocol
serial	I-Protocol
hardware	B-Architecture
,	O
the	O
generator	O
polynomial	O
uniquely	O
describes	O
the	O
bit	O
assignment	O
;	O
the	O
first	O
bit	O
transmitted	O
is	O
always	O
the	O
coefficient	O
of	O
the	O
highest	O
power	O
of	O
,	O
and	O
the	O
last	O
bits	O
transmitted	O
are	O
the	O
CRC	O
remainder	O
,	O
starting	O
with	O
the	O
coefficient	O
of	O
and	O
ending	O
with	O
the	O
coefficient	O
of	O
,	O
a.k.a.	O
</s>
<s>
However	O
,	O
when	O
bits	O
are	O
processed	O
a	O
byte	B-Application
at	O
a	O
time	O
,	O
such	O
as	O
when	O
using	O
parallel	O
transmission	O
,	O
byte	B-Application
framing	O
as	O
in	O
8B/10B	B-Protocol
encoding	I-Protocol
or	O
RS-232-style	O
asynchronous	O
serial	B-Protocol
communication	I-Protocol
,	O
or	O
when	O
implementing	O
a	O
CRC	O
in	O
software	O
,	O
it	O
is	O
necessary	O
to	O
specify	O
the	O
bit	O
ordering	O
(	O
endianness	O
)	O
of	O
the	O
data	O
;	O
which	O
bit	O
in	O
each	O
byte	B-Application
is	O
considered	O
"	O
first	O
"	O
and	O
will	O
be	O
the	O
coefficient	O
of	O
the	O
higher	O
power	O
of	O
.	O
</s>
<s>
If	O
the	O
data	O
is	O
destined	O
for	O
serial	B-Protocol
communication	I-Protocol
,	O
it	O
is	O
best	O
to	O
use	O
the	O
bit	O
ordering	O
the	O
data	O
will	O
ultimately	O
be	O
sent	O
in	O
.	O
</s>
<s>
For	O
example	O
,	O
both	O
IEEE	O
802	O
(	O
ethernet	O
)	O
and	O
RS-232	O
(	O
serial	B-Protocol
port	I-Protocol
)	O
standards	O
specify	O
least-significant	O
bit	O
first	O
(	O
little-endian	O
)	O
transmission	O
,	O
so	O
a	O
software	O
CRC	O
implementation	O
to	O
protect	O
data	O
sent	O
across	O
such	O
a	O
link	O
should	O
map	O
the	O
least	O
significant	O
bits	O
in	O
each	O
byte	B-Application
to	O
coefficients	O
of	O
the	O
highest	O
powers	O
of	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
floppy	B-Device
disks	I-Device
and	O
most	O
hard	B-Device
drives	I-Device
write	O
the	O
most	O
significant	O
bit	O
of	O
each	O
byte	B-Application
first	O
.	O
</s>
<s>
Thus	O
,	O
for	O
example	O
,	O
the	O
XMODEM-CRC	O
extension	O
,	O
an	O
early	O
use	O
of	O
CRCs	O
in	O
software	O
,	O
uses	O
an	O
msbit-first	O
CRC	O
.	O
</s>
<s>
So	O
far	O
,	O
the	O
pseudocode	B-Language
has	O
avoided	O
specifying	O
the	O
ordering	O
of	O
bits	O
within	O
bytes	B-Application
by	O
describing	O
shifts	O
in	O
the	O
pseudocode	B-Language
as	O
multiplications	O
by	O
and	O
writing	O
explicit	O
conversions	O
from	O
binary	O
to	O
polynomial	O
form	O
.	O
</s>
<s>
The	O
above	O
pseudocode	B-Language
can	O
be	O
written	O
in	O
both	O
forms	O
.	O
</s>
<s>
In	O
either	O
case	O
,	O
be	O
sure	O
to	O
transmit	O
the	O
bytes	B-Application
of	O
the	O
CRC	O
in	O
the	O
order	O
that	O
matches	O
your	O
chosen	O
bit-ordering	O
convention	O
.	O
</s>
<s>
Another	O
common	O
optimization	O
uses	O
a	O
lookup	B-Data_Structure
table	I-Data_Structure
indexed	O
by	O
highest	O
order	O
coefficients	O
of	O
rem	O
to	O
process	O
more	O
than	O
one	O
bit	O
of	O
dividend	O
per	O
iteration	O
.	O
</s>
<s>
Most	O
commonly	O
,	O
a	O
256-entry	O
lookup	B-Data_Structure
table	I-Data_Structure
is	O
used	O
,	O
replacing	O
the	O
body	O
of	O
the	O
outer	O
loop	O
(	O
over	O
i	O
)	O
with	O
:	O
</s>
<s>
One	O
of	O
the	O
most	O
commonly	O
encountered	O
CRC	O
algorithms	O
is	O
known	O
as	O
CRC-32	O
,	O
used	O
by	O
(	O
among	O
others	O
)	O
Ethernet	O
,	O
FDDI	B-Protocol
,	O
ZIP	B-General_Concept
and	O
other	O
archive	B-General_Concept
formats	I-General_Concept
,	O
and	O
PNG	O
image	O
format	O
.	O
</s>
<s>
You	O
will	O
note	O
that	O
the	O
code	O
corresponds	O
to	O
the	O
lsbit-first	O
byte-at-a-time	O
pseudocode	B-Language
presented	O
here	O
,	O
and	O
the	O
table	O
is	O
generated	O
using	O
the	O
bit-at-a-time	O
code	O
.	O
</s>
<s>
In	O
small	O
microcontrollers	B-Architecture
,	O
using	O
a	O
16-entry	O
table	O
to	O
process	O
four	O
bits	O
at	O
a	O
time	O
gives	O
a	O
useful	O
speed	O
improvement	O
while	O
keeping	O
the	O
table	O
small	O
.	O
</s>
<s>
One	O
popular	O
technique	O
is	O
to	O
use	O
the	O
bit-at-a-time	O
code	O
256	O
times	O
to	O
generate	O
the	O
CRCs	O
of	O
the	O
256	O
possible	O
8-bit	O
bytes	B-Application
.	O
</s>
<s>
The	O
CRCTable	O
is	O
a	O
memoization	O
of	O
a	O
calculation	O
that	O
would	O
have	O
to	O
be	O
repeated	O
for	O
each	O
byte	B-Application
of	O
the	O
message	O
(	O
)	O
.	O
</s>
<s>
Doing	O
so	O
maximizes	O
performance	O
on	O
superscalar	B-General_Concept
processors	I-General_Concept
.	O
</s>
<s>
Parallel	O
update	O
for	O
a	O
byte	B-Application
or	O
a	O
word	O
at	O
a	O
time	O
can	O
also	O
be	O
done	O
explicitly	O
,	O
without	O
a	O
table	O
.	O
</s>
<s>
This	O
is	O
normally	O
used	O
in	O
high-speed	O
hardware	B-Architecture
implementations	O
.	O
</s>
<s>
As	O
the	O
CRC-32	O
polynomial	O
has	O
a	O
large	O
number	O
of	O
terms	O
,	O
when	O
computing	O
the	O
remainder	O
a	O
byte	B-Application
at	O
a	O
time	O
each	O
bit	O
depends	O
on	O
several	O
bits	O
of	O
the	O
previous	O
iteration	O
.	O
</s>
<s>
In	O
byte-parallel	O
hardware	B-Architecture
implementations	O
this	O
calls	O
for	O
either	O
multiple-input	O
or	O
cascaded	O
XOR	O
gates	O
which	O
increases	O
propagation	O
delay	O
.	O
</s>
<s>
To	O
maximise	O
computation	O
speed	O
,	O
an	O
intermediate	O
remainder	O
can	O
be	O
calculated	O
by	O
passing	O
the	O
message	O
through	O
a	O
123-bit	O
shift	B-General_Concept
register	I-General_Concept
.	O
</s>
<s>
The	O
polynomial	O
is	O
a	O
carefully	O
selected	O
multiple	O
of	O
the	O
standard	O
polynomial	O
such	O
that	O
the	O
terms	O
(	O
feedback	O
taps	O
)	O
are	O
widely	O
spaced	O
,	O
and	O
no	O
bit	O
of	O
the	O
remainder	O
is	O
XORed	O
more	O
than	O
once	O
per	O
byte	B-Application
iteration	O
.	O
</s>
<s>
Finally	O
the	O
intermediate	O
remainder	O
is	O
divided	O
by	O
the	O
standard	O
polynomial	O
in	O
a	O
second	O
shift	B-General_Concept
register	I-General_Concept
to	O
yield	O
the	O
CRC-32	O
remainder	O
.	O
</s>
<s>
Block-wise	O
computation	O
of	O
the	O
remainder	O
can	O
be	O
performed	O
in	O
hardware	B-Architecture
for	O
any	O
CRC	O
polynomial	O
by	O
factorizing	O
the	O
State	O
Space	O
transformation	O
matrix	O
needed	O
to	O
compute	O
the	O
remainder	O
into	O
two	O
simpler	O
Toeplitz	O
matrices	O
.	O
</s>
<s>
used	O
in	O
hardware	B-Architecture
.	O
</s>
<s>
When	O
the	O
CRC	O
is	O
transmitted	O
with	O
the	O
correct	O
byte	B-Application
order	O
(	O
matching	O
the	O
chosen	O
bit-ordering	O
convention	O
)	O
,	O
a	O
receiver	O
can	O
compute	O
an	O
overall	O
CRC	O
,	O
over	O
the	O
message	O
and	O
the	O
CRC	O
,	O
and	O
if	O
they	O
are	O
correct	O
,	O
the	O
result	O
will	O
be	O
zero	O
.	O
</s>
<s>
In	O
fact	O
,	O
a	O
few	O
protocols	O
use	O
the	O
CRC	O
*	O
as*	O
the	O
ending	O
delimiter	O
--	O
CRC-based	B-Protocol
framing	I-Protocol
.	O
</s>
<s>
If	O
it	O
is	O
possible	O
that	O
a	O
transmission	O
error	O
could	O
add	O
such	O
bits	O
,	O
a	O
simple	O
solution	O
is	O
to	O
start	O
with	O
the	O
rem	O
shift	B-General_Concept
register	I-General_Concept
set	O
to	O
some	O
non-zero	O
value	O
;	O
for	O
convenience	O
,	O
the	O
all-ones	O
value	O
is	O
typically	O
used	O
.	O
</s>
