unsigned __int64 overflow problem

I am trying to find an elegant solution for the "unsigned __int64
overflow problem" or in general an "unsigned int overflow detection
problem". In particular, I just have written a little program that
calculates "aliquot sequences" using unsigned __int64. That gives me
integers up to 19 digits and that's all I need for now, but the
sequences often grow fast beyond the _UI64_MAX (maximum value of
__int64) and in that case, the __int64 variables assume incorrect
values (pretty much, garbage).

I was wondering if there is an elegant way, not crucially affecting
the calculation speed, of detecting if an unsigned __int64 (or any
other unsigned int) variable "got over the limit".

Advertisements

On 24 Aug 2003 22:46:30 -0700, (Vivi) wrote in
comp.lang.c++:
> I am trying to find an elegant solution for the "unsigned __int64
> overflow problem" or in general an "unsigned int overflow detection
> problem". In particular, I just have written a little program that
> calculates "aliquot sequences" using unsigned __int64. That gives me
> integers up to 19 digits and that's all I need for now, but the
> sequences often grow fast beyond the _UI64_MAX (maximum value of
> __int64) and in that case, the __int64 variables assume incorrect
> values (pretty much, garbage).
>
> I was wondering if there is an elegant way, not crucially affecting
> the calculation speed, of detecting if an unsigned __int64 (or any
> other unsigned int) variable "got over the limit".
>
> Thanks, Vivi

Note that __int64 is not a standard C++ data type. It is an
acceptable representation for the C "long long" type which will
probably be added to the next version of the C++ standard, but for now
it is not a part of the language.

If/when "long long" is added to the C++ language, the unsigned version
will have exactly the same behavior as all of the other C and C++
unsigned types. Specifically, they can't overflow. If a calculation
or conversion produces a value outside the range of the unsigned type,
it is modified by repeatedly adding or subtracting one mare than the
type's maximum value until it is within range. This required behavior
is well-defined and guaranteed.

Essentially, that means you get the low 64 bits of the result, any
carry to higher bits is merely discarded.

As to whether or not you can detect this after the fact, you can do
this fairly easily for addition and subtraction for the unsigned
types:

Advertisements

"Vivi" <> wrote in message
news:...
> I am trying to find an elegant solution for the "unsigned __int64
> overflow problem" or in general an "unsigned int overflow detection
> problem". In particular, I just have written a little program that
> calculates "aliquot sequences" using unsigned __int64. That gives me
> integers up to 19 digits and that's all I need for now, but the
> sequences often grow fast beyond the _UI64_MAX (maximum value of
> __int64) and in that case, the __int64 variables assume incorrect
> values (pretty much, garbage).
>
> I was wondering if there is an elegant way, not crucially affecting
> the calculation speed, of detecting if an unsigned __int64 (or any
> other unsigned int) variable "got over the limit".
>
> Thanks, Vivi

A natural way would be to check if it overfloww before
you actually carry out the operation.
If you are concerned about only in unsigned integers,

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!