integer-overflow

Why don't languages raise errors on integer overflow by default?

In several modern programming languages (including C++, Java, and C#), the language allows integer overflow to occur at runtime without raising any kind of error condition. For example, consider this (contrived) C# method, which does not account for the possibility of overflow/underflow. (For brevity, the method also doesn't handle the...

Can I prevent a integer overflow in C# using an unsigned right shift?

I want alwaysPositive to be assigned a positive number with all possible values for lareValue1 and largeValue2 (these are at least 1). The following statement causes a buffer overflow: int alwaysPositive = (largeValue1 + largeValue2) / 2; I know I can prevent it by substracting and adding: int alwaysPositive = largeValue1 + ((largeV...

Best way to detect integer overflow in C/C++

I was writing a program in C++ to find all solutions of a^b = c (a to the power of b), where a, b and c together use all the digits 0-9 exactly once. The program looped over values of a and b, and ran a digit-counting routine each time on a, b and a^b to check if the digits condition was satisfied. However, spurious solutions can be gen...

overflows in size_t additions

I like to have my code warning free for VS.NET and GCC, and I like to have my code 64 bit ready. Today I wrote a little module that deals with in memory buffers and provides access to the data via a file-style interface (e.g. you can read bytes, write bytes, seek around ect.). As the data-type for current read position and size I used ...

Force PHP integer overflow.

We have some integer arithmetic which for historical reasons has to work the same on PHP as it does in a few statically typed languages. Since we last upgraded PHP the behavior for overflowing integers has changed. Basically we are using following formula: function f($x1, $x2, $x3, $x4) { return (($x1 + $x2) ^ $x3) + $x4; } Howev...

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 integers of an arbitrary maximum size. The language currently works by translating code to ...

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 = expireTimeInDays * 24 * 60 * 60 * 1000; } I had a hard time to figure out what I did wrong. Now my question: Is that error so obvious ? ...

Why does this integer overflow occur?

I've wrapped a dll method that has an integer as an out parameter in a web service. In testing I was finding that when I was expecting -1 I was getting 65,535 instead. I realised that the dll was using 16 bit integers and I was specifying the standard .NET 32bit integer when referencing the external dll in my code. this was quickly fixed...

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 a little confused: shouldn't there be an arithmetic overflow when subtracting a larger unsigned integer from the other? What casting rules are ...

32bit int * 32bit int = 64 bit int?

In other words does this work as expected? int32 i = INT_MAX-1; int64 j = i * i; or do I need to cast the i to 64 bit first? ...

Should I use unsigned integers for count members?

I just spent an hour tracking an integer overflow in my code, so I decided to share that -most people on here will know that of course, but perhaps I can save someone some time. So, here's the question: Should I use unsigned integers for my count class members? Answer For example, assume a class TList <T> = class private FCount : C...

Overflow In c

Hi, 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 long)(a+b); Talking about 16 bit processers the accumulator will be of 16 bit size. Will there be an overflow in the above statement...

Why do my .net Int64's behaves as if they were Int32's ?

I'm witnessing a strange behavior in a .net program : Console.WriteLine(Int64.MaxValue.ToString()); // displays 9223372036854775807, which is 2^63-1, as expected Int64 a = 256*256*256*127; // ok Int64 a = 256*256*256*128; // compile time error : //"The operation overflows at compile time in checked mode" // If i do this at runtime, I...

C++ Template for safe integer casts

I am trying to write a C++ template function that will throw a runtime exception on integer overflow in casts between different integral types, with different widths, and possible signed/unsigned mismatch. For these purposes I'm not concerned with casting from floating-point types to integral types, nor other object-to-object conversions...

Why doesn't this overflow?

Given this code: int x = 20000; int y = 20000; int z = 40000; // Why is it printing WTF? Isn't 40,000 > 32,767? if ((x + y) == z) Console.WriteLine("WTF?"); And knowing an int can hold −32,768 to +32,767. Why doesn't this cause an overflow? ...

Catching overflow of left shift of constant 1 using compiler warning?

We're writing code inside the Linux kernel so, try as I might, I wasn't able to get PC-Lint/Flexelint working on Linux kernel code. Just too many built-in symbols etc. But that's a side issue. We have any number of compilers, starting with gcc, but others also. Their warnings options have been getting stronger over time, to where they ...

How to detect overflow when subtracting two signed 32 bit numbers in C?

I've got two signed integers, and i'd like to subtract them. I need to know if it overflowed. int one; int two; int result = two-one; if (OVERFLOW) { printf("overflow"); } else { printf("no overflow"); } Something like that. Is there a good way to do this? ...

Modular Exponentiation for high numbers in C++

Hello all! 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 have to work with anything 64-bits for awhile. An added bonus is that the algori...

Best way to handle and report memory allocation errors due to integer overflow in Objective-C?

To begin with, let me say that I understand how and why the problem I'm describing can happen. I was a Computer Science major, and I understand overflow/underflow and signed/unsigned arithmetic. (For those unfamiliar with the topic, Apple's Secure Coding Guide discusses integer overflow briefly.) My question is about reporting and recov...

C++ long overflowing prematurely

Hello there, 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 [-32767,32767] is mapped to [-1.0,1.0]. Where it stumbles is with larger arguments representing floats greater than 1.0: inline ...