Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.
Questions tagged [integer-overflow]
1056 questions
7
votes
4 answers
C++ long overflowing prematurely
I'm having a bizarre problem with C++ where the long data type is overflowing long before it should. What I'm doing (with success so far) is to have integers behave like floats, so that the range [-32767,32767] is mapped to [-1.0,1.0]. Where it…

rhodri
- 370
- 1
- 3
- 12
7
votes
3 answers
Does modulus overflow?
I know that (INT_MIN / -1) overflows, but (INT_MIN % -1) does not. At least this is what happens in two compilers, one pre-c++11 (VC++ 2010) and the other post-c++11 GCC 4.8.1
int x = INT_MIN;
cout << x / -1 << endl;
cout << x % -1 <<…

Red
- 171
- 5
- 13
7
votes
2 answers
Catch integer exceptions in Fortran
Is there a way to catch integer exceptions with gfortran or ifort like there is for catching floating point exceptions?
Consider this simple program to calculate the factorial:
program factorial
use, intrinsic :: iso_fortran_env
implicit…

Alexander Vogt
- 17,879
- 13
- 52
- 68
7
votes
2 answers
Why do integer datatypes overflow silently rather than throwing exception
I have learnt(atleast in java) that integer/long values overflow silently and their values start over from minimum value on overflow rather than throwing any exception.
I was using an external api for some file operations, in which max file size was…

Aqeel Ashiq
- 1,988
- 5
- 24
- 57
7
votes
2 answers
How to suppress overflow-checking in PowerShell?
PowerShell seems to perform bounds-checking after arithmetic operations and conversions. For instance, the following operations fail:
[byte]$a = 255
$a++
$a = [byte]256
Is there any way to enforce overflows or the typecast without resorting to a…

Chrysler
- 101
- 3
6
votes
5 answers
32bit int * 32bit int = 64 bit int?
In other words does this work as expected?
int32 i = INT_MAX-1;
int64 j = i * i;
or do I need to cast the i to 64 bit first?

Michael
- 242
- 2
- 10
6
votes
1 answer
Is there any difference between overflow and implicit conversion, whether technical or bit level (cpu-register-level)?
(I'm an novice, so there may be inaccuracies in what I say)
In my current mental model, an overflow is an arithmetical phenomenon (occurs when we perform arithmetic operations), and an implicit conversion is an assignment (initialization or not)…

Manuel
- 220
- 3
- 5
6
votes
6 answers
Generally, How do I prevent integer overflow from happening in C language?
Generally, How can I prevent integer overflow from happening in C programming language? I mean, Is there any functions to prevent?
And finally, Is integer overflow going to get me hacked like buffer overflow or etc?

Amir Mohsen Ghasemi
- 71
- 1
- 1
- 6
6
votes
6 answers
Is the size of an array constrained by the upper limit of int (2147483647)?
I'm doing some Project Euler exercises and I've run into a scenario where I have want arrays which are larger than 2,147,483,647 (the upper limit of int in C#).
Sure these are large arrays, but for instance, I can't do this
// fails
bool[]…

Ian G
- 29,468
- 21
- 78
- 92
6
votes
5 answers
PANDAS: int32 overflow? Can't bulid a pivot table
I use the pd.pivot_table() method to create a user-item matrix by pivoting the user-item activity data. However, the dataframe is so large that I got complain like this:
Unstacked DataFrame is too big, causing int32 overflow
Any suggestions on…

JoFox
- 81
- 1
- 1
- 2
6
votes
2 answers
Why does C# issue the error "cannot implicitly convert int to ushort" against modulo arithmetic on ushorts?
In another thread, someone asked about why adding two ushort values raised errors in C#. e.g.
ushort x = 4;
ushort y = 23;
ushort z = x+y; // ERROR cannot implicitly convert int to ushort
On that thread, people argued that the plus + operater…

Gregory Fenn
- 460
- 2
- 13
6
votes
2 answers
Modular inverses and unsigned integers
Modular inverses can be computed as follows (from Rosetta Code):
#include
int mul_inv(int a, int b)
{
int b0 = b, t, q;
int x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a %…

Ecir Hana
- 10,864
- 13
- 67
- 117
6
votes
3 answers
How to store long in a Swift array?
I got a couple user IDs I want to send in an array, but can't figure out the correct Swift 3 syntax for creating an array with very long integers. I tried casting, @ prefix and using as AnyObject, but that did not work.
let idArray =…

Alex Stone
- 46,408
- 55
- 231
- 407
6
votes
2 answers
Is over/underflow an undefined behavior at execution time?
I was reading about undefined behavior, and I'm not sure if it's a compile-time only feature, or if it can occurs at execution-time.
I understand this example well (this is extracted from the Undefined Behavior page of Wikipedia):
An example for…

Jules Lamur
- 2,078
- 1
- 15
- 25
6
votes
4 answers
Signed integer overflow: 999999999 * 10 cannot be represented in type 'int' Error
Why is there a runtime error? I made range2 a long long.
Code:
/*VARIABLES FOR WHILE LOOP*/
long long range1 = 9;
int length = 1;
/*FIND NUM'S LENGTH*/
while (NUM > range1)
{
long long range2 = range1 * 10 + 9;
length += 1;
…

Khalid Mukadam
- 73
- 1
- 1
- 4