By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 448,603 Members | 1,546 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,603 IT Pros & Developers. It's quick & easy.

# Unsigned integer overflow detection

 P: n/a Source: http://moryton.blogspot.com/2007/08/...flow-when.html Example from source: char unsigned augend (255); char unsigned const addend (255); char unsigned const sum (augend + addend); if (sum < augend) { std::puts ("Overflowed!"); } sum = augend + addend sum = 255 + 255 sum = 510 modulo 256 // Behind the scenes. sum = 254 Does it touch any implementation defined or undefined behaviour, or was that specific to signed integers (on some platforms)? What other methods are there for detecting unsigned integer overflow and/or underflow in C++? Aug 20 '07 #1
Share this Question
4 Replies

 P: n/a Raymond wrote: Source: http://moryton.blogspot.com/2007/08/...flow-when.html Example from source: char unsigned augend (255); char unsigned const addend (255); char unsigned const sum (augend + addend); if (sum < augend) { std::puts ("Overflowed!"); } sum = augend + addend sum = 255 + 255 sum = 510 modulo 256 // Behind the scenes. sum = 254 Does it touch any implementation defined or undefined behaviour, or was that specific to signed integers (on some platforms)? No, the behaviour is well-defined. All arithmetic operations with unsigned values work modulo 2^N, where N is the number of bits in the representation of the number. > What other methods are there for detecting unsigned integer overflow and/or underflow in C++? If you look in the archives (use Google Groups interface, e.g.), you should find that unsigned does not overflow. There is no such thing as "underflow" AFA integers are concerned, IIUIC. There is no way to "detect" it in the current language. There is only a way to "predict" it: unsigned a = UINT_MAX - 2, b = UINT_MAX - 3; // or whatever if (UINT_MAX - a < b) std::cout << "Adding " << a << " to " << b << " would \"overflow\"\n"; V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask Aug 20 '07 #2

 P: n/a On Aug 20, 10:24 pm, "Alf P. Steinbach"

 P: n/a Alf P. Steinbach wrote: * Raymond: >Source:http://moryton.blogspot.com/2007/08/...flow-when.htmlDoes it touch any implementation defined or undefined behaviour, orwas that specific to signed integers (on some platforms)? In C++ unsigned integer arithmetic is defined as modulo 2^n, where n is the number of bits. Yes, correct, but in my haste I left out *why* I felt unsure/asked these questions, and I think I found the part that I had read before; section 1.9 15, where overflows triggering exceptions are mentioned with regards to integers, or signed integers if the example is to be taken literally. If you want range checking you can check if your compiler provides range checking for signed integer types, or you can implement a range-checked integer type as a class, like Quite possibly there are existing such classes freely available on the net -- Google (and please report results of that search here! :-) ). There is only so much time one can spend searching before beginning to think again.. Anyway, thanks, but I think you made this a lot more complex than it needed to be, at least for me. Existing classes? Perhaps, but for such a small problem, that doesn't sound like the solution for me. It wasn't easy for me to find something concrete on this subject, and I tried finding other solutions after this one. The paper on Stroustrup's page, also linked to from the article, only dealt with signed integers, as your example did. Aug 20 '07 #4

 P: n/a Victor Bazarov wrote: Raymond wrote: >Does it touch any implementation defined or undefined behaviour, orwas that specific to signed integers (on some platforms)? No, the behaviour is well-defined. All arithmetic operations with unsigned values work modulo 2^N, where N is the number of bits in the representation of the number. Yes. >What other methods are there for detecting unsigned integer overflowand/or underflow in C++? If you look in the archives (use Google Groups interface, e.g.), you should find that unsigned does not overflow. There is no such thing as "underflow" AFA integers are concerned, IIUIC. My current point of view on this; an overflow comes from adding too much; an "underflow" comes from subtracting too much. An overflow in the general sense does not explain whether too much was added or subtracted, just that information was lost; it doesn't include how it happened. There is no way to "detect" it in the current language. There is only a way to "predict" it: unsigned a = UINT_MAX - 2, b = UINT_MAX - 3; // or whatever if (UINT_MAX - a < b) std::cout << "Adding " << a << " to " << b << " would \"overflow\"\n"; What do you mean? The source code I linked to detects it fine, and it appears to be fully portable. No need to think about the underlying binary interpretation of signed integers, or exceptions either apparently. Aug 20 '07 #5

### This discussion thread is closed

Replies have been disabled for this discussion.