Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.std.c Subject: Re: Why is shifting too far undefined behvaior? Date: Sat, 09 Oct 2021 04:27:45 -0700 Organization: A noiseless patient Spider Lines: 23 Message-ID: <87ee8uh1zi.fsf@shamko.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="e386cac8d3920670231b8e03b7881cbe"; logging-data="25602"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/i9EMQQ/lGFONrP6zEHZZZeEu2qeOj2rQ=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:OoDLik2hbx9wR+BKigrb9Tx2Ybo= sha1:ieP0SZc44j0xKfhFGC66Bd45urw= Xref: csiph.com comp.std.c:6343 Philipp Klaus Krause writes: > Looking at the C23 draft N2596, section J.2 (but this AFAIK has been > the same forever), we see that there is undefined behavior, when "An > expression is shifted by a negative number or by an amount greater > than or equal to the width of the promoted expression (6.5.7)." or > "An expression having signed promoted type is left-shifted and > either the value of the expression is negative or the result of > shifting would not be representable in the promoted type (6.5.7)." > > Does anyone know why this was made undefined (as opposed to yielding > an unspecified value)? This question tacitly assumes that there needs to be a reason. To me it seems quite plausible that these cases were left as undefined behavior simply because no one saw any reason to define them. Not everything thinks that just because something can be done that it should be done. > The C99 rationale doesn't mention anything. That observation supports the hypothesis that no one thought it important to define the behavior in any way.