unsigned long long + int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Ammon

    #1

    unsigned long long + int

    When I add an unsigned long long and an int, what type do each of the
    values get promoted to before the addition is performed? What is the
    type of the resulting expression? What occurs if the addition overflows
    or underflows?

    Thanks,

    -Peter
  • Mike Wahler

    #2
    Re: unsigned long long + int

    "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
    news:cjcna4$j1s $1@news.apple.c om...[color=blue]
    > When I add an unsigned long long and an int, what type do each of the
    > values get promoted to before the addition is performed?[/color]

    The 'unsigned long long' object is not promoted at all.
    The 'int' object is promoted to 'unsigned long long'.
    [color=blue]
    > What is the
    > type of the resulting expression?[/color]

    'unsigned long long'
    [color=blue]
    >What occurs if the addition overflows
    > or underflows?[/color]

    Undefined.

    -Mike


    Comment

    • Mike Wahler

      #3
      Re: (corr) unsigned long long + int


      "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
      news:WIm6d.1593 $Yr.1411@newsre ad3.news.pas.ea rthlink.net...[color=blue]
      > "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
      > news:cjcna4$j1s $1@news.apple.c om...[color=green]
      > > When I add an unsigned long long and an int, what type do each of the
      > > values get promoted to before the addition is performed?[/color]
      >
      > The 'unsigned long long' object is not promoted at all.
      > The 'int' object is promoted to 'unsigned long long'.
      >[color=green]
      > > What is the
      > > type of the resulting expression?[/color]
      >
      > 'unsigned long long'
      >[color=green]
      > >What occurs if the addition overflows
      > > or underflows?[/color]
      >
      > Undefined.[/color]

      Sorry, this is not correct. Upon overflow, unsigned integer
      types will 'wrap' to zero. The undefined behavior occurs with
      signed integer types.

      -Mike


      Comment

      • Peter Ammon

        #4
        Re: unsigned long long + int

        Mike Wahler wrote:[color=blue]
        > "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
        > news:cjcna4$j1s $1@news.apple.c om...
        >[color=green]
        >>When I add an unsigned long long and an int, what type do each of the
        >>values get promoted to before the addition is performed?[/color]
        >
        >
        > The 'unsigned long long' object is not promoted at all.
        > The 'int' object is promoted to 'unsigned long long'.[/color]

        Thanks! How does this promotion occur? Is it guaranteed to convert the
        value modulo ULLONG_MAX + 1 (like I hope), or is it an implementation
        defined thing?

        [...]

        -Peter

        --
        Pull out a splinter to reply.

        Comment

        • kal

          #5
          Re: unsigned long long + int

          Peter Ammon <peter_ammon@ro cketmail.com> wrote in message news:<cjcna4$j1 s$1@news.apple. com>...[color=blue]
          > When I add an unsigned long long and an int, what type do each of the
          > values get promoted to before the addition is performed? What is the
          > type of the resulting expression? What occurs if the addition overflows
          > or underflows?[/color]

          Try it on your machine/compiler and let us know.

          On of these days I am going to figure out what the following means.

          6.3.1.8 Usual arithmetic conversions

          1 Many operators that expect operands of arithmetic type cause
          conversions and yield result types in a similar way. The purpose
          is to determine a common real type for the operands and result.
          For the specified operands, each operand is converted, without
          change of type domain, to a type whose corresponding real type
          is the common real type. Unless explicitly stated otherwise, the
          common real type is also the corresponding real type of the result,
          whose type domain is the type domain of the operands if they are
          the same, and complex otherwise. This pattern is called the usual
          arithmetic conversions:

          First, if the corresponding real type of either operand is long
          double, the other operand is converted, without change of type
          domain, to a type whose corresponding real type is long double.

          Otherwise, if the corresponding real type of either operand is
          double, the other operand is converted, without change of type
          domain, to a type whose corresponding real type is double.

          Otherwise, if the corresponding real type of either operand is
          float, the other operand is converted, without change of type
          domain, to a type whose corresponding real type is float.

          Otherwise, the integer promotions are performed on both operands.
          Then the following rules are applied to the promoted operands:

          If both operands have the same type, then no further
          conversion is needed.

          Otherwise, if both operands have signed integer types or
          both have unsigned integer types, the operand with the type
          of lesser integer conversion rank is converted to the type
          of the operand with greater rank.

          Otherwise, if the operand that has unsigned integer type has
          rank greater or equal to the rank of the type of the other
          operand, then the operand with signed integer type is
          converted to the type of the operand with unsigned integer
          type.

          Otherwise, if the type of the operand with signed integer
          type can represent all of the values of the type of the
          operand with unsigned integer type, then the operand with
          unsigned integer type is converted to the type of the
          operand with signed integer type.

          Otherwise, both operands are converted to the unsigned
          integer type corresponding to the type of the operand
          with signed integer type.

          Comment

          • CBFalconer

            #6
            Re: unsigned long long + int

            Peter Ammon wrote:[color=blue]
            > Mike Wahler wrote:[color=green]
            >> "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
            >>[color=darkred]
            >>> When I add an unsigned long long and an int, what type do each
            >>> of the values get promoted to before the addition is performed?[/color]
            >>
            >> The 'unsigned long long' object is not promoted at all.
            >> The 'int' object is promoted to 'unsigned long long'.[/color]
            >
            > Thanks! How does this promotion occur? Is it guaranteed to
            > convert the value modulo ULLONG_MAX + 1 (like I hope), or is it
            > an implementation defined thing?[/color]

            Yes, guaranteed.

            --
            A: Because it fouls the order in which people normally read text.
            Q: Why is top-posting such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?

            Comment

            • S.Tobias

              #7
              Re: unsigned long long + int

              Mike Wahler <mkwahler@mkwah ler.net> wrote:[color=blue]
              > "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
              > news:cjcna4$j1s $1@news.apple.c om...[color=green]
              > > When I add an unsigned long long and an int, what type do each of the
              > > values get promoted to before the addition is performed?[/color][/color]
              [color=blue]
              > The 'unsigned long long' object is not promoted at all.
              > The 'int' object is promoted to 'unsigned long long'.[/color]
              ^^^^^^^^ -> converted

              --
              Stan Tobias
              sed 's/[A-Z]//g' to email

              Comment

              • kal

                #8
                Re: unsigned long long + int

                CBFalconer <cbfalconer@yah oo.com> wrote in message news:<415A4C7F. E71F989A@yahoo. com>...[color=blue]
                > Peter Ammon wrote:[/color]
                [color=blue][color=green]
                > > Mike Wahler wrote:[color=darkred]
                > >> "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
                > >>
                > >>> When I add an unsigned long long and an int, what type do each
                > >>> of the values get promoted to before the addition is performed?
                > >>
                > >> The 'unsigned long long' object is not promoted at all.
                > >> The 'int' object is promoted to 'unsigned long long'.[/color]
                > >
                > > Thanks! How does this promotion occur? Is it guaranteed to
                > > convert the value modulo ULLONG_MAX + 1 (like I hope), or is it
                > > an implementation defined thing?[/color]
                >
                > Yes, guaranteed.[/color]

                Seems so. But I still can't figure out what the standard says.

                Say, while converting from long to unsigned long where the value
                of long is negative, does this mean the value of unsigned long
                is calculated as ULONG_MAX + 1 + <value of long> ?

                6.3.1.3 Signed and unsigned integers

                2 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.

                Comment

                • CBFalconer

                  #9
                  Re: unsigned long long + int

                  kal wrote:[color=blue]
                  >
                  > CBFalconer <cbfalconer@yah oo.com> wrote in message news:<415A4C7F. E71F989A@yahoo. com>...[color=green]
                  > > Peter Ammon wrote:[/color]
                  >[color=green][color=darkred]
                  > > > Mike Wahler wrote:
                  > > >> "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
                  > > >>
                  > > >>> When I add an unsigned long long and an int, what type do each
                  > > >>> of the values get promoted to before the addition is performed?
                  > > >>
                  > > >> The 'unsigned long long' object is not promoted at all.
                  > > >> The 'int' object is promoted to 'unsigned long long'.
                  > > >
                  > > > Thanks! How does this promotion occur? Is it guaranteed to
                  > > > convert the value modulo ULLONG_MAX + 1 (like I hope), or is it
                  > > > an implementation defined thing?[/color]
                  > >
                  > > Yes, guaranteed.[/color]
                  >
                  > Seems so. But I still can't figure out what the standard says.
                  >
                  > Say, while converting from long to unsigned long where the value
                  > of long is negative, does this mean the value of unsigned long
                  > is calculated as ULONG_MAX + 1 + <value of long> ?
                  >
                  > 6.3.1.3 Signed and unsigned integers
                  >
                  > 2 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.[/color]

                  In most cases it means modulo arithmetic. The verbiage is
                  necessary to cater to the various allowable integral arithmetic
                  forms, 2s complement, 1s complement, sign-magnitude.

                  --
                  A: Because it fouls the order in which people normally read text.
                  Q: Why is top-posting such a bad thing?
                  A: Top-posting.
                  Q: What is the most annoying thing on usenet and in e-mail?


                  Comment

                  • Joe Wright

                    #10
                    Re: unsigned long long + int

                    S.Tobias wrote:
                    [color=blue]
                    > Mike Wahler <mkwahler@mkwah ler.net> wrote:
                    >[color=green]
                    >>"Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
                    >>news:cjcna4$j 1s$1@news.apple .com...
                    >>[color=darkred]
                    >>>When I add an unsigned long long and an int, what type do each of the
                    >>>values get promoted to before the addition is performed?[/color][/color]
                    >
                    >[color=green]
                    >>The 'unsigned long long' object is not promoted at all.
                    >>The 'int' object is promoted to 'unsigned long long'.[/color]
                    >
                    > ^^^^^^^^ -> converted
                    >[/color]

                    Objects are not converted, values are. Conversion to higher rank is
                    called promotion. Like in the Army. :=)
                    --
                    Joe Wright mailto:joewwrig ht@comcast.net
                    "Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---

                    Comment

                    • S.Tobias

                      #11
                      Re: unsigned long long + int

                      Joe Wright <joewwright@com cast.net> wrote:[color=blue]
                      > S.Tobias wrote:[color=green][color=darkred]
                      > >>The 'int' object is promoted to 'unsigned long long'.[/color]
                      > > ^^^^^^^^ -> converted[/color][/color]
                      [color=blue]
                      > Objects are not converted, values are. Conversion to higher rank is
                      > called promotion. Like in the Army. :=)[/color]

                      Are you sure (about integer promotions, not the Army :-) )?
                      I thought the word "promotions " was reserved only for _automatic_
                      conversions (rank_less_than _int)->(int); or (float)->(double)
                      in argument promotions.

                      --
                      Stan Tobias
                      sed 's/[A-Z]//g' to email

                      Comment

                      • Dan Pop

                        #12
                        Re: unsigned long long + int

                        In <Y4CdnSZpqOYVR8 DcRVn-sA@comcast.com> Joe Wright <joewwright@com cast.net> writes:
                        [color=blue]
                        >S.Tobias wrote:
                        >[color=green]
                        >> Mike Wahler <mkwahler@mkwah ler.net> wrote:
                        >>[color=darkred]
                        >>>"Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
                        >>>news:cjcna4$ j1s$1@news.appl e.com...
                        >>>
                        >>>>When I add an unsigned long long and an int, what type do each of the
                        >>>>values get promoted to before the addition is performed?[/color]
                        >>
                        >>[color=darkred]
                        >>>The 'unsigned long long' object is not promoted at all.
                        >>>The 'int' object is promoted to 'unsigned long long'.[/color]
                        >>
                        >> ^^^^^^^^ -> converted
                        >>[/color]
                        >
                        >Objects are not converted, values are.[/color]

                        Entirely gratuitous nit picking: the purpose of objects is to hold values.
                        [color=blue]
                        >Conversion to higher rank is called promotion.[/color]

                        Chapter and verse.
                        [color=blue]
                        >Like in the Army. :=)[/color]

                        In C, there are four strictly delimited categories of automatic
                        conversions: the integral promotions, the default argument promotions,
                        the usual arithmetic conversions and the assignment operator conversions.
                        To avoid misunderstandin g, use the word "promotion" only in one of the
                        two contexts where the standard uses it, even if the usual arithmetic
                        conversions are often army-like promotions...

                        Dan
                        --
                        Dan Pop
                        DESY Zeuthen, RZ group
                        Email: Dan.Pop@ifh.de
                        Currently looking for a job in the European Union

                        Comment

                        • Joe Wright

                          #13
                          Re: unsigned long long + int

                          Dan Pop wrote:[color=blue]
                          > In <Y4CdnSZpqOYVR8 DcRVn-sA@comcast.com> Joe Wright <joewwright@com cast.net> writes:
                          >
                          >[color=green]
                          >>S.Tobias wrote:
                          >>
                          >>[color=darkred]
                          >>>Mike Wahler <mkwahler@mkwah ler.net> wrote:
                          >>>
                          >>>
                          >>>>"Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
                          >>>>news:cjcna4 $j1s$1@news.app le.com...
                          >>>>
                          >>>>
                          >>>>>When I add an unsigned long long and an int, what type do each of the
                          >>>>>values get promoted to before the addition is performed?
                          >>>
                          >>>
                          >>>>The 'unsigned long long' object is not promoted at all.
                          >>>>The 'int' object is promoted to 'unsigned long long'.
                          >>>
                          >>> ^^^^^^^^ -> converted
                          >>>[/color]
                          >>
                          >>Objects are not converted, values are.[/color]
                          >
                          >
                          > Entirely gratuitous nit picking: the purpose of objects is to hold values.
                          >
                          >[color=green]
                          >>Conversion to higher rank is called promotion.[/color]
                          >
                          >
                          > Chapter and verse.
                          >
                          >[color=green]
                          >>Like in the Army. :=)[/color]
                          >
                          >
                          > In C, there are four strictly delimited categories of automatic
                          > conversions: the integral promotions, the default argument promotions,
                          > the usual arithmetic conversions and the assignment operator conversions.
                          > To avoid misunderstandin g, use the word "promotion" only in one of the
                          > two contexts where the standard uses it, even if the usual arithmetic
                          > conversions are often army-like promotions...
                          >
                          > Dan[/color]

                          What got my attention first was Stan's need to correct Mike's use of
                          'promoted' to 'converted'. That was gratuitous I think. Then, in
                          context I see Mike promoting objects. That is simply wrong and that
                          'the purpose of objects is to hold values' does not change the fact
                          that objects are not converted or promoted. Values are.

                          I do appreciate your advice. Thank you.
                          --
                          Joe Wright mailto:joewwrig ht@comcast.net
                          "Everything should be made as simple as possible, but not simpler."
                          --- Albert Einstein ---

                          Comment

                          Working...