Questions tagged [integer-overflow]

Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.

1056 questions
731
votes
30 answers

How do I detect unsigned integer overflow?

I was writing a program in C++ to find all solutions of ab = c, where a, b and c together use all the digits 0-9 exactly once. The program looped over values of a and b, and it ran a digit-counting routine each time on a, b and ab to check if the…
Chris Johnson
  • 10,469
  • 4
  • 31
  • 35
266
votes
12 answers

How does Java handle integer underflows and overflows and how would you check for it?

How does Java handle integer underflows and overflows? Leading on from that, how would you check/test that this is occurring?
KushalP
  • 10,976
  • 6
  • 34
  • 27
266
votes
7 answers

Why is unsigned integer overflow defined behavior but signed integer overflow isn't?

Unsigned integer overflow is well defined by both the C and C++ standards. For example, the C99 standard (§6.2.5/9) states A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting…
anthonyvd
  • 7,329
  • 4
  • 30
  • 51
252
votes
4 answers

(-2147483648> 0) returns true in C++?

-2147483648 is the smallest integer for integer type with 32 bits, but it seems that it will overflow in the if(...) sentence: if (-2147483648 > 0) std::cout << "true"; else std::cout << "false"; This will print true in my testing. However,…
benyl
  • 2,097
  • 2
  • 13
  • 7
193
votes
36 answers

Unexpected results when working with very big integers on interpreted languages

I am trying to get the sum of 1 + 2 + ... + 1000000000, but I'm getting funny results in PHP and Node.js. PHP $sum = 0; for($i = 0; $i <= 1000000000 ; $i++) { $sum += $i; } printf("%s", number_format($sum, 0, "", "")); //…
Baba
  • 94,024
  • 28
  • 166
  • 217
161
votes
15 answers

How to avoid overflow in expr. A * B - C * D

I need to compute an expression which looks like: A*B - C*D, where their types are: signed long long int A, B, C, D; Each number can be really big (not overflowing its type). While A*B could cause overflow, at same time expression A*B - C*D can be…
NGix
  • 2,542
  • 8
  • 28
  • 39
154
votes
6 answers

Why does long long n = 2000*2000*2000*2000; overflow?

long long int n = 2000*2000*2000*2000; // overflow long long int n = pow(2000,4); // works long long int n = 16000000000000; // works Why does the first one overflow (multiplying integer literal constants to assign to a long…
Fabbiucciello
  • 1,514
  • 2
  • 4
  • 9
131
votes
9 answers

Why does Java think that the product of all numbers from 10 to 99 is 0?

The following block of codes gives the output as 0. public class HelloWorld{ public static void main(String []args){ int product = 1; for (int i = 10; i <= 99; i++) { product *= i; } …
Aniruddha Sarkar
  • 1,903
  • 2
  • 14
  • 15
117
votes
15 answers

How can I check if multiplying two numbers in Java will cause an overflow?

I want to handle the special case where multiplying two numbers together causes an overflow. The code looks something like this: int a = 20; long b = 30; // if a or b are big enough, this result will silently overflow long c = a * b; That's a…
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
110
votes
8 answers

Efficient unsigned-to-signed cast avoiding implementation-defined behavior

I want to define a function that takes an unsigned int as argument and returns an int congruent modulo UINT_MAX+1 to the argument. A first attempt might look like this: int unsigned_to_signed(unsigned n) { return static_cast(n); } But as…
Nemo
  • 70,042
  • 10
  • 116
  • 153
107
votes
6 answers

(A + B + C) ≠ (A + C + B​) and compiler reordering

Adding two 32-bit integers can result an integer overflow: uint64_t u64_z = u32_x + u32_y; This overflow can be avoided if one of the 32-bit integers is first casted or added to a 64-bit integer. uint64_t u64_z = u32_x + u64_a + u32_y; However, if…
Tal
  • 1,759
  • 1
  • 12
  • 22
98
votes
13 answers

Detecting signed overflow in C/C++

At first glance, this question may seem like a duplicate of How to detect integer overflow?, however it is actually significantly different. I've found that while detecting an unsigned integer overflow is pretty trivial, detecting a signed overflow…
Channel72
  • 24,139
  • 32
  • 108
  • 180
95
votes
3 answers

Is signed integer overflow still undefined behavior in C++?

As we know, signed integer overflow is undefined behavior. But there is something interesting in C++11 cstdint documentation: signed integer type with width of exactly 8, 16, 32 and 64 bits respectively with no padding bits and using 2's complement…
Archie
  • 6,391
  • 4
  • 36
  • 44
94
votes
14 answers

Catch and compute overflow during multiplication of two large integers

I am looking for an efficient (optionally standard, elegant and easy to implement) solution to multiply relatively large numbers, and store the result into one or several integers : Let say I have two 64 bits integers declared like this : uint64_t a…
Ben
  • 7,372
  • 8
  • 38
  • 46
89
votes
5 answers

Java Integer compareTo() - why use comparison vs. subtraction?

I've found that java.lang.Integer implementation of compareTo method looks as follows: public int compareTo(Integer anotherInteger) { int thisVal = this.value; int anotherVal = anotherInteger.value; return (thisVal
Vladimir
  • 12,753
  • 19
  • 62
  • 77
1
2 3
70 71