64 bit operation on 32 bit PC

Quote:

> How to do 64 bit operation such as +, - on 32 bit PC?

Think back to your school days. When you did addition with large (>10)

integers, you may have arranged them above each other, and labeled each

column of digits "units, tens, hundreds, etc"

The principal is that same for large integers. Handle an array of ints and

have each integer have an exponentially increasing value.

(For simplicity sake, in these examples, I'll use 16 bit ints, so

UINT_MAX is 65535. If yours int's are 32 bits, adjust your figures

appropiately.)

So to add two 64 bit ints, each aranged as an array of four unsigned ints...

281474976710656s 4294967296s 65536s Units

i1[3] i1[2] i1[1] i1[0]

i2[3] i2[2] i2[1] i2[0]

-------------------------------------------------------------

=============================================================

Start from the right hand column and add i1[0] and i2[0]. If the result

goes over 65536, carry one over to the next column. Write the result, less

the carried 65536 underneath the rightmost column.

Similarly, for i1[1] and i2[1], add them together and also add the

carry from the last column. Again, if your result goes over 65536, reduce the

result by that value and carry one over the next column.

From this, a simple algorithm should result.

Carry=0

For each column from zero,

Total up that column and add the carry.

Carry = result / 65536. [A]

Total[column] = result % 65536. [A]

End for

If carry == 1

Overflow error.

For subtraction, the concept is similar, if a subtraction would result in

a -ve number, you would "borrow" a ten. Check your notes from your school

days if you are unsure.

As for divison or multiplicaton, you can use repeated addition or

subtraction, but look up long division/multiplication. If you can get it

to work, it'll be a lot better.

Anyway, here's a footnote...

[A] If you want to add two integers together and check for overflow, you

may have a problem. You may have a language extension which allows

you to detect an overflow, but you probably won't.

Consider doing just the addition operation in assembly. Most CPUs will

silently roll overflows around and set a CPU flag which you can detect.

For a more portable method, cast the two integers to longs and then add the

two longs together. Although this only works if a long can hold at least

twice the range of an int. If int==long on your setup, consider the other

integers (char, short, int and long). Two of these are probably different

sizes.

Once you have selected your two integer types, hide them behind typedefs.

That way, should you move to a different compiler or platform, you can

chnage the underlying int types just once.

However, it must be said that a C compiler is quite allowed to make

char==short==int==long.

In this circumstance, you'd need to waste at least one bit of your

underlying int type, so that worst case, two instances of the highest

possible value can be added together and fit inside an int happily.

In the example of 16 bits ints, a base of 32768 instead of 65536 would allow

this to work. (32767+32767+1 = 65535, which fits)

Anyway, hope this helps.

Bill, forty integers in a row.