Hi all,
I just upgraded from EW9.0 to EW11. I started this just before EW12 was released...
EW11 does compile some strange things with an assignment of the most negative int32 number...
The code was:
//cura
var int32 val32 = 0;
val32 = (int32)0x80000000;
So this is the most negative number in twos-complement.
It generates following c-code:
//c
XInt32 val32;
val32 = -(int)2147483648;
But why is this typecast there? this splits the integer-literal into two parts. Now it takes 2147483648 as a positive number and after casting it to (int), it builds the negative of it:
-(int)2147483648 //is interpreted as follows:
-((int)2147483648)
the positive number 2147483648 overflows to -2147483648, so the expression is now:
-(-2147483648) = 2147483648
that again will overflow to
-2147483648
but the initial overflow will trigger a warning in gcc:
warning: integer overflow in expression '-2147483648' of type 'int' results in '-2147483648' [-Woverflow]
I tried to work-around with changing the strange hex-to-int-cast to the actual integer value:
//cura
val32 = -2147483648
but now even ew studio complaints:
Warning Application::parentService.onPressButton (11:11) : Unary minus operator applied on unsigned operand 'uint32'. This may cause an overflow. Therefore the resulting value is considered as being still unsigned 'uint32'. To suppress the warning prefix the operand by the type cast operator (int32), e.g. -(int32)(operand).
the resulting c-code is again:
//c
val32 = -(int)2147483648;
When following the suggestion of adding the cast-operator:
//cura
val32 = -(int32)2147483648;
then, the warning in ew-studio is gone, but the same c-code is built:
//c
val32 = -(int)2147483648;
Thus the gcc overflow-warning is still present.
I would understand, that my strange cast from the beginning ((int32)0x80000000) is hacky. But why on earth i could not enter a simple negative integer literal? Why is the minus interpreted as an unary operator?
Greetings Daniel