unsigned int with signed long int (same width) - how is conversion done?

Discussion in 'C Programming' started by G Fernandes, Feb 16, 2005.

  1. G Fernandes

    G Fernandes Guest

    Hello,

    I have a question about using an unsigned int and a signed long int in
    an expression (where UINT_MAX > LINT_MAX).

    Based on the conversion rules, the unsigned int will be convered to
    long int, but what if it can't fit? Is this undefined behaviour?

    Any help will be appreciated.

    [I guess the same thing can happen with unsigned long and signed long
    long]
     
    G Fernandes, Feb 16, 2005
    #1
    1. Advertising

  2. G Fernandes wrote:

    > I have a question about using an unsigned int and a signed long int in
    > an expression (where UINT_MAX > LINT_MAX).
    >
    > Based on the conversion rules, the unsigned int will be convered to
    > long int


    No, it won't. If signed long can't hold all possible values for unsigned
    int, both operands are converted to unsigned long. This is part of the
    "usual arithmetic conversions", which are explained in great detail in the
    standard.


    Christian
     
    Christian Kandeler, Feb 16, 2005
    #2
    1. Advertising

  3. On Tue, 15 Feb 2005 21:25:25 -0800, G Fernandes wrote:

    > Hello,
    >
    > I have a question about using an unsigned int and a signed long int in
    > an expression (where UINT_MAX > LINT_MAX).
    >
    > Based on the conversion rules, the unsigned int will be convered to
    > long int, but what if it can't fit? Is this undefined behaviour?


    In that case both are converted to unsigned long.

    > Any help will be appreciated.
    >
    > [I guess the same thing can happen with unsigned long and signed long
    > long]


    That's the normal case where the signed value is converted to the
    unsigned type.

    Any integer value can be converted to an unsigned type with a well defined
    result. That may not preserve the value, and obviously won't if the
    initial value is negative. When the value of an integer conversion can't
    be represented in the new type the standard says:

    "Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or
    subtracting one more than the maximum value that can be represented in
    the new type until the value is in the range of the new type."

    So, for example, it follows that if you convert the value -1 to any
    unsigned type the result is the maximum value that the unsigned type can
    represent.

    Lawrence





    Lawrence
     
    Lawrence Kirby, Feb 16, 2005
    #3
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    686
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,201
    Peter Shaggy Haywood
    Sep 20, 2005
  3. CFAN
    Replies:
    6
    Views:
    828
    Tor Rustad
    Apr 4, 2007
  4. pereges

    Promoting unsigned long int to long int

    pereges, Jun 30, 2008, in forum: C Programming
    Replies:
    112
    Views:
    2,091
    David Thompson
    Jul 28, 2008
  5. pozz
    Replies:
    12
    Views:
    747
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page