6.5.7.4 Bitwise shift operators

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

    6.5.7.4 Bitwise shift operators

    I have a question about the following:

    4. The result of E1 << E2 is E1 left-shifted E2 bit positions: vacated
    bits are filled with zeros. ... If E1 has a signed type and a
    nonnegative value, and E1x2^32 is representable in the result type,
    then that is the resulting value; otherwise, the behavior is undefined.

    I am a bit confused by this last clause- "otherwise, the behavior is
    undefined." Does this mean I should not left shit a signed negative
    value because undefined behavior will result? Or is the standard just
    saying that nothing can be said about the value of the result of left
    shifting a negative value? Am I at least guaranteed that the binary
    representation of the signed negative integer will just be shifted left
    'E2' bits?

    Thanks.

  • Eric Sosman

    #2
    Re: 6.5.7.4 Bitwise shift operators

    joshc wrote:[color=blue]
    > I have a question about the following:
    >
    > 4. The result of E1 << E2 is E1 left-shifted E2 bit positions: vacated
    > bits are filled with zeros. ... If E1 has a signed type and a
    > nonnegative value, and E1x2^32 is representable in the result type,[/color]

    That's "E2," not "32."
    [color=blue]
    > then that is the resulting value; otherwise, the behavior is undefined.
    >
    > I am a bit confused by this last clause- "otherwise, the behavior is
    > undefined." Does this mean I should not left shit a signed negative
    > value because undefined behavior will result?[/color]

    Yes, "the behavior is undefined" means "the behavior is
    undefined." Also, the typo is particularly apt ...
    [color=blue]
    > Or is the standard just
    > saying that nothing can be said about the value of the result of left
    > shifting a negative value? Am I at least guaranteed that the binary
    > representation of the signed negative integer will just be shifted left
    > 'E2' bits?[/color]

    "The behavior is undefined" means that the Standard says
    nothing about what may happen. It does not describe what value
    may be produced. It does not even specify that *any* value will
    be produced; the program may trap on some kind of overflow error,
    or raise a signal and handle it in McGooley's Pub (where, after
    sufficient drafts or draughts other sorts of overflow may occur).

    "Undefined behavior" is often misunderstood as "catastroph ic
    behavior," but this is incorrect. The Standard has been described
    as a sort of contract between the programmer and the implementation:
    if the programmer obeys certain rules the implementation is required
    to behave in thus-and-such a manner. But if the programmer strays
    outside the specified bounds the Standard no longer attempts to
    govern the implementation; "all bets are off." That's what the
    Standard means when it says "the behavior is undefined:" not that
    "the behavior is fatal," but that "It's your problem now, cobber."

    When you left-shift a negative integer, or when you left-shift
    all the leading zeroes away and "promote" a value bit into the sign
    position, the C Standard throws up its hands. Perhaps you will get
    some kind of value that can (in retrospect) be understood in terms
    of the pre-shifted value. Perhaps you will get an overflow trap.
    Perhaps demons will fly out of your nose. The essential idea is
    this: the C Standard (for reasons both technical and political)
    declines to mandate behavior X or behavior Y; it says "You're on
    your own, Bub" and washes its hands of the matter. You may still
    be able to get a reasonable result -- but if so, you're relying on
    something outside the C language itself, like some other Standard
    or like the idiosyncracies of the implementation you're using.

    Advice: Most of the time, the bit-shifting and bit-logical
    operators should be used with unsigned values because the effect
    of mucking with the "sign" bit is well-defined. Bit-fiddling
    with signed quantities can be done safely, but is usually of little
    value and should usually be avoided.

    --
    Eric Sosman
    esosman@acm-dot-org.invalid

    Comment

    • joshc

      #3
      Re: 6.5.7.4 Bitwise shift operators


      Eric Sosman wrote:[color=blue]
      > joshc wrote:[color=green]
      > > I have a question about the following:
      > >
      > > 4. The result of E1 << E2 is E1 left-shifted E2 bit positions:[/color][/color]
      vacated[color=blue][color=green]
      > > bits are filled with zeros. ... If E1 has a signed type and a
      > > nonnegative value, and E1x2^32 is representable in the result type,[/color]
      >
      > That's "E2," not "32."
      >[color=green]
      > > then that is the resulting value; otherwise, the behavior is[/color][/color]
      undefined.[color=blue][color=green]
      > >
      > > I am a bit confused by this last clause- "otherwise, the behavior[/color][/color]
      is[color=blue][color=green]
      > > undefined." Does this mean I should not left shit a signed negative
      > > value because undefined behavior will result?[/color]
      >
      > Yes, "the behavior is undefined" means "the behavior is
      > undefined." Also, the typo is particularly apt ...
      >[/color]

      Yes, that was a typo and it is obvious the 32 would be stupid. Thanks
      for the rest of your info though.

      Comment

      Working...