C++ Logo

std-discussion

Advanced search

Re: Validity of conversions between 4 byte-like types (byte, char) objects

From: Bo Persson <bo_at_[hidden]>
Date: Thu, 12 Dec 2019 10:54:16 +0100
On 2019-12-11 at 19:09, Roman Babinicz via Std-Discussion wrote:
>
>
> On 11/12/2019 17:26, Thiago Macieira wrote:
>
>> Pre-C++17:
>> The integer conversions are very well explained in the standard. Converting
>> from signed to unsigned is such that any non-negative values retain their
>> values and any negative ones are represented as TYPE_MAX + 1 -
>> absolute(value).
>
> Thanks,
>
> in e.g. C++14, which points of standard define the negative values as
> above to be converted into TYPE_MAX + 1 - absolute(value) ?
>
>
> It was my understanding that the converted value in such case depends on
> the binary representation of negative integers, and it would be as you
> written on platforms with 2's complement - but could differ on others.
>
> C++14 4.7(.2) [conv.integral] mentions what would happen in two's
> complement representation - but that "Note" text seems to only apply to
> situation in which we know that the two's complement is used for the
> signed type.
> And in C++14 that is not guaranteed afaik.

No, it is not *guaranteed* by the standard, but in practice it is. There
are no known C++ compilers for any other type of machines.

So in C++20 two's complement is the only possible representation.
Because that is all that exists.


>
> Which is why I thought that for some values it is implementation defined
> what will be the result of assigning signed int into unsigned int.
>
>
>

Even if it is implementation defined, all existing implementations will
define it as two's complement.


So if you write some ones' complement version of your code, you will
have an *extremely* hard time trying to test it anywhere. :-)


    Bo Persson

Received on 2019-12-12 03:56:51