Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.linkpendium.com!news.linkpendium.com!news.iecc.com!nnews.iecc.com!.POSTED.gal.iecc.com!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Is this code considered to transgress the signed overflow purism? Date: Thu, 27 Apr 2017 15:13:46 -0700 Organization: None to speak of Lines: 55 Message-ID: References: <11ea6682-cdee-4513-97c2-6c90b585716a@googlegroups.com> <87o9vi6rql.fsf@bsb.me.uk> <26aee5e9-ed2f-4c6e-af68-b85418c7e641@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: gal.iecc.com; posting-host="gal.iecc.com:64.57.183.53"; logging-data="11809"; mail-complaints-to="abuse@iecc.com" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Funky-Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail Funky-Injection-Info: mx02.eternal-september.org; posting-host="2e5ec5e1f0fe87e81c65a0e293e55e1b"; logging-data="10722"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hHVDJNB9ffk1rj2UHbXzb" Cancel-Lock: sha1:I1TyhqKm8VXYZ15I/2yXlBnG8k4= sha1:oj2HvgnmaseSOieffCFYALVXb4w= Funky-Xref: news.eternal-september.org comp.lang.c:187480 Xref: csiph.com comp.lang.c:108630 joel.rees@gmail.com writes: [...] > We have this coddling of brain-damaged CPUs when a left-shift on a > negative operand is specified on sign-magnitude architectures or > something, but we have this assurance that casting a -1 to an unsigned > fills the thing with ones. > > (I know calling it brain-damaged is too extreme, but ...) I suggest you might benefit from describing these things in less inflammatory terms. The behavor of left-shifting a negative value is undefined. This is specified in N1570 6.5.7p4. (I don't recall that I've ever felt the need to left-shift a negative integer.) The behavior of converting an integer (either signed or unsigned) value to an unsigned type is well defined; the result is adjusted modulo MAX+1, where MAX is the maximum value of the unsigned type. This is described in N1570 6.3.1.3p2. (N1570 is the latest publicly available draft of the ISO C standard, available at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf .) [...] > Unsigned char promoted to signed int? Yes. The promotion rules are in N1570 6.3.1.1p2. The idea is that there are no arithmetic operations on types narrower than int or unsigned int, so anything narrower must be "promoted" before you can operate on it. If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the *integer promotions*. The 1989 ANSI C committee had to choose between two schemes, both implemented by existing compilers at the time: "unsigned preserving" and "value preserving". They chose the latter. (I think I would have preferred unsigned preserving semantics myself, but I haven't taken the time to think about possible disadvantages.) This is discussed in the C99 Rationale, section 6.3.1.1, available at http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf . [...] -- 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"