Jan 17, 2009

How would you add two 64 bit numbers on a 32 bit machine?

How would you add two 64 bit numbers on a 32 bit machine?
C supports “long long int” which is 64 bits. You can use this use in arithmetic as any other type
or
unsigned long x1, x2, y1, y2, z1, z2;
/* set x1, x2, y1, y2 here */
z1 = z2 = 0;

z1 = x1 + y1;
if (z1 < x1 || z1 < y1) {
/* overflow */
z2++;
}
z2 += x2 + y2;

No comments:

Post a Comment