Re: Implicit Promotion of data types

Discussion in 'C++' started by Alf P. Steinbach, Feb 20, 2010.

  1. * Ruben Safir:
    > I'm reading both C++ and C material on implicit promotion of data types
    > when variables are operands of arithmetic or logic operators and in
    > neither language does the results from G++ (or GCC) seem to bare out the
    > documentation in either Lippman or from King (in C programming).
    >
    > They both say that signed ints will be promoted to unsigned long ints
    > (thereby possibly losing their sign) but I haven't seen this as a result
    > in test code.
    >
    > For example:
    >
    >
    >
    > #include <iostream>
    >
    > using namespace std;
    > int main(){
    >
    > int a = -500;
    >
    > long unsigned int b = 100000000;
    >
    > b = b + a;
    >
    > cout << "Should lose sign==>" << b << endl;
    >
    > return 0;
    > }
    >
    > g++ -Wall -o test.bin test.cc
    >
    > ./test.bin
    > Should lose sign==>99999500
    >
    >
    > Is there definitive rules for this behavior documented somewhere?


    The term "lose sign" is a bit too vague to describe what happens in the general
    case, such as above.

    In a promotion from signed value v to unsigned value u you have u = v + K*R
    where R is the range of the unsigned type (with n-bits unsigned you have R =
    2^n) and K is an integer, in practice 0 or 1.

    To investigate this you should focus exclusively on that conversion, and not
    bring in unsigned arithmetic as you do above (it just complicates things).


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Feb 20, 2010
    #1
    1. Advertising

  2. * Ruben Safir:
    > On Sat, 20 Feb 2010 07:32:52 +0100, Alf P. Steinbach wrote:
    >
    >> * Ruben Safir:
    >>> I'm reading both C++ and C material on implicit promotion of data types
    >>> when variables are operands of arithmetic or logic operators and in
    >>> neither language does the results from G++ (or GCC) seem to bare out
    >>> the documentation in either Lippman or from King (in C programming).
    >>>
    >>> They both say that signed ints will be promoted to unsigned long ints
    >>> (thereby possibly losing their sign) but I haven't seen this as a
    >>> result in test code.
    >>>
    >>> For example:
    >>>
    >>>
    >>>
    >>> #include <iostream>
    >>>
    >>> using namespace std;
    >>> int main(){
    >>>
    >>> int a = -500;
    >>>
    >>> long unsigned int b = 100000000;
    >>>
    >>> b = b + a;
    >>>
    >>> cout << "Should lose sign==>" << b << endl;
    >>>
    >>> return 0;
    >>> }
    >>>
    >>> g++ -Wall -o test.bin test.cc
    >>>
    >>> ./test.bin
    >>> Should lose sign==>99999500
    >>>
    >>>
    >>> Is there definitive rules for this behavior documented somewhere?

    >> The term "lose sign" is a bit too vague to describe what happens in the
    >> general case, such as above

    >
    > I understand that, but the lose of the sign and the obscure pop-up of
    > large numbers is the fastest symptom of a signed int becoming unsigned.
    >
    >> In a promotion from signed value v to unsigned value u you have u = v +
    >> K*R where R is the range of the unsigned type (with n-bits unsigned you
    >> have R = 2^n) and K is an integer, in practice 0 or 1.

    >
    > I'm not sure if I'm understanding this. R is the value of the size of a
    > signed values maximum?


    No, as you quoted, R is the range of the unsigned type, for n bits R = 2^n
    (where ^ denotes exponentiation). That's 1 plus the signed values maximum.


    > What is K?


    As you quoted, for a promotion K is 0 or 1.


    > But most importantly, when does this happen?


    As you quoted, one case where it happens is with a promotion from signed value v
    to unsigned value u. More generally it happens for any conversion from signed to
    unsigned integer type. But then K is not necessarily 0 or 1, but whatever's
    needed to get into the unsigned type's range 0 through 2^n-1 where n is the
    number of value representation bits in the unsigned type.

    For an example of promotion,

    unsigned const u = -5;

    gives u the value -5 + R where for 32-bit unsigned R = 2^32.


    > I can't confirm the trigger of a conversion as stated in
    > standard academic texts under the conditions of arithmatic binary
    > operations.


    Sorry, I'm unable to parse that.

    But it's easy enough to check.

    To understand it you'll want to visit Wikipedia's page on modular arithmetic,
    a.k.a. "clock arithmetic" (since it's the same system used on an analog clockface).


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Feb 21, 2010
    #2
    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. promotion of integer types

    , Mar 26, 2005, in forum: C Programming
    Replies:
    3
    Views:
    308
  2. Stef

    mixing types promotion etc..

    Stef, May 25, 2005, in forum: C Programming
    Replies:
    5
    Views:
    337
    Dave Thompson
    May 31, 2005
  3. Replies:
    1
    Views:
    549
    James Kanze
    Apr 12, 2009
  4. red floyd

    Re: implicit promotion

    red floyd, May 30, 2011, in forum: C++
    Replies:
    8
    Views:
    266
    Öö Tiib
    May 30, 2011
  5. Andrew Cooper

    Implicit integer promotion

    Andrew Cooper, Nov 29, 2013, in forum: C Programming
    Replies:
    18
    Views:
    198
    David Brown
    Dec 22, 2013
Loading...

Share This Page