3 ms·
Assume sizeof(int) == sizeof(unsigned int) == 4 g << h Well defined because 2147483648 can be represented in a 32 bit unsigned int Even under those assumption
by cautious_int 11y ago
Assume sizeof(int) == sizeof(unsigned int) == 4
g << h Well defined because 2147483648 can be represented in a 32 bit unsigned int
Even under those assumptions, it is implementation defined if unsigned int can hold the value 2^31. It is perfectly valid to have an UINT_MAX value of 2^31-1. In that case the code will cause undefined behavior.
The only guarantee for unsigned int is that it's UINT_MAX value is at least 2^16-1, regardless of its bit size, and that it has at least as much value bits as a signed int.
For example C allows these ranges:
int: -2^31 , 2^31-1
unsigned int: 0 , 2^31-1