Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Portable 16 bit binary integer write Date: Fri, 16 Sep 2016 16:23:25 -0700 Organization: None to speak of Lines: 34 Message-ID: References: <50a1e8f4-4fb3-4491-97d0-54768bf28685@googlegroups.com> <4fde1133-61ff-4045-9cbe-cccff1408ad3@googlegroups.com> <0b757bbb-0494-44cb-bd5a-26f117aaa404@googlegroups.com> <2cf666d7-0755-4c70-adad-07dbafc2b2e7@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="16447"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18bZ2PX3c45DNWww5IljBme" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:2LIjkOaeJxyBNHAyKZwau4dO8Sk= sha1:N9BYV3KF1G2ia8nuII5Afns1vDY= Xref: csiph.com comp.lang.c:93669 supercat@casperkitty.com writes: > On Friday, September 16, 2016 at 12:59:14 PM UTC-5, David Brown wrote: [...] >> supercat wrote: >> > As evidence, I offer the fact that shifting a negative number left is UB >> > even though so far as I can tell every two's-complement machine (i.e. the >> > majority of C implementations) implements them the same way. >> >> Left-shift of a negative number /is/ defined, as long as its value makes >> sense. Perhaps you should read the standards before complaining about them. > > I did read the standard. From N1570 6.5.7: > > 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 nonnegative value, > and E1 x 2E2 is representable in the result type, then that is the > resulting value; otherwise, the behavior is undefined. Right. Your statement was that "shifting a negative number left is UB". It's UB only if the result is outside the range of the signed type. Your statement implied (certainly to me, and presumably to David) that left-shifting a negative number *always* has undefined behavior. It doesn't. If that's not what you meant, that's fine, but please recognize that your statement was at best unclear. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"