Mailinglist Archive: opensuse-programming (53 mails)

< Previous Next >
RE: [suse-programming-e] Detect overflow when adding 2 unsigned int
  • From: Hallingstad Håkon <hakon.hallingstad@xxxxxxx>
  • Date: Tue, 7 Jun 2005 06:54:27 +0200
  • Message-id: <4723EA30C3CDB94EB907CD7322832F9E0787AA@xxxxxxxxxxxxxxxxxxxxxx>
Use 'if (c < a)' (or 'if (c < b)').

Regards,
Håkon Hallingstad

-----Original Message-----
From: Verdi March [mailto:cincaipatron@xxxxxxx]
Sent: 7. juni 2005 06:18
To: suse-programming-e@xxxxxxxx
Subject: [suse-programming-e] Detect overflow when adding 2 unsigned int


Hi,

how do I detect if an overflow in the following?

unsigned int a, b, c;
a = 0xffffffff;
b = 0xffffffff;
c = a + b;

I understand this can be done by declaring c long long
and check if c > (1 << 32) - 1, but I just want to know
if it's possible with c as an unsigned integer.

Will the following work for all values of a and b?

if (b != c - a)
printf("Overflow\n");


Regards,
Verdi

--
Weitersagen: GMX DSL-Flatrates mit Tempo-Garantie!
Ab 4,99 Euro/Monat: http://www.gmx.net/de/go/dsl

--
To unsubscribe, email: suse-programming-e-unsubscribe@xxxxxxxx
For additional commands, email: suse-programming-e-help@xxxxxxxx Archives can be found at: http://lists.suse.com/archive/suse-programming-e

< Previous Next >
Follow Ups