www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is the behaviour of shift expressions with negative left operands

reply Harry Gillanders <ayjsy47yyz8 temp.mailbox.org> writes:
The spec doesn't seem to explicitly mention what happens when the 
left operand
of a shift expression is signed and negative. [1]
But I know that D follows C's semantics for this sort of stuff, 
and the C
standard specifies that the result of a negative left operand is 
undefined
for `<<`, and implementation-defined for `>>`. [2][3]

Is D's behaviour the same as C in this regard?

[1]: https://dlang.org/spec/expression.html#shift_expressions
[2]: https://port70.net/~nsz/c/c89/c89-draft.html#3.3.7
[3]: https://port70.net/~nsz/c/c11/n1570.html#6.5.7
Apr 28 2020
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Apr 28, 2020 at 08:30:27PM +0000, Harry Gillanders via
Digitalmars-d-learn wrote:
 The spec doesn't seem to explicitly mention what happens when the left
 operand of a shift expression is signed and negative. [1]
 But I know that D follows C's semantics for this sort of stuff, and
 the C standard specifies that the result of a negative left operand is
 undefined for `<<`, and implementation-defined for `>>`. [2][3]
[...] Yes, it's UB. Don't do it. T -- One Word to write them all, One Access to find them, One Excel to count them all, And thus to Windows bind them. -- Mike Champion
Apr 28 2020