Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: two's complement idea
Date: Tue, 25 Feb 2020 07:56:46 -0800
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <86y2sqace9.fsf@linuxsc.com>
References:
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="736228d0aa4bff80edfe6f310ec1eb3b"; logging-data="26676"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IUGLSuQrKTcIaQ95CyM2TgX0e8TlvcIU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:1MvFomT1DFyzIjfhfC4TBtStjjc= sha1:t2niH8haM3mLVQUobU4BM+iyV7M=
Xref: csiph.com comp.lang.c++:72021
cross@spitfire.i.gajendra.net (Dan Cross) writes:
> 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)
>
> ?
Why not just this? (size_t)-1 /2 +1