0

I'm very new to C++ and while I was trying to do something by brute force, and while adding some positive integers, I noticed at some point that the sum became negative.

By looking a little bit more closely, I noticed that adding 2147483647 with 1 is returning -2147483648.

Does someone knows why this - sign appears ?

JWM
  • 53
  • 1
  • 4
  • 3
    it is because of integer overflow – advocateofnone Mar 03 '15 at 23:23
  • [`#include `](http://www.cplusplus.com/reference/climits/) – Mooing Duck Mar 03 '15 at 23:27
  • Related: http://stackoverflow.com/questions/17496666/for-every-int-x-x1-x-is-this-always-true – indiv Mar 03 '15 at 23:28
  • It's the same reason an odometer rolls over from its highest value to its lowest value. – David Schwartz Mar 03 '15 at 23:58
  • @DavidSchwartz odometers don't show negative values though so it is not really the same. In fact it depends on the definition of 2's complement. On systems that use one's complement or sign-magnitude then this would not roll over to the lowest value. – M.M Mar 04 '15 at 00:01
  • @MattMcNabb As I said, it's the same in that it rolled over from its highest value to its lowest value. I'm not claiming it must always be the same in every imaginable way it *could* happen, I'm just talking about the way it actually *did* happen, so what other systems do or might do is not relevant. – David Schwartz Mar 04 '15 at 01:01

2 Answers2

5

This is well-known and is called an Integer overflow.

A signed integer has a maximum value (2147483647) which can't be exceeded.
Exceeding it will cause a loopback to its minimal value which is -2147483648.
It is closely linked to the way memory works.

Telokis
  • 3,399
  • 14
  • 36
  • 1
    Specifically, a `signed` integer has the limitation. An `unsigned` integer has a greater range. – Thomas Matthews Mar 03 '15 at 23:49
  • I modified it. Thanks – Telokis Mar 03 '15 at 23:50
  • 1
    @ThomasMatthews an unsigned integer still has a limit, but it wraps around to zero rather than a very negative number. – Mark Ransom Mar 03 '15 at 23:51
  • 2
    Note that `INT_MAX + 1` is _undefined behavior_. Beware of [nasal demons](http://catb.org/jargon/html/N/nasal-demons.html). – Nicu Stiurca Mar 03 '15 at 23:57
  • 1
    In other words, exceeding it **may** cause a loopback. As the linked page notes, also legal is the result `INT_MAX+1 == INT_MAX` (no change) which you sometimes see on DSP's. And on DSP's you may also see `32767+1==32767` as they're not all 32 bits. – MSalters Mar 04 '15 at 12:32
2

Further info about integer overflow.

Historically this comes from the fact that on common CPUs, issuing the hardware increment instruction resulted in the behaviour that you see.

In C, and in C++ prior to C++11, this was actually undefined behaviour. Recent versions of gcc -O3 may perform optimization based on this operation being undefined (e.g. cut out flow branches where this operation occurs).

However, since C++11 there is a bool named std::numeric_limits<int>::is_modulo that you can inspect. If this is false then your code is undefined behaviour, but if this is true then it is well-defined to perform what you are setting.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365