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 19:06:05 -0700 Organization: None to speak of Lines: 35 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> <09cb4413-16a6-4b63-89d0-9dafe56562f6@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="29564"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yXA1mmCmKBe+CUfqYNZk8" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:y2tpcBBqbsypQOzYQxOw5+p+9z0= sha1:Nrt9VCE8/ydcvJtLk/uBhSn6riY= Xref: csiph.com comp.lang.c:93674 supercat@casperkitty.com writes: > On Friday, September 16, 2016 at 4:17:31 PM UTC-5, Keith Thompson wrote: >> supercat writes: >> > 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. > > The language of the Standard only specifies behavior if E1 is unsigned, or > if *both* of the following conditions apply: > > 1. E1 is signed AND NON-NEGATIVE. > > 2. The result of multiplying E1 * 2^E2 would be representable in the > result type. > > The expression -5 << 3 satisfies the second criterion, but fails the first. > Consequently, it invokes Undefined Behavior. You're absolutely right. Looks like I failed reading comprehension for the day. My apologies. -- 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"