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
20
votes
5 answers

Is it possible to access the overflow flag register in a CPU with C++?

After performing a mathematical operation, for say, multiplying two integers, is it possible to access the overflow flag register in a CPU with C++ ? If not what are other fast ways to check for an overflow ?
Loers Antario
  • 1,611
  • 6
  • 20
  • 24
19
votes
3 answers

Incrementing an integer value beyond its integer limit - C#

I've a for loop which keeps incrementing an integer value till the loop completes. So if the limit n is a double variable and the incremented variable 'i' is an integer, i gets incremented beyond its limits. double total = 0; double number =…
NLV
  • 21,141
  • 40
  • 118
  • 183
19
votes
4 answers

Wrap around explanation for signed and unsigned variables in C?

I read a bit in C spec that unsigned variables(in particular unsigned short int) perform some so called wrap around on integer overflow, although I couldn't find anything on signed variables except that I left with undefined behavior. My professor…
orustammanapov
  • 1,792
  • 5
  • 25
  • 44
18
votes
4 answers

Warning : overflow in implicit constant conversion

In the following program, the line 5 does give overflow warning as expected, but surprisingly the line 4 doesn't give any warning in GCC: http://www.ideone.com/U0BXn int main() { int i = 256; char c1 = i; //line 4 char c2 = 256; …
Nawaz
  • 353,942
  • 115
  • 666
  • 851
18
votes
4 answers

How does overflow work in java?

I've read about overflow, I know that "Overflow is when a number is so large that it will no longer fit within the data type, so the system “wraps around” to the next lowest value and counts up from there". For example: short s = (short)1921222; //…
Luisk4
  • 377
  • 1
  • 5
  • 12
18
votes
2 answers

Swift: How to disable integer overflow / underflow traps for a function

I'm importing some old C code into a swift project, and porting it across to pure swift code. Some of it does "encryption" wherein it does something like let a = UInt8(x) // e.g. 30 let b = a - 237 In C this just underflows and wraps around, which…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
18
votes
11 answers

How to deal with a wrapping counter in embedded C

I need to deal with a counter that gives me ticks for my application. The counter is 32bits so what I need to know is how to deal with it when it wraps. for example: I have a function that returns a (timestamp + shifttime) and I have another…
jramirez
  • 8,537
  • 7
  • 33
  • 46
18
votes
2 answers

Integer division overflows

The Problem I have been thinking about integer (type int) overflows, and it occurs to me that division could overflow. Example: On my current platform, I have INT_MIN == -INT_MAX - 1 and thus INT_MIN < -INT_MAX and thus INT_MIN / -1 > -INT_MAX /…
Anon
  • 265
  • 3
  • 7
18
votes
5 answers

Are fixed-width integers distributive over multiplication?

For three n-bit signed integers a, b, and c (such as 32-bit), is it always true that a * (b + c) == (a * b) + (a * c), taking into account integer overflow? I think this is language-independent, but if it's not, I'm specifically interested in the…
Taymon
  • 24,950
  • 9
  • 62
  • 84
17
votes
1 answer

How to compile and run an optimized Rust program with overflow checking enabled

I'm writing a program that's quite compute heavy, and it's annoyingly slow to run in debug mode. My program is also plagued by integer overflows, because I'm reading data from u8 arrays and u8 type spreads to unexpected places via type inference,…
Kornel
  • 97,764
  • 37
  • 219
  • 309
17
votes
6 answers

Overflow occurs with multiplication

long m = 24 * 60 * 60 * 1000 * 1000; The above code creates overflow and doesn't print the correct result. long m2 = 24L * 60 * 60 * 1000 * 1000; long m3 = 24 * 60 * 60 * 1000 * 1000L; The above 2 lines print the correct result. My questions…
rents
  • 768
  • 1
  • 7
  • 22
17
votes
6 answers

Modular Exponentiation for high numbers in C++

So I've been working recently on an implementation of the Miller-Rabin primality test. I am limiting it to a scope of all 32-bit numbers, because this is a just-for-fun project that I am doing to familiarize myself with c++, and I don't want to…
Axel Magnuson
  • 1,192
  • 1
  • 10
  • 26
16
votes
6 answers

detecting multiplication of uint64_t integers overflow with C

Is there any efficient and portable way to check when multiplication operations with int64_t or uint64_t operands overflow in C? For instance, for addition of uint64_t I can do: if (UINT64_MAX - a < b) overflow_detected(); else sum = a + b; But I…
salva
  • 9,943
  • 4
  • 29
  • 57
16
votes
2 answers

How does one trap arithmetic overflow errors in Swift?

This one is probably easy. We know that the operator &+ does modular arithmetic on integers (wraps around), while the operator + causes an error. $ swift 1> var x: Int8 = 100 x: Int8 = 100 2> x &+ x $R0: Int8 = -56 3> x + x Execution…
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
15
votes
6 answers

How does one safely static_cast between unsigned int and int?

I have an 8-character string representing a hexadecimal number and I need to convert it to an int. This conversion has to preserve the bit pattern for strings "80000000" and higher, i.e., those numbers should come out negative. Unfortunately, the…
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125