C++ Logo

std-discussion

Advanced search

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

From: Roman Babinicz <rb_at_[hidden]>
Date: Wed, 11 Dec 2019 19:09:49 +0100
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.

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.

Received on 2019-12-11 12:32:57