<s>
The	O
Fletcher	B-Algorithm
checksum	I-Algorithm
is	O
an	O
algorithm	O
for	O
computing	O
a	O
position-dependent	O
checksum	O
devised	O
by	O
John	O
G	O
.	O
Fletcher	O
(	O
1934	O
–	O
2012	O
)	O
at	O
Lawrence	O
Livermore	O
Labs	O
in	O
the	O
late	O
1970s	O
.	O
</s>
<s>
The	O
objective	O
of	O
the	O
Fletcher	B-Algorithm
checksum	I-Algorithm
was	O
to	O
provide	O
error-detection	O
properties	O
approaching	O
those	O
of	O
a	O
cyclic	O
redundancy	O
check	O
but	O
with	O
the	O
lower	O
computational	O
effort	O
associated	O
with	O
summation	O
techniques	O
.	O
</s>
<s>
As	O
with	O
simpler	O
checksum	O
algorithms	O
,	O
the	O
Fletcher	B-Algorithm
checksum	I-Algorithm
involves	O
dividing	O
the	O
binary	O
data	O
word	O
to	O
be	O
protected	O
from	O
errors	O
into	O
short	O
"	O
blocks	O
"	O
of	O
bits	O
and	O
computing	O
the	O
modular	O
sum	O
of	O
those	O
blocks	O
.	O
</s>
<s>
As	O
an	O
example	O
,	O
the	O
data	O
may	O
be	O
a	O
message	O
to	O
be	O
transmitted	O
consisting	O
of	O
136	O
characters	O
,	O
each	O
stored	O
as	O
an	O
8-bit	O
byte	B-Application
,	O
making	O
a	O
data	O
word	O
of	O
1088	O
bits	O
in	O
total	O
.	O
</s>
<s>
So	O
,	O
the	O
simple	O
checksum	O
is	O
computed	O
by	O
adding	O
together	O
all	O
the	O
8-bit	O
bytes	B-Application
of	O
the	O
message	O
,	O
dividing	O
by	O
255	O
and	O
keeping	O
only	O
the	O
remainder	O
.	O
</s>
<s>
The	O
checksum	O
value	O
is	O
transmitted	O
with	O
the	O
message	O
,	O
increasing	O
its	O
length	O
to	O
137	O
bytes	B-Application
,	O
or	O
1096	O
bits	O
.	O
</s>
<s>
The	O
first	O
weakness	O
of	O
the	O
simple	O
checksum	O
is	O
that	O
it	O
is	O
insensitive	O
to	O
the	O
order	O
of	O
the	O
blocks	O
(	O
bytes	B-Application
)	O
in	O
the	O
data	O
word	O
(	O
message	O
)	O
.	O
</s>
<s>
At	O
the	O
end	O
of	O
the	O
data	O
word	O
,	O
the	O
modulus	O
operator	O
is	O
applied	O
and	O
the	O
two	O
values	O
are	O
combined	O
to	O
form	O
the	O
Fletcher	B-Algorithm
checksum	I-Algorithm
value	O
.	O
</s>
<s>
When	O
the	O
data	O
word	O
is	O
divided	O
into	O
8-bit	O
blocks	O
,	O
as	O
in	O
the	O
example	O
above	O
,	O
two	O
8-bit	O
sums	O
result	O
and	O
are	O
combined	O
into	O
a	O
16-bit	O
Fletcher	B-Algorithm
checksum	I-Algorithm
.	O
</s>
<s>
When	O
the	O
data	O
word	O
is	O
divided	O
into	O
16-bit	O
blocks	O
,	O
two	O
16-bit	O
sums	O
result	O
and	O
are	O
combined	O
into	O
a	O
32-bit	O
Fletcher	B-Algorithm
checksum	I-Algorithm
.	O
</s>
<s>
When	O
the	O
data	O
word	O
is	O
divided	O
into	O
32-bit	O
blocks	O
,	O
two	O
32-bit	O
sums	O
result	O
and	O
are	O
combined	O
into	O
a	O
64-bit	O
Fletcher	B-Algorithm
checksum	I-Algorithm
.	O
</s>
<s>
The	O
Adler-32	B-Algorithm
checksum	O
is	O
a	O
specialization	O
of	O
the	O
Fletcher-32	O
checksum	O
devised	O
by	O
Mark	O
Adler	O
.	O
</s>
<s>
One	O
study	O
showed	O
that	O
Fletcher-32	O
outperforms	O
Adler-32	B-Algorithm
in	O
both	O
performance	O
and	O
in	O
its	O
ability	O
to	O
detect	O
errors	O
.	O
</s>
<s>
As	O
an	O
example	O
,	O
a	O
Fletcher-16	O
checksum	O
shall	O
be	O
calculated	O
and	O
verified	O
for	O
a	O
byte	B-Application
stream	O
of	O
0x01	O
0x02	O
.	O
</s>
<s>
It	O
could	O
be	O
transmitted	O
with	O
the	O
byte	B-Application
stream	O
and	O
be	O
verified	O
as	O
such	O
on	O
the	O
receiving	O
end	O
.	O
</s>
<s>
Another	O
option	O
is	O
to	O
compute	O
in	O
a	O
second	O
step	O
a	O
pair	O
of	O
check	O
bytes	B-Application
,	O
which	O
can	O
be	O
appended	O
to	O
the	O
byte	B-Application
stream	O
so	O
that	O
the	O
resulting	O
stream	O
has	O
a	O
global	O
Fletcher-16	O
checksum	O
value	O
of	O
0	O
.	O
</s>
<s>
In	O
our	O
case	O
the	O
checksum	O
bytes	B-Application
are	O
CB0	O
=	O
0xF8	O
and	O
CB1	O
=	O
0x04	O
.	O
</s>
<s>
The	O
transmitted	O
byte	B-Application
stream	O
is	O
0x01	O
0x02	O
0xF8	O
0x04	O
.	O
</s>
<s>
The	O
receiver	O
runs	O
the	O
checksum	O
on	O
all	O
four	O
bytes	B-Application
and	O
calculates	O
a	O
passing	O
checksum	O
of	O
0x00	O
0x00	O
,	O
as	O
illustrated	O
below	O
:	O
</s>
<s>
The	O
Fletcher	B-Algorithm
checksum	I-Algorithm
cannot	O
distinguish	O
between	O
blocks	O
of	O
all	O
0	O
bits	O
and	O
blocks	O
of	O
all	O
1	O
bits	O
.	O
</s>
<s>
This	O
also	O
means	O
a	O
sequence	O
of	O
all	O
00	O
bytes	B-Application
has	O
the	O
same	O
checksum	O
as	O
a	O
sequence	O
(	O
of	O
the	O
same	O
size	O
)	O
of	O
all	O
FF	O
bytes	B-Application
.	O
</s>
<s>
The	O
below	O
is	O
a	O
treatment	O
on	O
how	O
to	O
calculate	O
the	O
checksum	O
including	O
the	O
check	O
bytes	B-Application
;	O
i.e.	O
,	O
the	O
final	O
result	O
should	O
equal	O
0	O
,	O
given	O
properly-calculated	O
check	O
bytes	B-Application
.	O
</s>
<s>
The	O
code	O
by	O
itself	O
,	O
however	O
,	O
will	O
not	O
calculate	O
the	O
check	O
bytes	B-Application
.	O
</s>
<s>
An	O
inefficient	O
but	O
straightforward	O
implementation	O
of	O
a	O
C	B-Language
language	I-Language
function	O
to	O
compute	O
the	O
Fletcher-16	O
checksum	O
of	O
an	O
array	B-Data_Structure
of	O
8-bit	O
data	O
elements	O
follows	O
:	O
</s>
<s>
On	O
lines	O
3	O
and	O
4	O
,	O
the	O
sums	O
are	O
16-bit	O
variables	O
so	O
that	O
the	O
additions	O
on	O
lines	O
9	O
and	O
10	O
will	O
not	O
overflow	B-Algorithm
.	O
</s>
<s>
Here	O
,	O
this	O
is	O
done	O
after	O
each	O
addition	O
,	O
so	O
that	O
at	O
the	O
end	O
of	O
the	O
for	B-Language
loop	I-Language
the	O
sums	O
are	O
always	O
reduced	O
to	O
8	O
bits	O
.	O
</s>
<s>
At	O
the	O
end	O
of	O
the	O
input	O
data	O
,	O
the	O
two	O
sums	O
are	O
combined	O
into	O
the	O
16-bit	O
Fletcher	B-Algorithm
checksum	I-Algorithm
value	O
and	O
returned	O
by	O
the	O
function	O
on	O
line	O
13	O
.	O
</s>
<s>
Example	O
source	O
code	O
for	O
calculating	O
the	O
check	O
bytes	B-Application
,	O
using	O
the	O
above	O
function	O
,	O
is	O
as	O
follows	O
.	O
</s>
<s>
The	O
check	O
bytes	B-Application
may	O
be	O
appended	O
to	O
the	O
end	O
of	O
the	O
data	O
stream	O
,	O
with	O
the	O
c0	O
coming	O
before	O
the	O
c1	O
.	O
</s>
<s>
The	O
most	O
important	O
optimization	O
consists	O
in	O
using	O
larger	O
accumulators	O
and	O
delaying	O
the	O
relatively	O
costly	O
modulo	O
operation	O
for	O
as	O
long	O
as	O
it	O
can	O
be	O
proven	O
that	O
no	O
overflow	B-Algorithm
will	O
occur	O
.	O
</s>
<s>
Here	O
is	O
a	O
C	B-Language
implementation	O
that	O
applies	O
the	O
first	O
but	O
not	O
the	O
second	O
optimization	O
:	O
</s>
<s>
In	O
this	O
respect	O
,	O
the	O
Fletcher	B-Algorithm
checksum	I-Algorithm
is	O
not	O
different	O
from	O
other	O
checksum	O
and	O
CRC	O
algorithms	O
and	O
needs	O
no	O
special	O
explanation	O
.	O
</s>
<s>
An	O
ordering	O
problem	O
that	O
is	O
easy	O
to	O
envision	O
occurs	O
when	O
the	O
data	O
word	O
is	O
transferred	O
byte-by-byte	O
between	O
a	O
big-endian	O
system	O
and	O
a	O
little-endian	O
system	O
and	O
the	O
Fletcher-32	O
checksum	O
is	O
computed	O
.	O
</s>
<s>
If	O
blocks	O
are	O
extracted	O
from	O
the	O
data	O
word	O
in	O
memory	O
by	O
a	O
simple	O
read	O
of	O
a	O
16-bit	O
unsigned	O
integer	O
,	O
then	O
the	O
values	O
of	O
the	O
blocks	O
will	O
be	O
different	O
in	O
the	O
two	O
systems	O
,	O
due	O
to	O
the	O
reversal	O
of	O
the	O
byte	B-Application
order	O
of	O
16-bit	O
data	O
elements	O
in	O
memory	O
,	O
and	O
the	O
checksum	O
result	O
will	O
be	O
different	O
as	O
a	O
consequence	O
.	O
</s>
<s>
Because	O
the	O
Fletcher-16	O
checksum	O
uses	O
8-bit	O
blocks	O
,	O
it	O
is	O
not	O
affected	O
by	O
byte	B-Application
endianness	O
.	O
</s>
