Signed without signature

look at this code:

void main ()
{
int i = -1;
unsigned u = 1;

cout << u + i;
}

adding u (unsigned) and I (signature), so I have to convert to an unsigned type, so it should be interpreted ((2 ^ 32) - 1), and the expression should change: -1 + 1 to ((2 ^ 32 ) - 1) + 1, but when I run the code, it leads to 0 why?

+3
source share
2 answers

-1in an unsigned representation of the largest possible number unsigned may contain ( UINT_MAX).

Adding 1 to this completes due to properties unsignedof 0.

+6
source

(unsigned) -1 - 0xFFFFFFFF. 1 + 0xFFFFFFFF = 0x100000000, which overflows int and leads to 0.

+1
source

All Articles