Unary minus applied to unsigned int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Ward

    Unary minus applied to unsigned int

    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


  • Tõnu Aas

    #2
    Re: Unary minus applied to unsigned int

    > I tried to compile the following line:[color=blue]
    > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));[/color]

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

    Tõnu.


    Comment

    • Ron Natalie

      #3
      Re: Unary minus applied to unsigned int


      "Andrew Ward" <anwar@ihug.co. nz> wrote in message news:r1ucb.1583 30$JA5.3889031@ news.xtra.co.nz ...
      [color=blue]
      > But in my c++ book is says that the postfix L forces the integer to be
      > signed.[/color]
      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.



      Comment

      • Jerry Coffin

        #4
        Re: Unary minus applied to unsigned int

        In article <3f72b7c0$1@new s.infonet.ee>, tonu@ids.ee says...

        [ ... ]
        [color=blue]
        > 2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
        > Its some compiler bug, because -2147483648L is nice signed long constant.[/color]

        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.

        Comment

        • Mike Wahler

          #5
          Re: Unary minus applied to unsigned int


          "Tõnu Aas" <tonu@ids.ee> wrote in message news:3f72b7c0$1 @news.infonet.e e...[color=blue][color=green]
          > > I tried to compile the following line:
          > > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));[/color]
          >
          > 2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
          > Its some compiler bug, because -2147483648L is nice signed long constant.[/color]

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

          -Mike


          Comment

          • Jack Klein

            #6
            Re: Unary minus applied to unsigned int

            On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <anwar@ihug.co. nz>
            wrote in comp.lang.c++:
            [color=blue]
            > Hi All,
            > I tried to compile the following line:
            > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));[/color]

            Replace -2147483648L with (-2147483647 - 1)
            [color=blue]
            >
            > 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[/color]

            --
            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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

            Comment

            • Ron Natalie

              #7
              Re: Unary minus applied to unsigned int


              "Jack Klein" <jackklein@spam cop.net> wrote in message news:dmd7nv8ocd verf4a62d8n06tb 6lnn1q7hf@4ax.c om...[color=blue]
              > On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <anwar@ihug.co. nz>
              > wrote in comp.lang.c++:
              >[color=green]
              > > Hi All,
              > > I tried to compile the following line:
              > > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));[/color]
              >
              > Replace -2147483648L with (-2147483647 - 1)[/color]

              If he really wants the minimum long and maximum ulong variables
              he really should use numeric_limits min and max.


              Comment

              Working...