Shop OBEX P1 Docs P2 Docs Learn Events
Accessing a byte within an array of longs. — Parallax Forums

Accessing a byte within an array of longs.

W9GFOW9GFO Posts: 4,010
edited 2010-06-20 05:51 in Propeller 1
I know you can access a particular byte within a long like this: MyVar.byte[noparse][[/noparse] 2]

How can you address a byte within a long that is part of an array?

VAR long MyVar[noparse][[/noparse]100]

MyVar[noparse][[/noparse]23].byte[noparse][[/noparse] 2] does not work.

Also, how can I look at just one bit in an array of longs?

Rich h

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.

Post Edited (W9GFO) : 6/20/2010 12:14:46 AM GMT

Comments

  • localrogerlocalroger Posts: 3,452
    edited 2010-06-20 00:05
    You can do it the verbose way: byte[noparse][[/noparse] @myvar[noparse][[/noparse] 100 ] ]
  • T ChapT Chap Posts: 4,223
    edited 2010-06-20 00:09
    I think you may have lost some of your intended code due to formatting in the forum, since you would need to specify which byte by using byte[noparse][[/noparse] 3 ] for example. I can't get this to work MyVar[noparse][[/noparse] 23 ].byte but this does: MyVar.byte[noparse][[/noparse] 10 ] for example, where you would have to manage in multiples of 4 which long/byte you want to access.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-06-20 00:23
    byte[noparse][[/noparse]­@MyVar[noparse][[/noparse]­23]][noparse][[/noparse]­2] is probably the most intuitive.

    -Phil
  • W9GFOW9GFO Posts: 4,010
    edited 2010-06-20 00:24
    Todd Chapman said...
    I think you may have lost some of your intended code due to formatting in the forum, since you would need to specify which byte by using byte[noparse][[/noparse] 3 ] for example. I can't get this to work MyVar[noparse][[/noparse] 23 ].byte but this does: MyVar.byte[noparse][[/noparse] 10 ] for example, where you would have to manage in multiples of 4 which long/byte you want to access.
    Original post edited to add a space inside the brackets.

    So does MyVar.byte[noparse][[/noparse]23] point to the 24th byte in the array which is byte 3 of MyVar[noparse][[/noparse] 6]?

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • W9GFOW9GFO Posts: 4,010
    edited 2010-06-20 00:31
    The verbose way it is! Thanks.

    Now, is there an equally simple way to look at a single bit within an array?

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • ratronicratronic Posts: 1,451
    edited 2010-06-20 03:39
    W9FGO,·you could try something like this,

    if (byte[noparse]/noparse]@myvar[noparse][[/noparse]23[noparse][[/noparse]2] >> 4) & 1

    · 'this bit is a 1

    else

    · 'this bit is a 0

    The 4 in the if statement above is the bit position(0 - 7)·you pick to check. But I'm not a expert so maybe one of the gurus here can step in and show us a better way!



    edit: thats looking at bit position 4(0-7) of the 3rd byte of the 24th long in myvar[noparse]/noparse

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ···································Fix it, if ain't broke!


    D Rat

    Dave Ratcliff N6YEE

    Post Edited (ratronic) : 6/20/2010 3:51:34 AM GMT
  • W9GFOW9GFO Posts: 4,010
    edited 2010-06-20 04:43
    I was thinking of something like this;

    if myvar[noparse][[/noparse]23] & $10_00_00_00 '(%00010000_00000000_00000000_00000000)

    'bit 4 of byte 3 of the 24th long in the array is a 1

    else

    'bit 4 of byte 3 of the 24th long in the array is a 0

    Or a maybe this is simpler;

    if myvar[noparse][[/noparse]23] >> 28 & 1

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-06-20 04:51
    The quickest to execute would probably be this: if myvar[noparse][[/noparse]­23] & constant(1 << 28)

    It saves having to do the shift at run-time.

    -Phil
  • ratronicratronic Posts: 1,451
    edited 2010-06-20 05:07
    Yes if your looking at a long instead a byte then it would be,

    if (myvar[noparse][[/noparse]23] >> 28) & 1·

    the >> 28 gets the bit you want shifted to bit position 0·, the & 1 masks out any other bits than bit position 0. With a long, bit positions are 0 - 31 (32 bits). Also notice where the parenthesis are placed

    Edit: = thanks Phil, I was hoping somebody would show a better way!



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ···································Fix it, if ain't broke!


    D Rat

    Dave Ratcliff N6YEE
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-06-20 05:22
    If the bit has any significance beyond a purely mathematical one, it's better for readability to give it a name and test if that way:

    CON

    GLAXORBIT = 1 << 28

    ...

    if (myvar[noparse][[/noparse]*28] & GLAXORBIT)

    Plus, if you decide GLAXORBIT needs to be located somewhere else, you've got only the CON statement to change.

    -Phil
  • W9GFOW9GFO Posts: 4,010
    edited 2010-06-20 05:51
    Would this work?

    CON
      randomByte = $FF << 5     ' arbitrary group of 8 bits   (00000000_00000000_00011111_11100000)
    
    VAR
    
      long myVar[noparse][[/noparse]100]
      byte myByte
    
    PUB Main 
    
      myVar[noparse][[/noparse]23] := $FFFFFFFF
    
      myByte := GetByte(myVar[noparse][[/noparse]23], randomByte)
    
    
    PUB GetByte (_var, thisByte)
    
    result := _var & thisByte >> ((>| thisByte) - 8)
    




    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.

    Post Edited (W9GFO) : 6/20/2010 6:22:32 AM GMT
Sign In or Register to comment.