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
15
votes
2 answers
"Int" multiplication in c++ with "long long" result
I am trying to find the square of a int. My code looks like below:
long long sqr=0;
int num=77778;
sqr= num*num;
The result should have been 6049417284
But when I check the output it shows 1754449988.
What is the mistake I am doing?
long long…

srip
- 617
- 1
- 6
- 18
15
votes
2 answers
Is overflow of intN_t well-defined?
In C99 there're some (optional) types like int8_t, int16_t and the like, which are guaranteed to be have exactly specified width and no padding bits, and represent numbers in two's complement (7.18.1.1). In 6.2.6.2 signed integer overflow is…

Ruslan
- 18,162
- 8
- 67
- 136
15
votes
4 answers
Often big numbers become negative
Since I started using eclipse for project euler, I noticed that big numbers sometime become a seemingly random negative numbers. I suppose this has something to do with passing the boudry of the type.
I'll be glad if you could explain to me how…

user2435678
- 319
- 2
- 3
- 14
14
votes
3 answers
Why don't I get an integer overflow when adding two chars?
Possible Duplicate:
Addition of two chars produces int
Given the following C++ code:
unsigned char a = 200;
unsigned char b = 100;
unsigned char c = (a + b) / 2;
The output is 150 as logically expected, however shouldn't there be an integer…

hiddensunset4
- 5,825
- 3
- 39
- 61
14
votes
3 answers
Fast method to multiply integer by proper fraction without floats or overflow
My program frequently requires the following calculation to be performed:
Given:
N is a 32-bit integer
D is a 32-bit integer
abs(N) <= abs(D)
D != 0
X is a 32-bit integer of any value
Find:
X * N / D as a rounded integer that is X scaled to N/D…

Taron
- 191
- 9
14
votes
4 answers
How to detect overflow on power in Java
I know that java.lang.Math provides a set of static methods to perform some operations (sum, difference, multiply, increment, decrement, negate, toInt), throwing an ArithmeticException on overflow.
Is there something similar for power?

Claudia
- 521
- 1
- 5
- 17
14
votes
7 answers
How to handle arbitrarily large integers
I'm working on a programming language, and today I got the point where I could compile the factorial function(recursive), however due to the maximum size of an integer the largest I can get is factorial(12). What are some techniques for handling…

Alex Gaynor
- 14,353
- 9
- 63
- 113
14
votes
6 answers
How do I get real integer overflows in MATLAB/Octave?
I'm working on a verification-tool for some VHDL-Code in MATLAB/Octave. Therefore I need data types which generate "real" overflows:
intmax('int32') + 1
ans = -2147483648
Later on, it would be helpful if I can define the bit width of a variable,…

marvin2k
- 1,573
- 2
- 14
- 18
14
votes
5 answers
Checking for underflow/overflow in C++?
Is there a general way to check for an overflow or an underflow of a given data type (uint32, int etc.)?
I am doing something like this:
uint32 a,b,c;
... //initialize a,b,c
if(b < c) {
a -= (c - b)
}
When I print a after some iterations, it…

Legend
- 113,822
- 119
- 272
- 400
14
votes
3 answers
"Simulate" a 32-bit integer overflow in JavaScript
JavaScript can handle the following Math just fine:
var result = (20000000 * 48271) % 0x7FFFFFFF;
But in some programming languages, that first int*int multiplication results in a value too large to hold in a standard 32 bit integer. Is there any…

IQAndreas
- 8,060
- 8
- 39
- 74
14
votes
2 answers
Overflow when multiplying Integers and assigning to Long
If I type the following into the immediate window I get Runtime error '6': Overflow.
MsgBox 24 * 60 * 60
Why is this?
This also fails:
Dim giveTime As Long
giveTime = 24 * 60 * 60
Why is this? giveTime is declared as a Long type, so 24 × 60…

Excel_Cowboy_Yeeha
- 143
- 1
- 6
14
votes
2 answers
Is there a safe way to get the unsigned absolute value of a signed integer, without triggering overflow?
Consider a typical absolute value function (where for the sake of argument the integral type of maximum size is long):
unsigned long abs(long input);
A naive implementation of this might look something like:
unsigned long abs(long input)
{
if…

Billy ONeal
- 104,103
- 58
- 317
- 552
13
votes
2 answers
Do C99 signed integer types defined in stdint.h exhibit well-defined behaviour in case of an overflow?
All operations on "standard" signed integer types in C (short, int, long, etc) exhibit undefined behaviour if they yield a result outside of the [TYPE_MIN, TYPE_MAX] interval (where TYPE_MIN, TYPE_MAX are the minimum and the maximum integer value…

Alexandros
- 3,044
- 1
- 23
- 37
13
votes
3 answers
Why doesn't compound assignment in Java catch overflow problems?
To my shock, it turns out that the following code will compile without even warnings:
public void test()
{
int value = 2000000000;
long increment = 1000000000;
value += increment;
}
Whereas this gives a compile-time error, as you would…

Hakanai
- 12,010
- 10
- 62
- 132
13
votes
1 answer
Do the C# and Java specifications spell out the same behavior on signed integer overflow?
In C and C++, the behavior of signed integer overflow or underflow is undefined.
In Java and C# (unchecked contexts), the behavior seems to be defined to an extent.
From the Java specification, we have:
The integer operators do not indicate…

Theodoros Chatzigiannakis
- 28,773
- 8
- 68
- 104