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
47
votes
5 answers
How can I detect integer overflow on 32 bits int?
I know such topic was asked several times, but my question is about overflow on full 32 bits of int. For example:
11111111111111111111111111111111 +
00000000000000000000000000000001 =
00000000000000000000000000000000 //overflow!
I found…

ashur
- 4,177
- 14
- 53
- 85
42
votes
4 answers
Is there some meaningful statistical data to justify keeping signed integer arithmetic overflow undefined?
The C Standard explicitly specifies signed integer overflow as having undefined behavior. Yet most CPUs implement signed arithmetics with defined semantics for overflow (except maybe for division overflow: x / 0 and INT_MIN / -1).
Compilers writers…

chqrlie
- 131,814
- 10
- 121
- 189
38
votes
2 answers
Panicked at 'attempt to subtract with overflow' when cycling backwards though a list
I am writing a cycle method for a list that moves an index either forwards or backwards. The following code is used to cycle backwards:
(i-1)%list_length
In this case, i is of the type usize, meaning it is unsigned. If i is equal to 0, this leads…

lmartens
- 1,432
- 2
- 12
- 19
35
votes
9 answers
why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?
System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE);
is true.
I understand that integer in Java is 32 bit and can't go above 231-1, but I can't understand why adding 1 to its MAX_VALUE results in MIN_VALUE and not in some kind of…

Oleg Mikheev
- 17,186
- 14
- 73
- 95
30
votes
4 answers
Why is (18446744073709551615 == -1) true?
When I was working on string::npos I noticed something and I couldn't find any explanation for it on the web.
(string::npos == ULONG_MAX)
and
(string::npos == -1)
are true.
So I tried this:
(18446744073709551615 == -1)
which is also true.
How…

Bahadır
- 454
- 1
- 4
- 8
29
votes
2 answers
Equivalent of atoi for unsigned integers
I'm doing two operations involving atoi and I'm wondering how I can do this with unsigned integers because atoi seems to convert these to signed causing a wraparound integer overflow. I want to work with 32bit unsigned integers but atoi is limiting…

John
- 341
- 1
- 6
- 13
29
votes
3 answers
How to check for signed integer overflow in C without undefined behaviour?
There's (1):
// assume x,y are non-negative
if(x > max - y) error;
And (2):
// assume x,y are non-negative
int sum = x + y;
if(sum < x || sum < y) error;
Whichs is preferred or is there a better way.

Ganker
- 329
- 1
- 3
- 5
28
votes
3 answers
Why is this long overflowing to -1, instead of the minimum value for the type?
I have the following code that returns the number of nodes in a tree when a complete Binary Tree is layer layers tall:
public static long nNodesUpToLayer(int layer) {
if (layer < 0) throw new IllegalArgumentException(
"The layer…

Carcigenicate
- 43,494
- 9
- 68
- 117
28
votes
3 answers
Can a non-empty string have a hashcode of zero?
By "non-empty", I mean in this question a string which contains at least one non-zero character.
For reference, here's the hashCode implementation :
1493 public int hashCode() {
1494 int h = hash;
1495 if (h == 0) {
1496 …

Denys Séguret
- 372,613
- 87
- 782
- 758
26
votes
3 answers
How to detect integer overflow in C
We know CPython promotes integers to long integers (which allow arbitrary-precision arithmetic) silently when the number gets bigger.
How can we detect overflow of int and long long in pure C?

Dean
- 427
- 1
- 4
- 8
26
votes
5 answers
What does -fwrapv do?
Can anyone provide some code examples that act differently when compiled with -fwrapv vs without?
The gcc documentation says that -fwrapv instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication…

ktal
- 263
- 1
- 3
- 4
25
votes
13 answers
Why would you want an integer overflow to occur?
In this question the topic is how to make VS check for an arithmetic overflow in C# and throw an Exception: C# Overflow not Working? How to enable Overflow Checking?
One of the comments stated something weird and got upvoted much, I hope you can…

magnattic
- 12,638
- 13
- 62
- 115
25
votes
3 answers
Subtraction between signed and unsigned followed by division
The following results make me really confused:
int i1 = 20-80u; // -60
int i2 = 20-80; // -60
int i3 =(20-80u)/2; // 2147483618
int i4 =(20-80)/2; // -30
int i5 =i1/2; // -30
i3 seems to be computed as (20u-80u)/2, instead of…

Leo Lai
- 863
- 8
- 17
24
votes
4 answers
What are arithmetic underflow and overflow in C?
What do arithmetic underflow and overflow mean in C programming?

Registered User
- 5,173
- 16
- 47
- 73
23
votes
3 answers
Integer overflow in Python3
I'm new to Python, I was reading this page where I saw a weird statement:
if n+1 == n: # catch a value like 1e300
raise OverflowError("n too large")
x equals to a number greater than it?! I sense a disturbance in the Force.
I know that in…

klenium
- 2,468
- 2
- 24
- 47