Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!not-for-mail From: cross@spitfire.i.gajendra.net (Dan Cross) Newsgroups: comp.lang.c++ Subject: Re: two's complement idea Date: Mon, 24 Feb 2020 03:12:22 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 21 Message-ID: References: NNTP-Posting-Host: 166.84.136.80 X-Trace: reader2.panix.com 1582513942 8970 166.84.136.80 (24 Feb 2020 03:12:22 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 24 Feb 2020 03:12:22 +0000 (UTC) X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: cross@spitfire.i.gajendra.net (Dan Cross) Xref: csiph.com comp.lang.c++:71988 In article , Alf P. Steinbach wrote: >On 22.01.2020 08:32, Bonita Montero wrote: >> I recently had the problem that I wanted to have a constant which is >> size_t wide and where the highest bit is set. I simply solved it that >> way (I assume ptrdiff_t as wide as size_t, but this should be common >> today on all platforms): >> std::size_t const SIZE_T_MSB = std::numeric_limits::min(); >> A guarantee from the standard that signed values are 2's-complement >> woud be nice for that. > >C++20 will give you that guarantee. > >But e.g. `size_t(-1) - size_t(-1)/2` does the job with just C++98 >guarantees. Hmm, can't one simply compute: ~(size_t)0 ^ (~(size_t)0 >> 1) ?