<s>
The	O
IETF	O
(	O
Internet	O
Engineering	O
Task	O
Force	O
)	O
attempts	O
to	O
define	O
"	O
serial	B-Protocol
number	I-Protocol
arithmetic	I-Protocol
"	O
for	O
the	O
purposes	O
of	O
manipulating	O
and	O
comparing	O
these	O
sequence	B-Protocol
numbers	I-Protocol
.	O
</s>
<s>
This	O
task	O
is	O
rather	O
more	O
complex	O
than	O
it	O
might	O
first	O
appear	O
,	O
because	O
most	O
algorithms	O
use	O
fixed-size	O
(	O
binary	O
)	O
representations	O
for	O
sequence	B-Protocol
numbers	I-Protocol
.	O
</s>
<s>
Some	O
protocols	O
choose	O
to	O
ignore	O
these	O
issues	O
and	O
simply	O
use	O
very	O
large	O
integers	O
for	O
their	O
counters	O
,	O
in	O
the	O
hope	O
that	O
the	O
program	O
will	O
be	O
replaced	O
(	O
or	O
they	O
will	O
retire	O
)	O
before	O
the	O
problem	O
occurs	O
(	O
see	O
Y2K	B-Error_Name
)	O
.	O
</s>
<s>
Many	O
communication	O
protocols	O
apply	O
serial	B-Protocol
number	I-Protocol
arithmetic	I-Protocol
to	O
packet	O
sequence	B-Protocol
numbers	I-Protocol
in	O
their	O
implementation	O
of	O
a	O
sliding	O
window	O
protocol	O
.	O
</s>
<s>
Some	O
versions	O
of	O
TCP	O
use	O
protection	O
against	O
wrapped	O
sequence	B-Protocol
numbers	I-Protocol
(	O
PAWS	O
)	O
.	O
</s>
<s>
PAWS	O
applies	O
the	O
same	O
serial	B-Protocol
number	I-Protocol
arithmetic	I-Protocol
to	O
packet	O
timestamps	O
,	O
using	O
the	O
timestamp	O
as	O
an	O
extension	O
of	O
the	O
high-order	O
bits	O
of	O
the	O
sequence	B-Protocol
number	I-Protocol
.	O
</s>
<s>
Only	O
addition	O
of	O
a	O
small	O
positive	O
integer	O
to	O
a	O
sequence	B-Protocol
number	I-Protocol
and	O
comparison	O
of	O
two	O
sequence	B-Protocol
numbers	I-Protocol
are	O
discussed	O
.	O
</s>
<s>
Adding	O
an	O
integer	O
to	O
a	O
sequence	B-Protocol
number	I-Protocol
is	O
simple	O
unsigned	O
integer	O
addition	O
,	O
followed	O
by	O
unsigned	O
modulo	O
operation	O
to	O
bring	O
the	O
result	O
back	O
into	O
range	O
(	O
usually	O
implicit	O
in	O
the	O
unsigned	O
addition	O
,	O
on	O
most	O
architectures	O
)	O
:	O
</s>
<s>
Basically	O
,	O
adding	O
values	O
beyond	O
this	O
range	O
will	O
cause	O
the	O
resultant	O
sequence	B-Protocol
number	I-Protocol
to	O
"	O
wrap	O
"	O
,	O
and	O
(	O
often	O
)	O
result	O
in	O
a	O
number	O
that	O
is	O
considered	O
"	O
less	O
than	O
"	O
the	O
original	O
sequence	B-Protocol
number	I-Protocol
.	O
</s>
<s>
A	O
means	O
of	O
comparing	O
two	O
sequence	B-Protocol
numbers	I-Protocol
i	O
and	O
i	O
(	O
the	O
unsigned	O
integer	O
representations	O
of	O
sequence	B-Protocol
numbers	I-Protocol
s1	O
and	O
s2	O
)	O
is	O
presented	O
.	O
</s>
<s>
The	O
algorithm	O
presented	O
for	O
comparison	O
is	O
complex	O
,	O
having	O
to	O
take	O
into	O
account	O
whether	O
the	O
first	O
sequence	B-Protocol
number	I-Protocol
is	O
close	O
to	O
the	O
"	O
end	O
"	O
of	O
its	O
range	O
of	O
values	O
,	O
and	O
thus	O
a	O
smaller	O
"	O
wrapped	O
"	O
number	O
may	O
actually	O
be	O
considered	O
"	O
greater	O
"	O
than	O
the	O
first	O
sequence	B-Protocol
number	I-Protocol
.	O
</s>
<s>
there	O
are	O
sequence	B-Protocol
numbers	I-Protocol
for	O
which	O
comparison	O
is	O
undefined	O
.	O
</s>
<s>
Thus	O
,	O
it	O
is	O
often	O
difficult	O
or	O
impossible	O
to	O
avoid	O
all	O
"	O
undefined	O
"	O
comparisons	O
of	O
sequence	B-Protocol
numbers	I-Protocol
.	O
</s>
<s>
By	O
mapping	O
the	O
unsigned	O
sequence	B-Protocol
numbers	I-Protocol
onto	O
signed	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
arithmetic	I-General_Concept
operations	O
,	O
every	O
comparison	O
of	O
any	O
sequence	B-Protocol
number	I-Protocol
is	O
defined	O
,	O
and	O
the	O
comparison	O
operation	O
itself	O
is	O
dramatically	O
simplified	O
.	O
</s>
<s>
The	O
algorithm	O
specifies	O
that	O
,	O
for	O
N-bit	O
sequence	B-Protocol
numbers	I-Protocol
,	O
there	O
are	O
2N−1−1	O
values	O
considered	O
"	O
greater	O
than	O
"	O
and	O
2N−1−1	O
considered	O
"	O
less	O
than	O
"	O
.	O
</s>
<s>
Most	O
modern	O
hardware	O
implements	O
signed	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
binary	O
arithmetic	O
operations	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
16-bit	O
2	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
value	O
may	O
contain	O
numbers	O
ranging	O
from	O
to	O
.	O
</s>
<s>
So	O
,	O
if	O
we	O
simply	O
re-cast	O
sequence	B-Protocol
numbers	I-Protocol
as	O
2	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
integers	O
and	O
allow	O
there	O
to	O
be	O
one	O
more	O
sequence	B-Protocol
number	I-Protocol
considered	O
"	O
less	O
than	O
"	O
than	O
there	O
are	O
sequence	B-Protocol
numbers	I-Protocol
considered	O
"	O
greater	O
than	O
"	O
,	O
we	O
should	O
be	O
able	O
to	O
use	O
simple	O
signed	O
arithmetic	O
comparisons	O
instead	O
of	O
the	O
logically	O
incomplete	O
formula	O
proposed	O
by	O
the	O
RFC	O
.	O
</s>
<s>
Here	O
are	O
some	O
examples	O
(	O
in	O
16	O
bits	O
,	O
again	O
)	O
,	O
comparing	O
some	O
random	O
sequence	B-Protocol
numbers	I-Protocol
,	O
against	O
the	O
sequence	B-Protocol
number	I-Protocol
with	O
the	O
value	O
0	O
:	O
</s>
<s>
It	O
is	O
easy	O
to	O
see	O
that	O
the	O
signed	O
interpretation	O
of	O
the	O
sequence	B-Protocol
numbers	I-Protocol
are	O
in	O
the	O
correct	O
order	O
,	O
so	O
long	O
as	O
we	O
"	O
rotate	O
"	O
the	O
sequence	B-Protocol
number	I-Protocol
in	O
question	O
so	O
that	O
its	O
0	O
matches	O
up	O
with	O
the	O
sequence	B-Protocol
number	I-Protocol
we	O
are	O
comparing	O
it	O
against	O
.	O
</s>
<s>
It	O
turns	O
out	O
that	O
this	O
is	O
simply	O
done	O
using	O
an	O
unsigned	O
subtraction	O
and	O
simply	O
interpreting	O
the	O
result	O
as	O
a	O
signed	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
number	O
.	O
</s>
<s>
The	O
result	O
is	O
the	O
signed	O
"	O
distance	O
"	O
between	O
the	O
two	O
sequence	B-Protocol
numbers	I-Protocol
.	O
</s>
<s>
All	O
sequence	B-Protocol
number	I-Protocol
arithmetic	O
must	O
deal	O
with	O
"	O
wrapping	O
"	O
of	O
sequence	B-Protocol
numbers	I-Protocol
;	O
the	O
number	O
2N−1	O
is	O
equidistant	O
in	O
both	O
directions	O
,	O
in	O
sequence	B-Protocol
number	I-Protocol
terms	O
.	O
</s>
<s>
This	O
is	O
obviously	O
true	O
for	O
any	O
two	O
sequence	B-Protocol
numbers	I-Protocol
with	O
distance	O
of	O
0x8000	O
between	O
them	O
.	O
</s>
<s>
Furthermore	O
,	O
implementing	O
serial	B-Protocol
number	I-Protocol
arithmetic	I-Protocol
using	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
arithmetic	I-General_Concept
implies	O
serial	O
numbers	O
of	O
a	O
bit-length	O
matching	O
the	O
machine	O
's	O
integer	O
sizes	O
;	O
usually	O
16-bit	O
,	O
32-bit	O
and	O
64-bit	O
.	O
</s>
