To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Serial number arithmetic

From Wikipedia, the free encyclopedia

Many protocols and algorithms require the serialization or enumeration of related entities. For example, a communication protocol must know whether some packet comes "before" or "after" some other packet. The IETF (Internet Engineering Task Force) RFC 1982 attempts to define "serial number arithmetic" for the purposes of manipulating and comparing these sequence numbers. In short, when the absolute serial number value decreases by more than half of the maximum value (e.g. 128 in an 8-bit value), it is considered to be "after" the former, whereas other decreases are considered to be "before".

This task is rather more complex than it might first appear, because most algorithms use fixed-size (binary) representations for sequence numbers. It is often important for the algorithm not to "break down" when the numbers become so large that they are incremented one last time and "wrap" around their maximum numeric ranges (go instantly from a large positive number to 0 or a large negative number). Some protocols choose to ignore these issues and simply use very large integers for their counters, in the hope that the program will be replaced (or they will retire) before the problem occurs (see Y2K).

Many communication protocols apply serial number arithmetic to packet sequence numbers in their implementation of a sliding window protocol. Some versions of TCP use  protection against wrapped sequence numbers (PAWS). PAWS applies the same serial number arithmetic to packet timestamps, using the timestamp as an extension of the high-order bits of the sequence number.[1]

YouTube Encyclopedic

  • 1/3
    Views:
    886 689
    154 492
    1 318
  • Sequences and Series (part 1)
  • Aptitude Made Easy - How to solve Number Series problems in seconds? Reasoning Math tricks
  • How to Decode Cisco Serial Numbers

Transcription

Let's learn a little bit about sequences and series. So what's a sequence? Well, a sequence is just a bunch of numbers in some order. You know, the most difficult sequence is 1, 2, 3, 4. You get the point. And what's a series? Well, it's often represented-- it's just a sum of sequences, a sum of a sequence. So, for example, the arithmetic sequence-- sorry the arithmetic series is just the sum of the arithmetic sequence. So 1 plus 2 plus 3 plus-- we could keep going until maybe some number. This is called the arithmetic series. Nothing too fancy here. But before we move forward, let's get a notation for how we can represent these sums without necessarily having to write out all of the digits or having to keep doing this dot, dot, dot, plus notation. And that notation is Sigma notation. That's an upper case Sigma. And how do you use Sigma notation? Well, let's say I wanted to represent this arithmetic series. So I would say, well, let's add up a bunch of-- let's call them k's. This is an arbitrary variable. And we'll start at k equals 1. We'll start at k equals 1, and we'll go to k is equal to big N. And this is the exact same thing, so we first make k equal to 1, and then we add it to k is equal to 2 plus 3, and we go all the way until N minus 1 and then plus N. So this is the Sigma notation for the arithmetic series. Before I move on, I think this is a good time just to like learn a little bit more about the arithmetic series. We'll actually focus on this one and the geometric series because those are the two that you'll see most often. And then once you learn calculus, I'll show you the power and Taylor series, which is exact-- the Taylor series is a specific version of a power series. But let's play around with this arith-- I keep wanting to say arith-MET-ic, but a-RITH-metic, either way-- series. So let's call the sum S. Let's say that this is equal to the sum from k is equal to 1 to N of k, which is equal to, just like we said, 1 plus 2 plus 3. And we'll just keep adding them, dot, dot, dot, to a bunch of numbers, to big N minus 1 plus big N, right? Fair enough. Now, bear with me a second. I'm just going to write that same exact sum again, but I'm just going to write it in reverse order. And I think it's intuitive to you that it doesn't matter what order I add up numbers in. They'll add up to the same number. 2 plus 1 is the same thing as 1 plus 2, right? So let me write this exact same sum, but I'll write it in reverse order. So that's the same thing as N plus N minus 1, plus N minus 2, plus-- and the pluses keep going-- plus 2 plus 1. This is the exact sum, just in the reverse order. And I did that for a reason because now I'm going to add both sides of this equation. I'm going to take-- S plus S. Well, that's just 2S. And that's going to equal this sum plus this sum. I wrote this so that the sum becomes clean. And why do I say that? Well, let's add up corresponding terms. We could have added up any terms, but-- so since they all have to add up, let's just add the 1 plus the N, then we'll add the 2 plus the N minus 1, then we'll add the 3 plus the N minus 2, and so forth. And I think you'll see in a second, or maybe you already realize why I'm doing this. One plus the N, the 2 plus the N minus 1, the 3 and the N minus 2, all the way to the N minus 1 and the 2, the N and the 1. What's 1 plus N? Well, that's just N plus 1, right? What's 2 plus N minus 1? Well, that's also N plus 1, right? What's 3 plus N minus 2? I think you could guess. It's N minus 1. And we just keep doing that. And what's N minus 2 plus 2? Sorry, this is a plus. N plus 1. And what's N plus 1? Well, that's just N plus 1, of course. So my question to you is how many of these N plus 1's are there? Well, there are N of them, right? Each N plus 1 corresponds to each of these terms, so there are N of these. So instead of just adding N plus 1 N times, we could say that this is just N times N plus 1. So we have 2 times the sum is equal to N times N plus 1, and we could divide both sides by 2, and we get the sum is equal to N times N plus 1 over 2. Now, why is this neat, or why is this cool at all? Well, first of all, we found out a way to sum this Sigma notation up. We got kind of a well-defined formula. And what makes this especially cool is you can use this for low-end parlor tricks. What do I mean by that? Well, you can go up to someone and you can say, well, how quickly do you think I can add up the numbers between 1 and-- what am I doing-- oh, between 1 and 100? And, you know, people will say, oh, it will take you a little time: 1 plus 2 plus 3. And you say, well, it takes me no time at all because this is what I can do. So the sum-- and I just want to show you that you can use different variables from B equals 1, we're taking the variable B, to 100, right? That's the sum from 1 to 100. And we figured out what that formula is. It's going to be 100 times 101 over 2. Well, what's 100 times 101? It's just going to be 101 with two zeroes, right? 10,100 over 2, and that equals 5,050. That's pretty neat. Instead of having to say 1 plus 2 plus 3 plus blah, blah, blah, blah, blah, blah, blah, blah, plus 98 plus 99 plus 100, this would take you some time, and there's a very good chance you would make a careless mistake. We could just plug into this formula, which we proved and hopefully you understood, and say that equals 5,050. You could do even something more impressive: the sum from 1 to 1,000. What's the sum from 1 to 1,000? Well, our formula, remember, was N times N plus 1 over 2. So if N is equal to 1,000, then what's our sum? It's 1,000 times 1,001 over 2, which is equal to-- well, we'll just add three zeroes to this: 1,001, one, two, three. Sorry, I think that was my first burp ever on one of these videos. I should re-record it, but I'm going to move forward. That kind of disconcerted me a little bit. I'd eaten too much. Anyway, divided by 2, and what is that? Let's see, it'll be 500-- let's see, this is a million. Half of a million is 500,000. 500,500. And that would have taken you forever to do manually. But based on this formula we just got, you know how to do it very, very quickly. So that's the arithmetic series. But let's do another one. This is another typical series that you might see. Actually, this one you'll see a lot in your life, especially if you go into finance or really a whole series of scientific-- this shows up a lot, and this is called the geometric series. And the geometric series is-- essentially you take x. And I'll do it generally where I just take a variable x, and I say-- well, no, no. Let me just not take an x. Let me just take some number. So let's say some number a to the k from-- I don't know. Let's say from k is equal to 0 to k is equal to N. What does that mean? Well, that means a to the 0, right, k is 0, plus a 1 plus a squared plus a to the third plus-- and you could keep going-- plus a to the N minus 1 plus a to the N minus 2. This is called the geometric series. And it might not be obvious to you, but this type of growth, where you keep increasing the exponent, this is called geometric growth. So how do you take the sum of this? Well, let's see if we can do a similar trick, although this trick will involve one more step. So let's call the sum S. Let's call it the sum from k equals 0 to N, a to the k. And that, of course, is equal to what I just wrote. I probably didn't have to do it like this. a squared plus bup, bup, bup, bup, plus a to the N minus 1, plus a to the N minus 2. Now let's define another sum, and I'm going to call that aS. Actually, I'm about to run out of time, so I'll continue this in the next video.

Operations on sequence numbers

Only addition of a small positive integer to a sequence number and comparison of two sequence numbers are discussed. Only unsigned binary implementations are discussed, with an arbitrary size in bits noted throughout the RFC (and below) as "SERIAL_BITS".

Addition

Adding an integer to a sequence number is simple unsigned integer addition, followed by unsigned modulo operation to bring the result back into range (usually implicit in the unsigned addition, on most architectures):

s' = (s + n) modulo 2SERIAL_BITS

Addition of a value below 0 or above 2SERIAL_BITS−1 − 1 is undefined. Basically, adding values beyond this range will cause the resultant sequence number to "wrap", and (often) result in a number that is considered "less than" the original sequence number.

Comparison

A means of comparing two sequence numbers i1 and i2 (the unsigned integer representations of sequence numbers s1 and s2) is presented.

Equality is defined as simple numeric equality.

The algorithm presented for comparison is complex, having to take into account whether the first sequence number is close to the "end" of its range of values, and thus a smaller "wrapped" number may actually be considered "greater" than the first sequence number. Thus i1 is considered less than i2 only if

(i1 < i2 and i2i1 < 2SERIAL_BITS−1) or
(i1 > i2 and i1i2 > 2SERIAL_BITS−1)

Shortfalls

The algorithms presented by the RFC have at least one significant shortcoming: there are sequence numbers for which comparison is undefined. Since many algorithms are implemented independently by multiple independent cooperating parties, it is often impossible to prevent all such situations from occurring.

The authors of RFC 1982 acknowledge this without offering a general solution:

While it would be possible to define the test in such a way that the inequality would not have this surprising property, while being defined for all pairs of values, such a definition would be unnecessarily burdensome to implement, and difficult to understand, and would still allow cases where

s1 < s2 and (s1 + 1) > (s2 + 1)

which is just as non-intuitive.

Thus the problem case is left undefined, implementations are free to return either result, or to flag an error, and users must take care not to depend on any particular outcome. Usually this will mean avoiding allowing those particular pairs of numbers to co-exist.

Thus, it is often difficult or impossible to avoid all "undefined" comparisons of sequence numbers. However, a relatively simple solution is available. By mapping the unsigned sequence numbers onto signed two's complement arithmetic operations, every comparison of any sequence number is defined, and the comparison operation itself is dramatically simplified. All comparisons specified by the RFC retain their original truth values; only the formerly "undefined" comparisons are affected.

General solution

The RFC 1982 algorithm specifies that, for N-bit sequence numbers, there are 2N−1 − 1 values considered "greater than" and 2N−1 − 1 considered "less than". Comparison against the remaining value (exactly 2N−1-distant) is deemed to be "undefined".

Most modern hardware implements signed two's complement binary arithmetic operations. These operations are fully defined for the entire range of values for any operands they are given, since any N-bit binary number can contain 2N distinct values, and since one of them is taken up by the value 0, there are an odd number of spots left for all the non-zero positive and negative numbers. There is simply one more negative number representable than there are positive. For example, a 16-bit 2's complement value may contain numbers ranging from −32768 to +32767.

So, if we simply re-cast sequence numbers as 2's complement integers and allow there to be one more sequence number considered "less than" than there are sequence numbers considered "greater than", we should be able to use simple signed arithmetic comparisons instead of the logically incomplete formula proposed by the RFC.

Here are some examples (in 16 bits, again), comparing some random sequence numbers, against the sequence number with the value 0:

unsigned    binary    signed
sequence    value     distance
--------    ------    --------
   32767 == 0x7FFF ==  32767
       1 == 0x0001 ==      1
       0 == 0x0000 ==      0
   65535 == 0xFFFF ==     −1 
   65534 == 0xFFFE ==     −2
   32768 == 0x8000 == −32768

It is easy to see that the signed interpretation of the sequence numbers are in the correct order, so long as we "rotate" the sequence number in question so that its 0 matches up with the sequence number we are comparing it against. It turns out that this is simply done using an unsigned subtraction and simply interpreting the result as a signed two's complement number. The result is the signed "distance" between the two sequence numbers. Once again, if i1 and i2 are the unsigned binary representations of the sequence numbers s1 and s2, the distance from s1 to s2 is

distance = (signed)(i1 - i2)

If distance is 0, the numbers are equal. If it is < 0, then s1 is "less than" or "before" s2. Simple, clean and efficient, and fully defined. However, not without surprises.

All sequence number arithmetic must deal with "wrapping" of sequence numbers; the number 2N−1 is equidistant in both directions, in RFC 1982 sequence number terms. In our math, they are both considered to be "less than" each other:

distance1 = (signed)(0x8000 - 0x0)    == (signed)0x8000 == -32768 < 0
distance2 = (signed)(0x0    - 0x8000) == (signed)0x8000 == -32768 < 0

This is obviously true for any two sequence numbers with distance of 0x8000 between them.

Furthermore, implementing serial number arithmetic using two's complement arithmetic implies serial numbers of a bit-length matching the machine's integer sizes; usually 16-bit, 32-bit and 64-bit. Implementing 20-bit serial numbers needs shifts (assuming 32-bit ints):

distance = (signed)((i1 << 12) - (i2 << 12))

See also

References

  1. ^ RFC 1323: "TCP Extensions for High Performance", section 4.2.

External links

This page was last edited on 8 March 2024, at 22:28
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.