Before C++20 signed integers were not guaranteed to be two's complement. Now we have two papers proposing standardization of two's complement as the only representation: p0907 and p1236 and, if I understand correctly, one of them got merged into C++20 working draft.
So, what does it mean for signed-to-unsigned conversion and vice versa? I've looked at cppreference and has found the following wording:
If the destination type is unsigned, the resulting value is the smallest unsigned value equal to the source value modulo
2n
wheren
is the number of bits used to represent the destination type.If the destination type is signed, the value does not change if the source integer can be represented in the destination type. Otherwise the result is the unique value of the destination type equal to the source value modulo
2n
wheren
is the number of bits used to represent the destination type. (Note that this is different from signed integer arithmetic overflow, which is undefined).
Unfortunately, I have problems understanding this wording and I want to know what is written in C++20 Working Draft.
So there are two questions:
Language lawyer part: can someone please point what the standard says exactly and where in the standard does it say it?
Can someone explain that wording in more layman's terms, probably also explaining modular arithmetic and providing examples?