Re: integer overflow

From: Peter Nilsson (airia_at_acay.com.au)
Date: 04/17/04


Date: Sat, 17 Apr 2004 11:27:40 +1000


"Ashutosh Iddya" <ashutosh.iddya@news.edu.au> wrote in message
news:407fba3a$0$16582$5a62ac22@freenews.iinet.net.au...
> Hi ,
>
> I am performing an integer count of a particular operation in my program.
> After a sufficiently large value an overflow occurs. At the moment I have
> gone around the problem by declaring it as a double, even that has its
> limits. Is there a method of preventing this overflow or some method of
> recovering from it. Any help in this regard would be greatly appreciated.

  unsigned long counter[2] = { 0 };

  for (;;)
  {
    if (++counter[0] && ++counter[1])
      puts("64+ bit counter overflowed!");
  }

This is trivially extendable to as much precision as you want. But if you need a counter
bigger than a minimum of 64-bits, then I'd love to know what machine you're using and
where I can pick one up!

--
Peter


Relevant Pages

  • Re: integer overflow
    ... "CBFalconer" wrote in message ... > Ashutosh Iddya wrote: ... After a sufficiently large value an overflow occurs. ... >> this regard would be greatly appreciated. ...
    (comp.lang.c)
  • Re: integer overflow
    ... Ashutosh Iddya wrote: ... > I am performing an integer count of a particular operation in my ... After a sufficiently large value an overflow occurs. ... then you can use long long (on C99 or gnu gcc ...
    (comp.lang.c)
  • Re: integer overflow
    ... On Sat, 17 Apr 2004, Peter Nilsson wrote: ... >> I am performing an integer count of a particular operation in my program. ... >> After a sufficiently large value an overflow occurs. ... Any help in this regard would be greatly appreciated. ...
    (comp.lang.c)
  • Re: integer overflow
    ... >> I am performing an integer count of a particular operation in my program. ... >> After a sufficiently large value an overflow occurs. ... >> recovering from it. ... Any help in this regard would be greatly appreciated. ...
    (comp.lang.c)
  • Re: Signed fixed-point multiplication
    ... language) for performing a signed 16.16 fixed-point multiply with ... When does overflow occur with 32bits*32bits=>64bits ... ; V flag always clear? ...
    (comp.sys.acorn.programmer)