Home

Neelesh Bodas wrote:
> On Aug 20, 9:56 pm, Raymond <raym...@nosuch.gov> wrote:
>> Source:http://moryton.blogspot.com/2007/08/detecting-overflo wunderflow-when....
>>
>> Does it touch any implementation defined or undefined behaviour, or was
>> that specific to signed integers (on some platforms)?
>>
> 3.9.1(4): Unsigned integers, declared unsigned, shall obey the laws of
> arithmetic modulo 2^n where n is the number of bits in the value
> representation of that particular size of integer
>
> Footnote: This implies that unsigned arithmetic does not overflow
> because a result that cannot be represented by the resulting unsigned
> integer type is reduced modulo the number that is one greater than the
> largest value that can be represented by the resulting unsigned
> integer
> type.

Yes.

> Thus the only implementation-defined aspect in your example is the
> value of 'n'. A 'char' is not necessarily 8 bit entity, and hence n
> neednot be 8.

As mentioned in the article too, I might add, but we're busy people. :)

>> What other methods are there for detecting unsigned integer overflow
>> and/or underflow in C++?
>
> There are no overflows possible for unsigned integer arithmetic. The
> question of "underflow" doesnot arise since it is "unsigned"
> arithmetic.

Yes, but if you wanted to detect an overflow, then there is an overflow,
because it is now defined as adding too much, resulting in loss of
information due to a limited container. Same goes for underflow.

previous
next

Re: help understanding iterators with vector::insert
Re: Comprehensive online C++ library reference?
Re: which language allows you to change an argument's value?
Re: Getting rid of bitwise operators in Python 3?
Re: accessing keys in dict
Kidprotect
Pajacyk
Krwinka
Mam Marzenie
Akogo