PDA

View Full Version : 32 bit Addition - How to ?



capitano
- 28th December 2004, 09:13
Is possible in Pic Basic Pro make an addition at 32 bit ?

This is my problem


i var word


:loop

x=x+i ' x is variable at 32 bit


' program



goto loop

Can you help me please....

Melanie
- 28th December 2004, 14:19
Firstly you cannot have a single 32 bit variable defined in PICBasic so we define two 16 bit variables...

xlow var word ' as the lower bits 15-0
xhigh var word ' as the upper bits 31-16

together xhigh and xlow make up your 32 bits...

Now consider this...

xlow=xlow+i
if xlow < i then xhigh=xhigh+1

here we assume that variable i is also a word...

If the addition of variable i to xlow (the lower 16 bits of our 32 bit word) causes an overflow within xlow, the resultant will be less than variable i itself. If this occurs then simply carry 1 to the upper 16 bits residing in xhigh.

Melanie

capitano
- 28th December 2004, 18:19
Thank you ... now i try it....

capitano
- 28th December 2004, 19:45
Thank

The routine of addiction work fine, i try to make subtraction routine

xlow=xlow-i
if xlow > i then xhigh=xhigh-1

Can be it ?
I try but it don't work fine...

Melanie
- 29th December 2004, 11:14
Of course your subtraction doesn't work... how were you taught math at school when your teacher told you how to subtract numbers? Whatever you 'borrow' from the preceeding column has to be added into the column you are working on. Apply the same subtraction method you were taught when you were seven years old here. xhigh and xlow are simply your two columns of numbers.