1. interesting problem

can anyone tell me how this is possible ?

Code:
```#define LESS(x,y) ((x)-(y)<0)

int main()

{

unsigned int  first_t =0;
unsigned int secont_t=0;
int j=0;

first_t=~first_t ; // to make it all  ones
first_t=first_t - 1000; // now first_t is MAX_INT - 1000
second_t = first_t + 20 ; // now first_t < second_t
//first check
if (LESS(first_t, second_t))
printf(" first is less \n");

for (;j<5000;j++)
first_t++;
// at this point, first_t will have wrapped around ,
// so the value would be 3999 ( or 4000)

// second_t will have very high value 429496.......
// second will have 3999, since it wrapped around , the actual
// value should be  second_t+(5000-20)
if (LESS(first_t,second_t))
printf("1.... first is still less ....\n");
else
printf(".1..... second is more \n");

if (second_t > first_t)
printf(" 2......first is less \n");
else
printf(".2.......second is more \n");

return 0;
}```
the output would be

1 second is more
2 firsrt is less

why is this ??

}

2. Toss a couple printf's in.

3. What do you mean by toss a couple of printf's ?

The code is correct, i am courious as to how it is correct

4. Code:
```#include <stdio.h>

#define LESS(x,y) ((x)-(y)<0)

int main()
{
unsigned int  first_t =0;
unsigned int second_t=0;
int j=0;

first_t=~first_t ;
first_t=first_t - 1000;
second_t = first_t + 20 ;
printf("first_t = %u, second_t = %u\n", first_t, second_t);
if ( LESS(first_t, second_t) )
printf(" first is less \n");

for ( ;j<5000;j++ )
first_t++;
printf("first_t = %u, second_t = %u\n", first_t, second_t);
if ( LESS(first_t,second_t) )
printf("1.... first is still less ....\n");
else
printf(".1..... second is more \n");

printf("first_t = %u, second_t = %u\n", first_t, second_t);
if ( second_t > first_t )
printf(" 2......first is less \n");
else
printf(".2.......second is more \n");

return 0;
}

/* my output
first_t = 4294966295, second_t = 4294966315
first_t = 3999, second_t = 4294966315
.1..... second is more
first_t = 3999, second_t = 4294966315
2......first is less
*/```

5. True, that is the output, but.... shouldn't it both give the same result...

that is , "2....... first is less".

6. I'm sorry -- it's late for me. But if the first is not less, doesn't the second have to be more? More is not less.

7. Ok...
it's like this, at the first check
first_t = 3999 second_t = 4294..........

If I expand the macro

if ( ( 3999)-(4294.....) <0) <------ is true

so this should print , " 1.......first is still more .. "

but it will print the other..

8. When is an unsigned value less than zero?

9. The substraction won't be unsigned....just because the values are usigned...

10. There you go. Learn unsigned math.

11. You could always try working it out by hand using say just 4 bits.

12. ok, I missed out one thing....

#define LESS(x,y) ((long)(x) - (long)(y) <0)

so this should make it signed arithmatic... ??

13. Originally Posted by RoshanX
ok, I missed out one thing....

#define LESS(x,y) ((long)(x) - (long)(y) <0)

so this should make it signed arithmatic... ??
Signed integer overflow is undefined behavior. The value of an unsigned int might be out of the range of a long.

14. ok...this simplified code was taken from a linux kernel book....

i'll give the full code here..

Code:
```unsgined long timeout = jiffies + HZ/2;

/* do some work */

if (timeout > jiffies )
/* we did not timeout , good...*/
else
/* we have already timed out...error  */```
now, jiffies is a global vairable which keeps on increasing ... and ultimately wrap around.......
So, for example, once we set this timeout, and the jiffies value wraps around, it will start again from zero, which means, it won't timeout for a looong time, because the comparison , although jiffies is a larger value, is done against a warpped around samller jiffies value.

To get around this, it says they have a set of macros...
Code:
```#define time_before (unkonw, known) ((long)unknown- (long) known <0)

unsgined long timeout = jiffies+HZ/2;

/* .....*/

if (time_before(jiffies,timeout))
{
/* we did not time out, good......*/

}
else
/* we timed out */```
For some reason, the modifed code works, even if there is a wraparound.

15. Unsigned arithmetic is just easier, as well as being correct.
Code:
```#include <stdio.h>
#include <limits.h>

int main()
{
unsigned long then = ULONG_MAX - 50, now = then + 100, diff = now - then;
printf("diff = %lu\n", diff);
return 0;
}

/* my output
diff = 100
*/```