Shop OBEX P1 Docs P2 Docs Learn Events
replace a byte of a long — Parallax Forums

replace a byte of a long

Bobb FwedBobb Fwed Posts: 1,119
edited 2009-08-06 18:15 in Propeller 1
I was trying to think of an easy way to replace a single byte of a long, but I didn't get much sleep last night, so I am stuck with this solution:
'' switching out byte[noparse][[/noparse] 2 ]
byte := $80
long := (long & $FF_00_FF_FF) | (byte << 16)



There's got to be an easier way that I am just blind to because of lack of beauty sleep.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
April, 2008: when I discovered the answers to all my micro-computational-botherations!

Post Edited (Bobb Fwed) : 8/6/2009 3:59:27 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-08-05 18:36
    You could say: longValue.byte[noparse][[/noparse] 2 ] := byteValue
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-08-05 19:26
    Thanks. I knew there was an easier way, I just forgot about the .byte stuff.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-08-06 16:00
    OK, what about a variable as an array:
    can a byte (or word) be extracted/changed from a long?

    Or, alternatively, will two declared variables in a method (PUB Main | v1, v2) always be next to each other in hub memory (i.e. I could just store my information to two adjacent variables rather than an array)?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-08-06 16:16
    Well, I think I figured out a way to use the array:
    PUB Main | out, v[noparse][[/noparse] 2 ]
    
      v[noparse][[/noparse] 0 ] := $12_23_34_45
      v[noparse][[/noparse] 1 ] := $56_67_78_89
    
      out := v1.word[noparse][[/noparse] 3 ]
    
    


    out will be $56_67

    EDIT: fixed typo and formatting issue

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!

    Post Edited (Bobb Fwed) : 8/6/2009 7:05:26 PM GMT
  • localrogerlocalroger Posts: 3,452
    edited 2009-08-06 18:15
    Bobb, the .byte syntax doesn't work with individual array entries but you can do byte[noparse][[/noparse] @v[noparse][[/noparse] 1 ]·+ 1 ]
Sign In or Register to comment.