Shop OBEX P1 Docs P2 Docs Learn Events
32 bit math (input 32 bit value from keypad) — Parallax Forums

32 bit math (input 32 bit value from keypad)

ArchiverArchiver Posts: 46,084
edited 2003-10-01 00:27 in General Discussion
I am trying to incorporate some of the different 32 bit math functions that
have been discussed. I tried to use Tracy's method for double precision add but
it doesn't seem to work if either value is larger than 16 bits. also the
carry bit (cb) doesn't seem to ever generate a carry. I need to add two numbers
with a result that will ultimately fit in 32 bits, BUT either number could be
larger than 16 bits (though not simultaneously). Example would be to add
999,999,998 and 1 which should result in 999,999,999 which should fit in 32
bits.
Does anyone have any experience with a routine that will work for variables
in this wide range?
The project itself is a method to input and store a 32 bit number from a
numeric keypad.
Thanks!


[noparse][[/noparse]Non-text portions of this message have been removed]

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-09-30 23:55
    >I am trying to incorporate some of the different 32 bit math functions that
    >have been discussed. I tried to use Tracy's method for double
    >precision add but it doesn't seem to work if either value is larger
    >than 16 bits. also the
    >carry bit (cb) doesn't seem to ever generate a carry. I need to add
    >two numbers with a result that will ultimately fit in 32 bits, BUT
    >either number could be larger than 16 bits (though not
    >simultaneously). Example would be to add 999,999,998 and 1 which
    >should result in 999,999,999 which should fit in 32 bits.Does anyone
    >have any experience with a routine that will work for variables in
    >this wide range? The project itself is a method to input and store a
    >32 bit number from a
    >numeric keypad.
    >Thanks!


    Hi,


    I'm not sure which method you are talking about, but I don't see a
    problem with the following. Either value, x1:x0, or y1:y0, or both,
    can be 32 bit values, and the sum z1:z0 is also 32 bits. The value
    cb here is the carry out of the sum of the least significant words.

    x0 var word
    x1 var word
    y0 var word
    y1 var word
    z0 var word
    z1 var word

    add1616: ' Z=X+Y in 16:16 notation z1:z0 = x1:x0 + y1:y0
    z0 = x0 + y0 ' sum of low words
    cb = z0 max y0 - y0 max 1 ' carry from low add
    z1 = x1 + y1 + cb ' sum of high words plus carry from low word
    return ' z1:z0 < (2^32)

    If that still doesn't work for you, please give me an example.

    There might also be a possibility of a carry out of the whole 32 bit
    add. In some posts I leave the variable cb with the carry to 33 bits.

    For example, the sum 4294967295 + 1 generates a carry at both levels,
    that is, in hex, x1:x0 + y1:y0 = $ffffffff + $00000001 = 1$00000000.


    Here is the same snippet, but using PBASIC 2.5 IF-THEN-statement
    syntax instead of computed carry.

    add1616: ' Z=X+Y in 16:16 notation
    z0=x0 + y0
    z1=x1 + y1
    IF z0<y0 THEN z1=z1+1 ' z0<y0 is always true when a carry occurs
    ' but this trick only works when there are two
    ' terms in the sum, (and not 3 or more).
    return


    -- regards
    Tracy Allen
    http://www.emesystems.com
  • ArchiverArchiver Posts: 46,084
    edited 2003-10-01 00:27
    Thanks Tracy! There seems to be a typo in the code on your web site for this
    routine. The code posted here in this response seems to make more sense. The
    problem I was having was in the carry from low word addition to high word
    addition. Thanks again.


    [noparse][[/noparse]Non-text portions of this message have been removed]
Sign In or Register to comment.