How to calculate Internet checksum?

starcorn picture starcorn · Oct 21, 2010 · Viewed 94.2k times · Source

I have a question regarding how the Internet checksum is calculated. I couldn't find any good explanation from the book, so I ask it here.

Have a look at the following example.

The following two messages are sent: 10101001 and 00111001. The checksum is calculated with 1's complement. So far I understand. But how is the sum calculated? At first I thought it maybe is XOR, but it seems not to be the case.

              10101001
              00111001
              --------
   Sum        11100010
   Checksum:  00011101

And then when they calculate if the message arrived OK. And once again how is the sum calculated?

               10101001
               00111001
               00011101
               --------
   Sum         11111111
   Complement  00000000  means that the pattern is O.K.

Answer

Wooble picture Wooble · Oct 21, 2010

It uses addition, hence the name "sum". 10101001 + 00111001 = 11100010.

For example:

+------------+-----+----+----+----+---+---+---+---+--------+
| bin value  | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | result |
+------------+-----+----+----+----+---+---+---+---+--------+
| value 1    |   1 |  0 |  1 |  0 | 1 | 0 | 0 | 1 |    169 |
| value 2    |   0 |  0 |  1 |  1 | 1 | 0 | 0 | 1 |     57 |
| sum/result |   1 |  1 |  1 |  0 | 0 | 0 | 1 | 0 |    226 |
+------------+-----+----+----+----+---+---+---+---+--------+