Simple questions on shift ops and promotions

Discussion in 'C++' started by Glen Able, Jan 27, 2004.

  1. Glen Able

    Glen Able Guest

    Just to get my head straight on this...

    Firstly, am I right in thinking that right-shifting a signed integer has an
    undefined result (i.e. could be implemented as a logical or arithmetic
    shift)?

    If so, am I also right in assuming that if I right-shift a signed integer by
    an unsigned integer, the signed value will be promoted to unsigned and the
    shift will be performed as a logical right shift?

    Finally, if I specify an int literal, am I right in thinking that it's
    treated the same way as any other signed integer, and will be promoted to
    unsigned in the same way?

    thanks in advance,
    glen.
     
    Glen Able, Jan 27, 2004
    #1
    1. Advertising

  2. Glen Able

    Attila Feher Guest

    Glen Able wrote:
    > Just to get my head straight on this...
    >
    > Firstly, am I right in thinking that right-shifting a signed integer
    > has an undefined result (i.e. could be implemented as a logical or
    > arithmetic shift)?


    "If E1 has a signed type and a negative value, the resulting value is
    implementation-defined."

    This says: nope.

    > If so, am I also right in assuming that if I right-shift a signed
    > integer by an unsigned integer, the signed value will be promoted to
    > unsigned and the shift will be performed as a logical right shift?


    As above: it is implementation defined. The docs of your compiler should
    tell it.

    > Finally, if I specify an int literal, am I right in thinking that it's
    > treated the same way as any other signed integer, and will be
    > promoted to unsigned in the same way?


    The above are exrepts from the standard, talking about right shift operator.
    I believe that they answer your questions. As I see no promotions are done
    from signed to unsigned. IIRC that is not done just anywhere, only if the
    required type is unsigned - which seems not to be true in this case. I
    might be wrong, but I think you need that u to make your literal unsigned.

    --
    Attila aka WW
     
    Attila Feher, Jan 27, 2004
    #2
    1. Advertising

  3. Glen Able

    Jack Klein Guest

    On Tue, 27 Jan 2004 15:31:38 +0200, "Attila Feher"
    <> wrote in comp.lang.c++:

    > Glen Able wrote:
    > > Just to get my head straight on this...
    > >
    > > Firstly, am I right in thinking that right-shifting a signed integer
    > > has an undefined result (i.e. could be implemented as a logical or
    > > arithmetic shift)?

    >
    > "If E1 has a signed type and a negative value, the resulting value is
    > implementation-defined."
    >
    > This says: nope.
    >
    > > If so, am I also right in assuming that if I right-shift a signed
    > > integer by an unsigned integer, the signed value will be promoted to
    > > unsigned and the shift will be performed as a logical right shift?

    >
    > As above: it is implementation defined. The docs of your compiler should
    > tell it.


    No, you are incorrect here. The type of the result is the type of the
    promoted left operand. The type of the right operand (the shift
    count) does not enter into the promotion of the left operand (the
    value shifted).

    > > Finally, if I specify an int literal, am I right in thinking that it's
    > > treated the same way as any other signed integer, and will be
    > > promoted to unsigned in the same way?

    >
    > The above are exrepts from the standard, talking about right shift operator.
    > I believe that they answer your questions. As I see no promotions are done
    > from signed to unsigned. IIRC that is not done just anywhere, only if the
    > required type is unsigned - which seems not to be true in this case. I
    > might be wrong, but I think you need that u to make your literal unsigned.


    --
    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++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 28, 2004
    #3
  4. Glen Able

    Jack Klein Guest

    On Tue, 27 Jan 2004 13:21:59 -0000, "Glen Able"
    <> wrote in comp.lang.c++:

    > Just to get my head straight on this...
    >
    > Firstly, am I right in thinking that right-shifting a signed integer has an
    > undefined result (i.e. could be implemented as a logical or arithmetic
    > shift)?


    The result of right shifting a signed integer type with a negative
    value is implementation-defined, as Attila, said, unless it generates
    an invalid value. If it does not generate an invalid value, it is
    implementation-defined whether the sign bit is retained as a 1 or is
    set to 0. If it does generate an invalid value, then you generate
    undefined behavior.

    > If so, am I also right in assuming that if I right-shift a signed integer by
    > an unsigned integer, the signed value will be promoted to unsigned and the
    > shift will be performed as a logical right shift?


    No, any promotion of the left hand operand (value shifted) is
    completely unaffected by the type of the right hand operand (shift
    count). Specifically shifting a value of any signed integer type by
    an unsigned shift count does NOT convert the value shifted to
    unsigned. If the signed type has lesser rank than int (signed char,
    signed short, bool), it will be promoted to signed int, not unsigned
    int.

    > Finally, if I specify an int literal, am I right in thinking that it's
    > treated the same way as any other signed integer, and will be promoted to
    > unsigned in the same way?


    You are right in thinking that it will be treated the same way as any
    other signed integer, but that specifically means that it will NOT be
    converted to unsigned just because the second operand (shift count) is
    unsigned. If you want to shift an unsigned integer literal, append
    'U'.

    > thanks in advance,
    > glen.


    --
    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++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 28, 2004
    #4
  5. Glen Able

    Attila Feher Guest

    Jack Klein wrote:
    > On Tue, 27 Jan 2004 15:31:38 +0200, "Attila Feher"
    > <> wrote in comp.lang.c++:
    >
    >> Glen Able wrote:
    >>> Just to get my head straight on this...
    >>>
    >>> Firstly, am I right in thinking that right-shifting a signed integer
    >>> has an undefined result (i.e. could be implemented as a logical or
    >>> arithmetic shift)?

    >>
    >> "If E1 has a signed type and a negative value, the resulting value is
    >> implementation-defined."
    >>
    >> This says: nope.
    >>
    >>> If so, am I also right in assuming that if I right-shift a signed
    >>> integer by an unsigned integer, the signed value will be promoted to
    >>> unsigned and the shift will be performed as a logical right shift?

    >>
    >> As above: it is implementation defined. The docs of your compiler
    >> should tell it.

    >
    > No, you are incorrect here. The type of the result is the type of the
    > promoted left operand. The type of the right operand (the shift
    > count) does not enter into the promotion of the left operand (the
    > value shifted).


    Read 5.8/1

    "The operands shall be of integral or enumeration type and integral
    promotions are performed. The type of the result is that of the promoted
    left operand. The behavior is undefined if the right operand is negative, or
    greater than or equal to the length in bits of the promoted left operand."

    Read: "promoted left operand".

    1.) The left operand is promoted with the usual integer promotions.

    2.) AFAIK that cannot result in it being promoted to unsigned

    3.) I did not tell the type of the shift-count will trigger promotion

    4.) I did note that if the signed value is negative one should read the docs

    I have however made the mistake that by "signed integer" I have assumed a
    negative value there.

    --
    Attila aka WW
     
    Attila Feher, Jan 28, 2004
    #5
    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. Roberto Gallo

    Shift - byte[] buf shift

    Roberto Gallo, Jan 27, 2004, in forum: Java
    Replies:
    3
    Views:
    2,191
    Thomas Schodt
    Jan 27, 2004
  2. Wenjie
    Replies:
    3
    Views:
    1,070
    Ron Samuel Klatchko
    Jul 11, 2003
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,494
    CBFalconer
    Nov 30, 2006
  4. Sanny
    Replies:
    38
    Views:
    3,528
    Thomas Richter
    Apr 29, 2011
  5. devphylosoff

    what "shift" does, if not "$_ = shift;" ?

    devphylosoff, Nov 29, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    371
    Michele Dondi
    Dec 4, 2007
Loading...

Share This Page