Why does left shift operation invoke Undefined Behaviour when theleft side operand has negative valu

Discussion in 'C++' started by Prasoon Saurav, Sep 24, 2010.

  1. In C bitwise left shift operation invokes Undefined Behaviour when the
    left side operand has negative value.

    Relevant quote from ISO C99 (6.5.7/4)

    "The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
    bits are ï¬lled with zeros. If E1 has an unsigned type, the value of
    the result is E1× 2E2, reduced modulo one more than the maximum value
    representable in the result type. If E1 has a signed type and
    nonnegative value, and E1× 2E2 is representable in the result type,
    then that is the resulting value; otherwise, the behavior is
    undeï¬ned."

    But in C++ the behaviour is well defined.

    ISO C++-03 (5.8/2)

    "The value of E1 << E2 is E1 (interpreted as a bit pattern) left-
    shifted E2 bit positions; vacated bits are zero-filled. If E1 has an
    unsigned type, the value of the result is E1 multiplied by the
    quantity 2 raised to the power E2, reduced modulo ULONG_MAX+1 if E1
    has type unsigned long, UINT_MAX+1 otherwise. [Note: the constants
    ULONG_MAXand UINT_MAXare defined in the header ). ]"

    That means

    int a = -1, b=2, c;
    c= a << b ;
    invokes Undefined Behaviour in C but the behaviour is well defined in C
    ++.

    What forced the ISO C++ committee to consider that behaviour well
    defined as opposed to the behaviour in C?

    On the other hand the behaviour is implementation defined for bitwise
    right shift operation when the left operand is negative, right?

    My question is why does left shift operation invoke Undefined
    Behaviour in C and why does right shift operator invoke just
    Implementation defined behaviour?
    Prasoon Saurav, Sep 24, 2010
    #1
    1. Advertising

  2. Prasoon Saurav

    SG Guest

    On 24 Sep., 17:55, Prasoon Saurav wrote:
    >
    > In C bitwise left shift operation invokes Undefined Behaviour when the
    > left side operand has negative value.
    > [...]
    > But in C++ the behaviour is well defined.


    If anyone feels like earning some stack overflow reputation points,
    this has been asked already by the same author on SO. I added my
    answer into the mix:

    <http://stackoverflow.com/questions/3784996/why-does-left-shift-
    operation-invoke-undefined-behaviour-when-the-left-side-opera>

    Cheers!
    SG
    SG, Sep 24, 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. Wenjie
    Replies:
    3
    Views:
    1,031
    Ron Samuel Klatchko
    Jul 11, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,807
    Smokey Grindel
    Dec 2, 2006
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,446
    CBFalconer
    Nov 30, 2006
  4. Sanny
    Replies:
    38
    Views:
    3,380
    Thomas Richter
    Apr 29, 2011
  5. pc
    Replies:
    2
    Views:
    1,307
    crisgoogle
    Jun 8, 2011
Loading...

Share This Page