Shop OBEX P1 Docs P2 Docs Learn Events
Split a WORD into 2 BYTES? — Parallax Forums

Split a WORD into 2 BYTES?

TCTC Posts: 1,019
edited 2013-05-12 20:02 in Propeller 1
I need to find a way to split a word into 2 bytes, a high byte, and a low byte.
Thanks
TC

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-05-12 06:37
    Use w.byte[0] and w.byte[1].
  • TCTC Posts: 1,019
    edited 2013-05-12 07:09
    That worked great, Thank you. Would that work if I was using a LONG?

    Just trying to get a hang of it.
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-12 07:17
    TC wrote: »
    Would that work if I was using a LONG?
    Sure, you can also use the .word override (for longs) to get high and/or low word (or any word anywhere, i.e. fred.word[n] is OK).
  • TCTC Posts: 1,019
    edited 2013-05-12 07:27
    Oh wow, Thank you.
  • cavelambcavelamb Posts: 720
    edited 2013-05-12 15:43
    kuroneko wrote: »
    Sure, you can also use the .word override (for longs) to get high and/or low word (or any word anywhere, i.e. fred.word[n] is OK).

    K,
    Also, Oh Wow!
    But...
    Would you expand on that some?
    Perhaps with an example or two?
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-12 16:05
    cavelamb wrote: »
    Would you expand on that some? Perhaps with an example or two?
    This is before I had my morning coffee, HTH
    CON
      _clkmode = XTAL1|PLL16X
      _xinfreq = 5_000_000
    
    OBJ
      serial: "FullDuplexSerial"
      
    PUB null : n
    
      serial.start(31, 30, %0000, 115200)
      waitcnt(clkfreq*3 + cnt)
    
    ' expected output 80000000 7FFFFFFF
    
      n := NEGX
      repeat 2
        serial.hex(n.word[1], 4)                            ' high word
        serial.hex(n.word[0], 4)                            ' low word
        n--
        serial.tx(32)
      serial.tx(13)
    
    ' Using table instead ... 7FFFFFFE
    
      serial.hex(table.word[(@n - @table)/2 +1], 4)         ' high word
      serial.hex(table.word[(@n - @table)/2 +0], 4)         ' low word
      serial.tx(13)
    
    ' expected output BA98 FEDC 3210 7654 FFFF FFFF 4567 0123 CDEF 89AB
    
      repeat n from -4 to 5
        serial.hex(table.word[n], 4)                        ' print all words surrounding table
        serial.tx(32)
      serial.tx(13)
    
    ' access/print clkfreq
    '
    ' With table being at address A we have to go back A bytes or A/2 words.
    
      serial.hex(table.word[-@table/2 +1], 4)               ' high word
      serial.hex(table.word[-@table/2 +0], 4)               ' low word
      serial.tx(13)
      
    DAT
    
                    long    $FEDCBA98, $76543210            ' -3/-4, -1/-2
    table           long    -1                              ' +1/+0
                    long    $01234567, $89ABCDEF            ' +3/+2, +5/+4
    
    DAT
    
  • cavelambcavelamb Posts: 720
    edited 2013-05-12 20:02
    I'm about to turn in for the night.
    I'll get on this in the morning.

    Thank you. sir.
Sign In or Register to comment.