Shop OBEX P1 Docs P2 Docs Learn Events
bytemove question — Parallax Forums

bytemove question

Will it work to shift a line of bytes to the right by one byte using bytemove like this? bytemove(array[2],array[1],5) I want to put 4 bytes into the var called array one at a time shifting the bytes to the right before adding the new one at the left begining of the line. The last byte is a"don't care just a space to let the bytes to fall into with the bytemove.
Thanks
Jim

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2021-12-14 01:21

    Do this:

    bytemove(@array[1], @array[0], 4)

    Make sure array has a dimension of 5. (As you had written it, it would need to be 7 bytes long, but it wouldn't have worked anyway, since you left out the @'s.) The Spin interpreter will do the move properly for you automatically, without overwriting data that you don't want overwritten.

    -Phil

  • JonnyMacJonnyMac Posts: 9,102
    edited 2021-12-14 02:07

    I did this little experiment. Is this what you're looking for?

    BTW... if your array is always going to be four bytes, you can just use a long.

      buf <<= 8                                                     ' get rid of high byte
      buf.byte[0] := rndx.xrandomize($00, $FF)                      ' new low byte
    
  • Right you are, Jon! Dimension 4, and move 3.

    -Phil

  • Thanks Jon and Phil,
    The use of the @ symbol gets me every time. I think I like best Jon's idea of just using an array of longs as it does not waiste that extra byte in every cell of the array and neatly leaves every thing aligned on long boundries
    Thanks
    Jim

  • cgraceycgracey Posts: 14,152

    The Spin2 "move" commands figure out whether they should move first-to-last or last-to-first, based on the source-destination relationship.

  • The Spin2 "move" commands figure out whether they should move first-to-last or last-to-first, based on the source-destination relationship.

    The Spin1 move commands do that, too, so a moved datum doesn't propagate through the whole array.

    -Phil

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2021-12-14 20:21

    I think I like best Jon's idea of just using an array of longs ...

    You don't need an array of them. Just one will suffice. But you'll have to reframe your move command, thus:

    bytemove(@longvariable + 1, @longvariable, 3)

    -Phil

  • bytemove(@longvariable + 1, @longvariable, 3)

    You don't need to use bytemove. If you're adding to the LSB end, do this:

      buf <<= 8
      buf.byte[0] := newVal
    

    If you're adding to the MSB end, do this:

      buf >>= 8
      buf.byte[3] := newVal
    
  • Or maybe:

    buf := buf << 8 | newVal & $ff
    
    buf := newVal << 24 | buf >> 8
    

    Faster, slower, bigger, smaller?

    -Phil

  • OK, I like the idea of using a long to store the 4 bytes. What I am trying to do is take 4 ADC readings sum them together and devide by 4. to get an average.
    Jim

Sign In or Register to comment.