Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #72005

Re: two's complement idea

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c++
Subject Re: two's complement idea
Date 2020-02-24 08:22 -0800
Organization A noiseless patient Spider
Message-ID <86ftf0arax.fsf@linuxsc.com> (permalink)
References (2 earlier) <r090qk$dv2$1@dont-email.me> <868slti7gm.fsf@linuxsc.com> <r0mhtd$j13$1@dont-email.me> <86a75kgdjh.fsf@linuxsc.com> <r2atqe$8je$1@dont-email.me>

Show all headers | View raw


"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> writes:

> On 15.02.2020 14:56, Tim Rentsch wrote:
>
>> "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> writes:
>>
>>> On 27.01.2020 10:17, Tim Rentsch wrote:
>>>
>>>> "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> writes:
>>>>
>>>>> 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<ptrdiff_t>::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.
>>>>
>>>> Or just size_t(-1)/2+1.
>>>
>>> Or ~(size_t(-1)/2).
>>
>> In practice that is highly likely to work, but strictly speaking
>> it is not guaranteed (at least not in C;  I believe that holds
>> also in C++ but I am less well-versed in the C++ standard than
>> the C standard).  The reason is size_t is not required to be at
>> least as wide as int, so the ~ could produce a negative value.
>
> Interesting point.
>
> Does any C or C++ implemention with sizeof(size_t) < sizeof(int)
> exist, or has any such implementation existed?

I doubt it.  Certainly I don't know of any.

> Fix:  `~(size_t(-1)/2u)`. But the question is, since code is about
> communicating to humans, does this communicate the right thing?

I prefer the form I gave, because it's easer to see that the
value it produces is correct, and because it is less brittle, in
the sense that it doesn't depend on surrounding context to
produce the correct value.  Come to think of it though I would
change it slightly, to get a form that works in both C and C++:

    (size_t)-1 /2 +1

Normally in cases like this I would look for an alternative that
does not use a cast.  Here though I think a cast is necessary:
other possible ways of robustly getting the right value all are
worse in one way or another AFAICS.  Of course, if <stdint.h>
has been #include'd, we could use

    SIZE_MAX/2 + 1

which uses neither a cast nor the converting of -1.  So that's
probably better, in most circumstances, than the other forms
mentioned.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: two's complement idea Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-22 08:32 +0100
  Re: two's complement idea "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-01-22 09:23 +0100
    Re: two's complement idea Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-22 14:20 +0100
      Re: two's complement idea Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2020-01-22 13:37 +0000
    Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-27 01:17 -0800
      Re: two's complement idea "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-01-27 12:35 +0100
        Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-15 05:56 -0800
          Re: two's complement idea "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-02-16 09:17 +0100
            Re: two's complement idea Richard Damon <Richard@Damon-Family.org> - 2020-02-21 19:35 -0500
              Re: two's complement idea "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-02-22 07:01 +0100
            Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-24 08:22 -0800
    Re: two's complement idea cross@spitfire.i.gajendra.net (Dan Cross) - 2020-02-24 03:12 +0000
      Re: two's complement idea cdalten@gmail.com - 2020-02-24 05:53 -0800
      Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-25 07:56 -0800
        Re: two's complement idea cross@spitfire.i.gajendra.net (Dan Cross) - 2020-02-25 22:00 +0000
          Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-25 16:14 -0800
            Re: two's complement idea cdalten@gmail.com - 2020-02-25 18:18 -0800
            Re: two's complement idea cross@spitfire.i.gajendra.net (Dan Cross) - 2020-02-26 02:22 +0000

csiph-web