E160 - Implicit conversion may change the sign
A value is converted implicitly to an integer type that cannot hold every value of the source type, and
the two types differ in their sign. A negative value of a signed type is read as a large positive value
by an unsigned or bit type, and a large unsigned value is read as a negative value by a signed type of the
same size or smaller. Bit types (BYTE, WORD, DWORD, LWORD) count as unsigned.
An untyped integer literal is judged by its value. 5 can be assigned to an unsigned type, -1 cannot.
A literal with a type, such as DINT#5, is judged by its type.
Code example:
PROGRAM mainProg
VAR
signed : DINT := -1;
unsigned : UDINT;
END_VAR
unsigned := signed; // warning: DINT to UDINT
unsigned := -1; // warning: the literal is negative
unsigned := unsigned + 1;
END_PROGRAM
To silence the warning, convert explicitly:
unsigned := DINT_TO_UDINT(signed);
A conversion that narrows the type is reported by E067 as well. When the value is the result of an operation whose operand changes its sign, the operand is reported by E161 and the result is not reported.