This question is asked before but I am still confused.
I know that
unsigned int a = -1;
would be UINT_MAX
. But it is not because all bits of -1 is set. C11 says
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
So lets say UINT_MAX
is 100 (I know it should be greater then 2^16-1 but lets ignore this for now)
unsigned int a = -1; // will be
unsigned int a = -1 + UINT_MAX + 1; // 100 = UINT_MAX
Standard only says UINT_MAX >= 2^16-1
. But does it say anywhere it should be 2^n-1?
Also is answer different in C++?