6

What does C or C++ standards say about signed integer division for this special case, is it defined and in what way?

INT_MIN / -1      (0x8000_0000 / 0xFFFF_FFFF)

The mathematics result would be the positive value 0x8000_0000 which is not representable in 32 bit signed 2-complement.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Moberg
  • 5,253
  • 4
  • 38
  • 54
  • 9
    Please note that there's no such language as "C/C++", only the two separate, distinct and *very* different languages C and C++. Even for such simple things the wording of the standards could be different, leading to different semantics. Also, do you want information for a specific version of the standard? And if you want an authoritative quote from a standard you probably should add the `language-lawyer` tag. – Some programmer dude Oct 27 '21 at 07:17
  • Looks like UB. Try to do it at compile-time and you'll get an error (in GCC, Clang, and MSVC). – HolyBlackCat Oct 27 '21 at 07:19
  • 10
    _"...When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined,..."_ see __Overflows__ here - https://en.cppreference.com/w/cpp/language/operator_arithmetic – Richard Critten Oct 27 '21 at 07:20
  • 4
    Rules are different between C, C++<20 and C++20. – Bathsheba Oct 27 '21 at 07:30
  • The question is too broad. Please narrow down the question to one programming language. The answer is even different depending on which C or C++ version that is used. – Lundin Oct 27 '21 at 07:32
  • According to basic mathematics, dividing by -1 and multiplying by -1 is the same. What happens when you multiply instead of divide? – Dominique Oct 27 '21 at 07:43
  • 1
    @Dominique overflow is UB in C and C++<20 regardless of the operation you're doing – phuclv Oct 27 '21 at 08:04
  • @Dominique: "basic mathematics" -> "basic arithmetic". The proof in the language of the former is not trivial. – Bathsheba Oct 27 '21 at 08:04
  • I don't think the dupe is correct, because it doesn't answer in the language lawyer sense – phuclv Oct 27 '21 at 08:05
  • @phuclv: But this question is not tagged language lawyer. – Bathsheba Oct 27 '21 at 08:05
  • @phuclv: UB = Undefined Behaviour. (I needed to look it up. :-) ) – Dominique Oct 27 '21 at 08:06
  • Neither `INT_MIN` nor `-1` are necessarily represented as `0x80000000` or `0xFFFFFFFF`. To answer the question, if a mathematical operation (addition, multiplication, division, etc) on two values of type `int` produces a result that cannot be represented as an `int`, then the behaviour is undefined. If `INT_MIN/-1` (or `INT_MIN*(-1)`) produces a value that is more than `INT_MAX`, then the behaviour is undefined by the standard. A real world example where this could happen is a system with a 32-bit `int` that has `INT_MIN` equal to `-2147483648` and `INT_MAX` equal to `2147483647` – Peter Oct 27 '21 at 08:10
  • ...which is why you often see INT_MIN defined as -2147483647 - 1 – Bathsheba Oct 27 '21 at 08:16
  • @Bathsheba but the OP is asking `What does C or C++ standards say about signed integer division` and the ansers in the other question doesn't say anything about standards – phuclv Oct 28 '21 at 05:33
  • @phuclv Well that's a good point. I'll vote to reopen, and add the tag. – Bathsheba Oct 28 '21 at 06:59
  • Perhaps the question shouldn't have grouped c and c++ together. – Moberg Nov 16 '21 at 15:49

1 Answers1

3

Both the C standard [1] and C++ standard [2] say that signed integer overflow leads to undefined behavior.

[1]: https://www.gnu.org/software/autoconf/manual/autoconf-2.63/html_node/Integer-Overflow-Basics.html

[2]: https://en.cppreference.com/w/cpp/language/operator_arithmetic#Overflows

As the OP correctly states, the mathematical result would be the positive value 0x8000_0000 which is not representable in 32 bit signed 2-complement. Hence, this is a case of signed integer overflow.

In case of undefined behaviour, a program can do anything. This includes returning an arbirary result, dumping core, or even overrunning a buffer.

Moberg
  • 5,253
  • 4
  • 38
  • 54
Hanno
  • 41
  • 5
  • 1
    There were a lot of comments on the original post mentioning C++<20 and C++20. Is there any difference between them in this regard? – Moberg May 17 '23 at 16:36
  • No difference for C++20 in case of overflow due to an undefined computation, see [this post](https://stackoverflow.com/questions/70801443/why-is-signed-overflow-due-to-computation-still-undefined-behavior-in-c20) foe details. – Hanno May 18 '23 at 17:53