64 bit operation on 32 bit PC 
Author Message
 64 bit operation on 32 bit PC

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

Thanks.



Mon, 15 Apr 2002 03:00:00 GMT  
 64 bit operation on 32 bit PC

comp.lang.c:

Quote:

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

> Thanks.

This is off-topic, but most recent 32 bit compilers for PCs (MSVC,
Borland Builder, gcc) support a 64 bit integer type.  They either use
something like _INT64, or "long long" which has just been standardized
by the ANSI/ISO C standard update.

Are you sure your compiler doesn't have them?  If you have to do
extensive math with them, consider switching to a compiler that does.

Jack Klein
--
Home: http://jackklein.home.att.net



Mon, 15 Apr 2002 03:00:00 GMT  
 64 bit operation on 32 bit PC

Quote:



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

> Upgrade your compiler.  No really.

So he should get one of those shiny new C99 compilers?  I'm not sure
they're out yet Dann.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz


Tue, 16 Apr 2002 03:00:00 GMT  
 64 bit operation on 32 bit PC

Quote:

> > So he should get one of those shiny new C99 compilers?  I'm not sure
> > they're out yet Dann.

> Well that would be nice.  However, every current compiler for the PC from
> every vendor that I know of has a 64 bit integral type offered as an
> extension.  Cygnus GCC, Borland, Watcom, Microsoft, Symantec etc.

I knew about the former, but not about any of the latter.
Interesting.  Didn't know that 64-bit arithmetic was so important.
It's never come up in any of my code, but then again I don't do much
mathematical code.
--
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms


Tue, 16 Apr 2002 03:00:00 GMT  
 64 bit operation on 32 bit PC

Quote:

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

You already asked that.
--
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms


Tue, 16 Apr 2002 03:00:00 GMT  
 64 bit operation on 32 bit PC
On Thu, 28 Oct 1999 22:03:04 -0700, "Dann Corbit"

Quote:





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

> > > Upgrade your compiler.  No really.

> > So he should get one of those shiny new C99 compilers?  I'm not sure
> > they're out yet Dann.

> Well that would be nice.  However, every current compiler for the PC from
> every vendor that I know of has a 64 bit integral type offered as an
> extension.  Cygnus GCC, Borland, Watcom, Microsoft, Symantec etc.

> He can always get MIRACL and do integer math on integers of any size his
> heart might desire.

Better drop Symantec from that list, Dann.  Powersoft has officially
"defuncted" it.

Jack Klein
--
Home: http://jackklein.home.att.net



Tue, 16 Apr 2002 03:00:00 GMT  
 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.



Wed, 17 Apr 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. top 32 bits of 64-bit product of two 32-bit integers

2. Help: porting 32-bit app to 64-bit Dec Alpha

3. 64-bit integer on a 32-bit machine

4. Calling 64 bit lib. from 32 bit program

5. emulating a 64 bit divide with 2 32 bit registers in ansi c

6. converting signed 64 bit - 32 bit

7. 64-bit chips, 32-bit compatibility?

8. REQUEST: 64-bit integer math on 32-bit processor

9. Accessing 32-bit com componet from 64-bit application

10. 32-bit vs 64-bit

11. Looking for: multiply and divide of 32 bit integers (64 bit result)

12. Tool 2 port 32 bit programms 2 64 bit

 

 
Powered by phpBB® Forum Software