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++? 4 9948
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
On Aug 20, 10:24 pm, "Alf P. Steinbach" <al...@start.no wrote:
* Neelesh Bodas:
There are no overflows possible for unsigned integer arithmetic. The
question of "underflow" doesnot arise since it is "unsigned"
arithmetic.
Just a nit, but at least as far as the terminology I've been exposed to
(for some decades), "underflow" is strictly a non-integer representation
phenomenon. Integers just overflow, whether that's towards positive or
negative infinity. I.e., "overflow" and "underflow" refer to the
absolute value, not the sign.
Agreed. (But had to refer to wikipedia for that !!). so that would
make a very small change in my reply:
The question of "underflow" doesnot arise since it is unsigned
"integer" arithmetic
(Just a slip-of-quote) :)
-N
Alf P. Steinbach wrote:
* Raymond:
>Source: http://moryton.blogspot.com/2007/08/...flow-when.html
Does it touch any implementation defined or undefined behaviour, or was 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.
Victor Bazarov wrote:
Raymond wrote:
>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.
Yes.
>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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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;
|
by: Neil Zanella |
last post by:
Hello,
Often I happen to be dealing with nonnegative integers and since I know I won't
need negative numbers here I declare them as unsigned simply to make the program
somewhat clearer. Effectively, though, signed integers would often work too since
unless I am doing some modular arithmetic modulo the word length then I almost
never need to use the high bit since the integers I deal with are usually not
that large, and I would assume...
|
by: Robert Seacord |
last post by:
The CERT/CC has released a beta version of a secure integer library for
the C Programming Language. The library is available for download from
the CERT/CC Secure Coding Initiative web page at:
http://www.cert.org/secure-coding/
The purpose of this library is to provide a collection of utility
functions that can assist software developers in writing C programs that
are free from common integer problems such as integer overflow, integer...
|
by: subramanian100in |
last post by:
Suppose
unsigned int size = UINT_MAX;
Now consider
++size;
After increment operator, size value becomes zero in
| |
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*/
|
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.
--
|
by: thomas.mertes |
last post by:
Is it possible to use some C or compiler extension to catch
integer overflow?
The situation is as follows:
I use C as target language for compiled Seed7 programs.
For integer computions the C type 'long' is used.
That way native C speed can be reached.
Now I want to experiment with raising a Seed7 exception
(which is emulated with setjmp(), longjmp() in C) for integer
|
by: Chris Becke |
last post by:
I *know* my CPU has opcodes that can do this - when adding (or subtracting) there is a carry flag that can be invoked to make the result essentially 1 bit longer than the data size used in calculations.
When multiplying two numbers, the CPU automatically returns a double width result.
c & c++ give programmers these bloody ridiculous integer types of undetermined size and no really optimal way to even build a variable width number library...
|
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...
|
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...
| |
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |