473,769 Members | 6,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(unsign ed 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,l en)-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 4587
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(unsign ed 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,l en)-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
2246
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. The reference should be 3.9.1 (Fundamental types), and 4.7 (Integral conversions). It seems to me that the Standard doesn't specify: 1) The "value representation" of any of these types, except that (3.9.1/3) "... The range of nonnegative...
34
16690
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 - 3;
2
2136
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
36087
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 Programming: Deep C Secrets" and came across the following statement: "Avoid unnecessary complexity by minimizing your use of unsigned types. Specifically, don't use an unsigned type to represent a quantity just because it will never be negative...
42
7404
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
2777
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 different outcome and if the integral value isn't within any of the ranges, that's a fourth outcome. So far so easy, the problem is that n is a signed quantity and A, B, C are unsigned
17
7564
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
6223
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
10712
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 negative result)? AFAIR, this results in implementation-defined behavior but not undefined behavior. Is that correct? So what does the standard say to this code? void f(void) {
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.