6.5.7.4 Bitwise shift operators

J

joshc

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

Eric Sosman

joshc said:
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,

That's "E2," not "32."
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?

Yes, "the behavior is undefined" means "the behavior is
undefined." Also, the typo is particularly apt ...
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?

"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 "catastrophic
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.
 
J

joshc

Eric said:
That's "E2," not "32."


Yes, "the behavior is undefined" means "the behavior is
undefined." Also, the typo is particularly apt ...

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top