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: Sat, 17 Sep 2016 17:32:32 -0700 Organization: None to speak of Lines: 38 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> <68e53f3a-5fcc-44fc-90c4-bcf8dc3db680@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="15983"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SfMoqv0kQC9Z8QMmpEH2E" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:kPgtlGyQnncR2GZs5AMR+iYcZBE= sha1:/4n/3agXeqwGh9SkPg2fGKI6wuU= Xref: csiph.com comp.lang.c:93708 David Brown writes: > On 17/09/16 08:27, supercat@casperkitty.com wrote: [...] >> Before you recognized that you'd misread the Standard, you presumably >> would have thought that a programmer would be entitled to expect that >> when x==-5, a compiler would evaluate x<<3 as -40 with no >> side-effects. Does the fact that the Standard imposes no >> requirements change that? > > It changes nothing because: > > a) The compiler I use most, gcc, does exactly what you expect here. > The documentation of this behaviour in gcc could be clearer, but > combining the documentation with common sense (the compiler will use > the simplest and most efficient reasonable instruction on the target > cpu), you arrive at this interpretation. I probably wouldn't rely on that, partly because ... > b) I don't shift negative numbers. ... but also because gcc's behavior could easily vary for different target CPUs. It's likely that a shift on a signed integer would use the same instruction as a shift on an unsigned integer, which would yield the "expected" -40 on a 2's-complement system (and AFAIK gcc only supports 2's-complement), but there could easily be some pitfall that I haven't thought about. Ensuring that I only shift unsigned integers, or at least that I never shift negative integers, is a better use of my time than verifying that shifts of negative integers are going to "work" the way I might expect them to. -- 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"