How does unsigned subtraction work when it wraps around?

c

Solution

Take a simple 4 bit integer example where a = 5 and b = 6. The binary representation of each will be

a = 0101
b = 0110

Now when we subtract these (or take two's complement of b, sum with a, and add 1), we get the following

0101
1001
+  1
-----
1111

1111 is equal to 15 (unsigned) or -1 (signed, again translated using two's complement). By casting the two numbers to unsigned, we ensure that if b > a, the difference between the two is going to be a large unsigned number and have it's highest bit set. When translating this large unsigned number into its signed counterpart we will always get a negative number due to the set MSB.

As nos pointed out, when a sequence number wraps around from the max unsigned value back to the min, the macro will also return that the max value is < min using the above arithmetic, hence its usefulness.

Problem

This is a macro in the lwIP source code: ``` #define TCP_SEQ_LT(a,b) ((int32_t)((uint32_t)(a) - (uint32_t)(b)) < 0) ``` Which is used to check if a TCP sequence number is less than another, taking into account when the sequence numbers wrap around. It exploits the fact that arithmetic wraps around, but I am unable to understand how this works in this particular case. Can anyone explain what happens and why the above works ?

Original source