473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to ignore/catch unsigned int calculation overflow

I have some algorithm dealing with unsigned int calculation a lot, I am
trying to add some check for overflow. The initial thinking was very
easy, just do something like this,

int addition(unsigned int i1, unsigned int i2) {
if ( i1 + i2 < i1 || i1 + i2 < i2 ) {
...throw some expception...
}
return i1+i2;
}

I also define a minus function doing similar thing, but for simplicity,
I only show addition functin here.

But it does not work as I expected. For example I have the following
code,

unsigned int base=0xFFFF0000
unsigned len=0x10000
unsigned int high= addition(base,len)-1;

you can see that an exception is thrown since 0xFFFF0000 + 0x10000 is
0x0, but it is what I expect.

i know I can adjust the above equation to addition(base-1, len), but
for the real code, there are a lot of really long plus and minus mixed
calculation. I guess there is no universal rule to break addition and
minus.

So the question is, for addition-minus-mixed calculation, is there any
way to ignore overflow in the middle and only catch it at the end?

Mar 21 '06 #1
3 4549
li*****@hotmail.com wrote:

So the question is, for addition-minus-mixed calculation, is there any
way to ignore overflow in the middle and only catch it at the end?


Do it at higher precision. Use, say, unsigned long or unsigned long ong
(assuming one of them's larger than unsigned int), and if the result is
too big for an int then you've got an overflow.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 21 '06 #2
li*****@hotmail.com wrote:
I have some algorithm dealing with unsigned int calculation a lot, I am
trying to add some check for overflow.
Tricky, as unsigned int technically doesn't overflow in C++. It's just
integer
arithmetic modulo (UINT_MAX+1).
The initial thinking was very easy, just do something like this,

int addition(unsigned int i1, unsigned int i2) {
if ( i1 + i2 < i1 || i1 + i2 < i2 ) {
...throw some expception...
}
return i1+i2;
}

I also define a minus function doing similar thing, but for simplicity,
I only show addition functin here.

But it does not work as I expected. For example I have the following
code,

unsigned int base=0xFFFF0000
unsigned len=0x10000
unsigned int high= addition(base,len)-1;

you can see that an exception is thrown since 0xFFFF0000 + 0x10000 is
0x0, but it is what I expect.
OTOH, addition(0,0)-1 doesn't cause an exception (operator- doesn't
check)
but it should.
i know I can adjust the above equation to addition(base-1, len), but
for the real code, there are a lot of really long plus and minus mixed
calculation. I guess there is no universal rule to break addition and
minus. So the question is, for addition-minus-mixed calculation, is there any
way to ignore overflow in the middle and only catch it at the end?


Not with the built-in unsigned ints. They simply don't have enough bits
to
store both a value and the sequence of operations that resulted in that
value.
Simply said, all unsigned ints with all bits 0 are equal to 0U, even if
some
were the result of an overflow.

The best way to solve it is to use an intermediary type which does not
overflow.

HTH,
Michiel Salters

Mar 21 '06 #3
li*****@hotmail.com wrote:
So the question is, for addition-minus-mixed calculation, is there any
way to ignore overflow in the middle and only catch it at the end?


How about a class containing the unsigned int and a sticky overflow
flag. You can test the flag when you want, or throw in the conversion
to unsigned int if the flag is set.
Mar 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
2
by: Rahul | last post by:
Hi, I have a little program as follows : =================== STARTS HERE ================ #include <stdio.h> void f (unsigned long); int main() {
26
by: LuB | last post by:
This isn't a C++ question per se ... but rather, I'm posting this bcs I want the answer from a C++ language perspective. Hope that makes sense. I was reading Peter van der Linden's "Expert C...
42
by: sarathy | last post by:
Hi, I need clarification regarding signed characters in the C language. In C, char is 1 byte. So 1. Unsigned char - ASCII CHARACTER SET - EXTENDED CHARACTER SET
26
by: John Harrison | last post by:
I have a problem. I want to compare an integral value, n, against three half open ranges as follows [-A, 0) // range 1 [0, B) // range 2 [B, C} // range 3 Each range corresponds to a...
17
by: Tarique | last post by:
This program was compiled on MS Visual C++ 08 /*Fibonacci Numbers*/ #include<stdio.h> #include<limits.h> void fibonacci(int n) { unsigned long long fib0 = 0; /*First Fibonacci Number*/
105
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
13
by: Urs Thuermann | last post by:
I wasn't able to find an answer to the following questions in a draft version of IOS 9899-99. What is the semantic when you decrement an unsigned int below 0 (or subtract two unsigned ints with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.