Shop OBEX P1 Docs P2 Docs Learn Events
Moving/Seperating numbers — Parallax Forums

Moving/Seperating numbers

JBWolfJBWolf Posts: 405
edited 2013-02-17 03:43 in Propeller 1
I believe there are some unique ways using SPIN to move bytes and numbers around and was hoping somone could provide some help.
I am compiling multiple variables into one numeric value by multiplying by 10 and adding the next variable.
i.e... AllVars := ((((VarA * 10) + VarB) * 10) + VarC)
None of these individual variables exceed a value of 9 so they can be compiled.

So now I'm trying to find the best way to return it back to multiple variables.

I can only think of a complicated way to do this and the numbers do have remainders which I expect to be lost since the variables are not declared as float.
i.e:
VarA := (AllVars / 100)
VarB := ((AllVars / 10) - (VarA * 10))
VarC := ((AllVars - (VarA * 100)) - (VarB * 10))

I was wondering if there is SPIN code to simplify this task.
Thanks

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-02-17 03:39
    Try this, it splits the number into its decimal digits, each being stored into an element of the digits byte array. Sign is currently ignored but can easily be added.
    CON
      _clkmode = XTAL1|PLL16X
      _xinfreq = 5_000_000
    
    OBJ
      serial: "FullDuplexSerial"
      
    VAR
      byte  digits[16]
      
    PUB null | n
    
      serial.start(31, 30, %0000, 115200)
      waitcnt(clkfreq*3 + cnt)
      serial.tx(0)
      
      n := split(123456789)
    
      repeat n
        serial.dec(digits[--n])
      serial.tx(13)
    
    PRI split(value) : n
    
    ' n starts as 0
    
      repeat
        digits[n++] := ||(value // 10)
        value /= 10
      while value
    
    ' fill level is returned
    
    DAT
    
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-02-17 03:43
    Fastest way is using BCD representation. It makes use of the fact that the numbers 0 - 9 are equal to %0000 - %1001 and need maximum 4 bits

    AllVars := VarA | VarB<<4 | VarC<<8

    The reverse operation is:
    VarA := AllVars & %1111
    VarB := AllVars>>4 & %1111
    VarC := AllVars>>8 & %1111
Sign In or Register to comment.