6

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?

diralik
  • 6,391
  • 3
  • 28
  • 52

1 Answers1

4

It is implicitly guaranteed, since it is true for all the allowed forms of signedness:

(examples with 16 bit int)

  • One's complement, INT_MIN = -32767, INT_MAX = 32767
  • Two's complement, INT_MIN = -32768, INT_MAX = 32767
  • Sign & magnitude, 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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • For completeness: [this is a nice answer showing the minimum numerical ranges of the types of the C++ standard](https://stackoverflow.com/a/589684/8051589). – Andre Kampling Sep 07 '17 at 12:01
  • Where are those allowed forms of signedness listed in the standard? Or do you have a reference to how they are derived from the standard? – Yakk - Adam Nevraumont Sep 07 '17 at 14:40
  • 2
    @Yakk C++11 3.9.1 §7 `The representations of integral types shall define values by use of a pure binary numeration system.49 [ Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. —end example ]`. The C wording is a bit different but the same applies there too. – Lundin Sep 07 '17 at 14:47
  • Thanks for answer! Could you, please, explain, why there is word `Example` in this citation? – diralik Sep 07 '17 at 19:32
  • @Lundin Who says that an implementation or even the architecture couldn't reserve certain values? E.g. ``INT_MIN`` (update: not ``INT_MIN`` but the bit pattern, of course) might be illegal and loading it into a register with a "normal" operation might result in an interrupt, for example to help with debugging loads from uninitialized or freed memory. If that sounds far-fetched, consider that several libraries in debug mode will already initialize unallocated or freed memory with certain bit patterns. To put my question differently: What exactly is a "pure" binary numeration system? – Arne Vogel Sep 08 '17 at 10:31
  • @diraria Because they made an example? And yeah I suppose that examples aren't normative, the C++11 standard is a bit weird. The C standard is clearer, C11 6.2.6.2 "If the sign bit is one, the value shall be modified in one of the following ways: — the corresponding value with sign bit 0 is negated (sign and magnitude); — the sign bit has the value −(2M) (two’s complement); — the sign bit has the value −(2M − 1) (ones’ complement)." – Lundin Sep 08 '17 at 12:50
  • @ArneVogel The standard says so. 3.9.1 §2 `"Plain ints have the natural size suggested by the architecture of the execution environment 44)"` where note 44 provides the explanation: `"44) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header ."` Making the value of INT_MIN an illegal value, would turn the implementation non-compliant... and why would anyone do that, it doesn't make any sense. – Lundin Sep 08 '17 at 12:53
  • 1
    @ArneVogel "A pure binary system" is explained by note 49: `49) A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position. (Adapted from the American National Dictionary for Information Processing Systems.)` – Lundin Sep 08 '17 at 12:53
  • @Lundin: Yes, I realized that ``INT_MIN`` makes no sense, *hence the comment in parens*: Using the bit pattern that would otherwise be ``INT_MIN`` (and consequently setting ``INT_MIN`` one higher than it would otherwise be). So, e.g. -32768 (two's complement) could be reserved while ``INT_MIN`` is defined as -32767. Not sure what to make of note 49 but thanks a lot for the information. – Arne Vogel Sep 08 '17 at 13:32
  • @Lundin For signed integers, any operation that produces a value that cannot be represented in the integer's range leads to undefined behavior. If I made a two's complement, 16-bit int implementation where INT_MIN == -32767, and you'd write ``int i = -32767; --i;``, certainly this would be UB, right? So basically the question is do I have to define ``INT_MAX`` as -32768 in 16-bit, with two's complement? – Arne Vogel Sep 08 '17 at 13:36