20
votes
13answers
2k views

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? One of the comments stated something weird and got upvoted much, I ...
15
votes
5answers
7k views

How do I detect overflow while multiplying two 2's complement integers?

I want to multiply two numbers, and detect if there was an overflow. What is the simplest way to do that?
10
votes
9answers
9k views

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 ...
10
votes
6answers
4k views

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 ...
8
votes
10answers
5k views

Java multiply operation behavior

I wrote a method to convert a given number from days to milliseconds: private long expireTimeInMilliseconds; ... public void setExpireTimeInDays(int expireTimeInDays) { expireTimeInMilliseconds = ...
8
votes
4answers
5k views

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 -= ...
7
votes
3answers
925 views

Clojure - Calculate with big numbers

I want to calculate !1000 in clojure, how can I do this without getting a integer-overflow exception? My factorial code is right now: (reduce * (range 1 1001)).
7
votes
4answers
567 views

Permutation with Repetition: Avoiding Overflow

Background: Given n balls such that: 'a' balls are of colour GREEN 'b' balls are of colour BLUE 'c' balls are of colour RED ... (of course a + b + c + ... = n) The number of permutations in which ...
7
votes
5answers
19k views

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; ...
7
votes
4answers
1k views

No useful and reliable way to detect integer overflow in C/C++?

Possible Duplicate: Best way to detect integer overflow in C/C++ No, this is not a duplicate. The issue is the same but the question is different. The gcc compiler can optimize away an ...
6
votes
4answers
2k views

C integer overflow behaviour when assigning to larger-width integers

If I execute the following code in C: #include <stdint.h> uint16_t a = 4000; uint16_t b = 8000; int32_t c = a - b; printf("%d", c); It correctly prints '-4000' as the result. However, I'm ...
5
votes
4answers
310 views

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 ...
3
votes
2answers
536 views

What's an efficient way to avoid integer overflow converting an unsigned int to int in C++?

Is the following an efficient and problem free way to convert an unsigned int to an int in C++: #include <limits.h> void safeConvert(unsigned int passed) { int variable = ...
2
votes
2answers
1k views

Overflow In c

I have a doubt When two 16 bit values are added with max values, will there be overflow in the 16 bit machines? I will elaborate unsigned short a; unsigned short b; unsigned long c; c=(unsigned ...
2
votes
3answers
59 views

Signed integers' undefined behavior and Apple Secure Coding Guide

Apple Secure Coding Guide says the following (page 27): Also, any bits that overflow past the length of an integer variable (whether signed or unsigned) are dropped. However, regards to signed ...
0
votes
2answers
171 views

G++ allows for implicit long-to-int truncation on x64

I compile this code with g++ -Wall, get no warnings/errors: #include "stdio.h" int main() { long x = 1000000000000; int y = x; printf("%ld %d\n", x, y); return 0; } ...