Trying to understand the syntax in the standard:
6.3.1.3 Signed and unsigned integers
- When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
- Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
- Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
Note section 2
, which means that casting int
to unsigned int
is performed by adding UINT_MAX + 1
to the value in the int
.
Which is discussed for example in those two discussions:
Can a C compiler change bit representation when casting signed to unsigned?
Signed to unsigned conversion in C - is it always safe?
Well, since UINT_MAX + 1
is always promised to be zero (https://stackoverflow.com/a/14899158/2162550) section 2
can be read as:
Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting zero until the value is in the range of the new type.
Which makes no sense to me, since adding zero change nothing. Is my english interpretation is broken? What am I missing here?