operators and type promotion

Discussion in 'C Programming' started by swansnow, Dec 12, 2007.

  1. swansnow

    swansnow Guest

    This is perhaps a basic question, but it's been a while since I've
    done C, and none of the searches I did on operators and data types
    wanted to address this, so here goes...

    In the glibc (linux) library, I found this expression , and I want to
    know exactly what it's doing.

    res >= 0xfffff001u

    Here, res is an int. It looks to me like res is being interpreted as
    an unsigned int (long?) and then being compared against the constant
    (-4095). So if res happens to be, say, -22, this expression will be
    true. If res happens to be -4096, this statement will be false, maybe?

    Is that right?

    :)

    -Corinna
     
    swansnow, Dec 12, 2007
    #1
    1. Advertising

  2. swansnow

    James Kuyper Guest

    swansnow wrote:
    > This is perhaps a basic question, but it's been a while since I've
    > done C, and none of the searches I did on operators and data types
    > wanted to address this, so here goes...
    >
    > In the glibc (linux) library, I found this expression , and I want to
    > know exactly what it's doing.
    >
    > res >= 0xfffff001u
    >
    > Here, res is an int. It looks to me like res is being interpreted as
    > an unsigned int (long?) and then being compared against the constant
    > (-4095). So if res happens to be, say, -22, this expression will be
    > true. If res happens to be -4096, this statement will be false, maybe?
    >
    > Is that right?


    It depends very much upon the value of INT_MAX and UINT_MAX. If INT_MAX
    > 0xfffff001u, then the constant will be promoted to an int before

    performing the comparison. As a result, the comparison will be done as a
    signed comparison, and only very large positive values of res will pass
    the test. If, on the other hand, INT_MAX < 0xfffff001u, then res will be
    converted to the same type as the constant (which will be either
    unsigned int or unsigned long, depending upon whether or not UINT_MAX >
    0xfffff01u). The exact result you get will depend upon the results of
    that conversion.
     
    James Kuyper, Dec 12, 2007
    #2
    1. Advertising

  3. James Kuyper <> wrote:
    > swansnow wrote:
    > > In the glibc (linux) library, I found this expression ,
    > > and I want to know exactly what it's doing.
    > >
    > > res >= 0xfffff001u
    > >
    > > Here, res is an int. It looks to me like res is being
    > > interpreted as an unsigned int (long?)


    If 0xfffff001u <= UINT_MAX, it's equivalent to...

    (unsigned) res >= 0xfffff001u

    Otherwise it's equivalent to...

    (unsigned long) res >= 0xfffff001u

    > > and then being compared against the constant (-4095).
    > > So if res happens to be, say, -22, this expression will
    > > be true. If res happens to be -4096, this statement
    > > will be false, maybe?
    > >
    > > Is that right?

    >
    > It depends very much upon the value of INT_MAX and
    > UINT_MAX.


    I think you've misremembered.

    > If INT_MAX > 0xfffff001u, then the constant will be
    > promoted to an int before performing the comparison.


    Not according to 6.4.4.1p5. The u suffix means it is
    an unsigned integer; at least an unsigned int, but
    possibly a wider (higher rank). It's certainly not a
    lower rank than unsigned int though. E.g. (1 > -1u)
    is never true.

    --
    Peter
     
    Peter Nilsson, Dec 12, 2007
    #3
  4. swansnow

    James Kuyper Guest

    Peter Nilsson wrote:
    ....
    > I think you've misremembered.


    Yes, you're right. I actually thought to myself "I'd better double check
    before I post", and then decided not to. :-(
     
    James Kuyper, Dec 12, 2007
    #4
  5. swansnow

    swansnow Guest

    Thanks for the clarification. I was starting to worry... :)

    On Dec 11, 6:24 pm, Peter Nilsson <> wrote:
    > If 0xfffff001u <= UINT_MAX, it's equivalent to...
    >
    > (unsigned) res >= 0xfffff001u
    >
    > Otherwise it's equivalent to...
    >
    > (unsigned long) res >= 0xfffff001u
    >
     
    swansnow, Dec 12, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BigMan

    Type promotion rules?

    BigMan, Jun 12, 2005, in forum: C++
    Replies:
    7
    Views:
    7,011
    BigMan
    Jun 14, 2005
  2. KOFKS
    Replies:
    2
    Views:
    279
    KOFKS
    Jun 6, 2006
  3. KOFKS
    Replies:
    6
    Views:
    282
    Keith Thompson
    Jun 7, 2006
  4. Replies:
    1
    Views:
    556
    christian.bau
    Jan 15, 2007
  5. Replies:
    1
    Views:
    565
    James Kanze
    Apr 12, 2009
Loading...

Share This Page