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: Is this code considered to transgress the signed overflow purism? Date: Fri, 28 Apr 2017 12:24:04 -0700 Organization: None to speak of Lines: 21 Message-ID: References: <11ea6682-cdee-4513-97c2-6c90b585716a@googlegroups.com> <87o9vi6rql.fsf@bsb.me.uk> <26aee5e9-ed2f-4c6e-af68-b85418c7e641@googlegroups.com> <1f9a4820-c10f-4130-90c2-464b4bd48c5d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="2e5ec5e1f0fe87e81c65a0e293e55e1b"; logging-data="6241"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BYpSITdAo5gzFNmAJUxTm" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:Xx8VNA7g6ksFlLCrzKAonK7VN7k= sha1:wYxi+Lw64VGy1uBBY7xcFH8thjk= Xref: csiph.com comp.lang.c:108685 Keith Thompson writes: > supercat@casperkitty.com writes: > [...] >> Incidentally, if a platform with 32-bit "int" defines 1<<31 as -1 (likely >> on platforms that also handle C++, as C++ would require such behavior), >> then if "x" is "long long", then a compiler would likely have to generate >> code for x*(1<> would almost certainly be less efficient than x< > No, 1<<31 has undefined behavior in C++ if 2147483648 cannot be > represented as an int. (C++11 standard, section 5.8 [expr.shift], > unchanged in N4618.) I was mistaken on this point. N4618, which is a draft of C++17, makes 1<<31 equivalent to (int)2147483648U (for 32-bit int and unsigned int). -- 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"