Usually, INT_MIN
is -2 ^ n
and INT_MAX
is 2 ^ n - 1
Is it guaranteed, that if x
is positive number of type int
then expressoin -x
didn't cause overflow?
Usually, INT_MIN
is -2 ^ n
and INT_MAX
is 2 ^ n - 1
Is it guaranteed, that if x
is positive number of type int
then expressoin -x
didn't cause overflow?
It is implicitly guaranteed, since it is true for all the allowed forms of signedness:
(examples with 16 bit int
)
INT_MIN = -32767, INT_MAX = 32767
INT_MIN = -32768, INT_MAX = 32767
INT_MIN = -32767, INT_MAX = 32767
No other forms are allowed. As we can see, abs(INT_MIN) >= abs(INT_MAX)
for all the allowed forms.
As a side note, INT_MAX
is not allowed to be smaller than 32767
and INT_MIN
is not allowed to be smaller than -32767
. This is guaranteed by the requirements for limits.h.