interesting problem

This is a discussion on interesting problem within the C Programming forums, part of the General Programming Boards category; can anyone tell me how this is possible ? Code: #define LESS(x,y) ((x)-(y)<0) int main() { unsigned int first_t =0; ...

  1. #1
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68

    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 ??







    }
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Toss a couple printf's in.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    What do you mean by toss a couple of printf's ?

    The code is correct, i am courious as to how it is correct
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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 
    */
    Last edited by Dave_Sinkula; 02-25-2006 at 11:46 PM. Reason: Added color.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    True, that is the output, but.... shouldn't it both give the same result...

    that is , "2....... first is less".
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    Last edited by Dave_Sinkula; 02-25-2006 at 11:55 PM. Reason: not!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    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..
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    When is an unsigned value less than zero?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    The substraction won't be unsigned....just because the values are usigned...
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    There you go. Learn unsigned math.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You could always try working it out by hand using say just 4 bits.

  12. #12
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    ok, I missed out one thing....

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

    so this should make it signed arithmatic... ??
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote 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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    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.
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 11:09 AM
  2. Interesting Problem with IOCP and AcceptEx() :: Winsock
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 10-07-2003, 09:16 PM
  3. Interesting number theory problem
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-20-2003, 07:45 AM
  4. Interesting virtual function problem.
    By Sebastiani in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2003, 10:08 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21