Re: Adding large numbers in C



In article <1141113177.514386.234530@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Rajesh <rajesh.nagpal@xxxxxxxxx> wrote:
Hello Walter,

I didn't got the following logic from your last post :

"it would overflow, calculate what the result of the addition would be
mod 2 to the number of bits in the chunk"

Are you familiar with modulo arithmetic, such as is provided by
the C operator symbolized by the the percent sign (%) ?

Do you have a good C reference book that describes -exactly- what
happens when you do addition of unsigned integral values?


Also how to check whether the result of an operation would
overflow.......we can't keep the result in an unsigned long variable
and check it's value as it would have been wrapped around. Is there any
way to know that without actual addition?

Some elementary algebra:

If (X+Y) > ULONG_MAX then Y > (ULONG_MAX - X)

No matter what value X has in the unsigned long range, ULONG_MAX - X is
certain to be representable as an unsigned long. Substitute in
X as 0 and as ULONG_MAX to see that this is true.

(There happens to be a shortcut for this test -- but first you
have to know -exactly- what happens when you do additions of unsigned long.)
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
.



Relevant Pages