6

For example, in both 2's and 1's complement, INT_MAX*-1 is a valid number. Is this guaranteed? Are there any other guarantees? Could INT_MIN be -1 or 0 on any architecture, for example? Is there anything we can know about (INT_MAX + INT_MIN)? Can we even know that (INT_MAX + INT_MIN) doesn't cause undefined behaviour?

Drew
  • 12,578
  • 11
  • 58
  • 98
  • Since the min and max values have opposite signs (see Annex E), their sum must definitely be representable. Annex E also says that `INT_MIN` cannot be -1, since it has to be at most -32767. – Kerrek SB Mar 05 '14 at 00:43
  • INT_MIN must be less than or equal to -32767. So no, it can't be -1 or 0. – Stephen Canon Mar 05 '14 at 00:44

3 Answers3

4

INT_MAX + INT_MIN is always defined because adding two numbers of opposite sign can never overflow.

To answer your question as precisely as possible, the C standard mandates either 2's complement, 1's complement or sign-magnitude representation. (C11 6.2.6.2:2). The int type may have padding bits, but the sign and value bits are in any case used to represent either symmetrical or almost-symmetrical sets of numbers. One always has INT_MIN == - INT_MAX - 1 or INT_MIN == - INT_MAX depending on whether the platform uses 2's complement.

And of course INT_MAX is at least 32767, corresponding to a minimum of 16 bits for value and sign bits.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
2

The C specification requires INT_MIN to be -32767 or less, and INT_MAX to be 32767 or more. As nearly as I can tell, no further guarantees are made. Page 22: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

Scott Lawrence
  • 1,023
  • 6
  • 14
1

You are guaranteed that INT_MIN is at most -32767 and INT_MAX at least 32767.

So INT_MAX-1 is always a valid int.

INT_MAX + INT_MIN is then also always defined.

this
  • 5,229
  • 1
  • 22
  • 51