Unary minus applied to unsigned int

Discussion in 'C++' started by Andrew Ward, Sep 25, 2003.

  1. Andrew Ward

    Andrew Ward Guest

    Hi All,
    I tried to compile the following line:
    pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));

    but get this error:
    unary minus applied to unsigned type, result still unsigned.

    But in my c++ book is says that the postfix L forces the integer to be
    signed.

    Could anyone please explain this behaviour, and if possible point me to some
    documentation that explains how intergral types are handled in depth?

    Andy
    Andrew Ward, Sep 25, 2003
    #1
    1. Advertising

  2. Andrew Ward

    Tõnu Aas Guest

    > I tried to compile the following line:
    > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));


    2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
    Its some compiler bug, because -2147483648L is nice signed long constant.

    Tõnu.
    Tõnu Aas, Sep 25, 2003
    #2
    1. Advertising

  3. Andrew Ward

    Ron Natalie Guest

    "Andrew Ward" <> wrote in message news:r1ucb.158330$...

    > But in my c++ book is says that the postfix L forces the integer to be
    > signed.

    The number is too big for (signed) long so it becomes unsigned long.
    There are no negative literals, just positive ones with a unary minus
    applied to it. There is no way to represent that number with a literal
    I expect on your environment.

    However, you can use numeric_limits<long>::min() to get the value.
    Probably a better idea than hardcoding those number in anyhow.
    Ron Natalie, Sep 25, 2003
    #3
  4. Andrew Ward

    Jerry Coffin Guest

    In article <3f72b7c0$>, says...

    [ ... ]

    > 2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
    > Its some compiler bug, because -2147483648L is nice signed long constant.


    Unfortunately this isn't correct. The problem is that this is NOT
    parsed as simply a negative constant. It's parsed as a unary minus
    operator, followed by a positive constant. Unfortunately, on a typical
    32-bit system, 2147483648L is too large to be a signed long constant, so
    it's treated as an unsigned long constant. The unary minus is then
    applied to that unsigned long, and about the only (sort of) good result
    is a warning message telling you that you haven't gotten what you
    probably wanted.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 25, 2003
    #4
  5. Andrew Ward

    Mike Wahler Guest

    "Tõnu Aas" <> wrote in message news:3f72b7c0$...
    > > I tried to compile the following line:
    > > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));

    >
    > 2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
    > Its some compiler bug, because -2147483648L is nice signed long constant.


    That value is not required to be in the range of
    signed long. A value one greater is, however.

    -Mike
    Mike Wahler, Sep 25, 2003
    #5
  6. Andrew Ward

    Jack Klein Guest

    On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <>
    wrote in comp.lang.c++:

    > Hi All,
    > I tried to compile the following line:
    > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));


    Replace -2147483648L with (-2147483647 - 1)

    >
    > but get this error:
    > unary minus applied to unsigned type, result still unsigned.
    >
    > But in my c++ book is says that the postfix L forces the integer to be
    > signed.
    >
    > Could anyone please explain this behaviour, and if possible point me to some
    > documentation that explains how intergral types are handled in depth?
    >
    > Andy


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Sep 26, 2003
    #6
  7. Andrew Ward

    Ron Natalie Guest

    "Jack Klein" <> wrote in message news:...
    > On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <>
    > wrote in comp.lang.c++:
    >
    > > Hi All,
    > > I tried to compile the following line:
    > > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));

    >
    > Replace -2147483648L with (-2147483647 - 1)


    If he really wants the minimum long and maximum ulong variables
    he really should use numeric_limits min and max.
    Ron Natalie, Sep 26, 2003
    #7
    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. Marc

    Semantics of unary minus

    Marc, Feb 12, 2006, in forum: C Programming
    Replies:
    13
    Views:
    483
  2. Matthew  Cook
    Replies:
    6
    Views:
    493
  3. pozz
    Replies:
    12
    Views:
    720
    Tim Rentsch
    Mar 20, 2011
  4. Sam Stephenson
    Replies:
    9
    Views:
    222
    Caleb Clausen
    Nov 7, 2005
  5. Todd Burch
    Replies:
    3
    Views:
    139
    Jonas Roberto de Goes Filho (sysdebug)
    Aug 27, 2007
Loading...

Share This Page