Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Are there any conformant C compilers? Date: Tue, 13 Sep 2022 15:52:09 -0700 Organization: A noiseless patient Spider Lines: 32 Message-ID: <86fsguopk6.fsf@linuxsc.com> References: <87zgfqk9l4.fsf@nosuchdomain.example.com> <413ae987-3216-415f-88cf-8a6a0f7cfe4an@googlegroups.com> <68fbf792-a971-4e42-af84-c02257625abdn@googlegroups.com> <87ilmcyuv3.fsf@bsb.me.uk> <86illspz8y.fsf@linuxsc.com> <0TGTK.472157$BKL8.553@fx15.iad> <87sfkwwutb.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="e4a07c6eb40a7dcf36ed28254bbf399a"; logging-data="2837108"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WdnsQNMay3z/uSVzQ846H+KlS7s+TJCE=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:/vDyuPX4JKOYOYIZz+ee56S6BOA= sha1:HXH3d2ebcBk8oumpx445ngRqbRA= Xref: csiph.com comp.lang.c:167687 Ben Bacarisse writes: > scott@slp53.sl.home (Scott Lurndal) writes: > >> Tim Rentsch writes: > > > >>> constexpr unsigned int all_ones = -1; >> >> Wouldn't it be more natural to write >> >> constexpr unsigned int all_ones = ~1; > > You surely meant ~0, yes? > >> I'd never use your suggestion in real code; I've seen programmers do >> it, but it has never been proper. > > I have exactly the opposite reaction. The conversion of -1 to an > unsigned integer type is very explicitly described in such a way that > the maximum number of value bits must be set. This contrasts with ~0 > that might even be a trap representation. > > (~0u on the other had is well-defined.) Using either ~0u or -1u has the drawback that the intended target type is specified redundantly. These expressions might do the wrong thing if the variable being declared, as one example, were of type unsigned long rather than unsigned int. Using plain -1 doesn't have that drawback: it works for a variable declaration having _any_ unsigned type.