Shop OBEX P1 Docs P2 Docs Learn Events
Variable Bit access — Parallax Forums

Variable Bit access

omgitsaliv55omgitsaliv55 Posts: 24
edited 2009-09-21 02:55 in Propeller 1
Hi,

I am still a newbie with the propeller but am at the point where I need to be able to access individual bits of a variable, and so far all I have seen with the objects I've found is that people just use tricks with binary shifting and things like that to do what they need done. Is there any way that I can access and change a variable's bits directly like you can with the Basic Stamp 2? I appreciate any and all help.

Thanks,

Patrick

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-20 16:53
    No

    The only place where you can access individual bits and groups of bits is in some of the special registers (like OUTA and DIRA). You can access the two 16 bit words in a 32 bit long or the 4 bytes in a long or the 2 bytes in a word, but that's it.

    The only way to access individual bits or groups of bits in any variable is with the bit arithmetic operators (& | ^) and the shift operators. There is a bit shift operator (|<) that takes a bit number and produces a mask which helps (|<n is the same as 1 << n).

    Since OUTB is not really used, some people have moved a value to OUTB, then changed bits there and moved the result back to a variable.
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-09-20 18:19
    Hello Patrick,

    there's no equivalent to "rhino.HIGHBYTE.LOWNIB.BIT1"

    The concept of the Basic Stamp is to have a highly sophisticated set of commands doing complex things for you
    like SERIN, SEROUT, set a bit, clear a bit, etc. These commands have a very LIMITED possability of varifying the commands

    there is no direct way to do this in SPIN.
    The concept of SPIN is to have a BASIC set of commands pre-programmed in the propeller-tool (and the SPIN interpreter)

    SPIN allows to define NAMED subroutines that can vary in much more ways to suit almost every different kind of requirements

    This means you can add self-defined commands to the basic set to expand this basic command-set of the language SPIN

    it's no big thing to define new commands like

      PUB Set_Bit_Of_Byte   (PointerToByte,BitNo)
      PUB Clear_Bit_Of_Byte (PointerToByte,BitNo)
    
      PUB Set_Bit_Of_Word   (PointerToWord,BitNo)
      PUB Clear_Bit_Of_Word (PointerToWord,BitNo)
    
      PUB Set_Bit_Of_Long   (PointerToLong,BitNo)
      PUB Clear_Bit_Of_Long (PointerToLong,BitNo)
    
    
    



    VAR
      byte MyByte
    
    
    Pub Demo
      Set_Bit_Of_Byte(@MyByte,5)  'set bit no 5 of variable MyByte
        
    
    



    inside the commands "Set_Bit_Of_Byte" etc.
    you have to use the bitmanipulating commands out of the basic command-set of spin.
    For setting a bit the BITWISE OR command for clearing a bit the bitwise xor-command

    see attached archived democode

    best regards

    Stefan

    Post Edited (StefanL38) : 9/20/2009 7:24:11 PM GMT
  • omgitsaliv55omgitsaliv55 Posts: 24
    edited 2009-09-20 18:52
    Thanks a ton guys! Guess I'll be writing my own bit manipulation object today. Maybe I'll post it to obex later on if I have any success. This will make me think roll.gif

    Thanks again,

    Patrick
  • JonnyMacJonnyMac Posts: 9,198
    edited 2009-09-20 19:26
    Here's another approach to consider. I pass a value to a method and get one back, this way I can pass variables or constants, and I an move the modified version of a valuye to another without affecting the source; for example:

    destvar := togglebit(sourceval, bitnum)

    Where sourceval can be a variable or constant, including the variable destvar.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-09-21 02:55
    You could predefine some constants, viz:

    CON
    
      BIT0 = 1
      BIT1 = 2
      BIT2 = 4
      BIT3 = 8
      ...
      BIT31 = $8000_0000
    
    
    


    Then to test, say, bit 5 of rhino, you'd do something like:

      if rhino & Bit5
    
    
    


    To set bit 5 of rhino, you'd do:

    rhino |= BIT5
    
    
    


    And to clear bit 5,

    rhino &= !BIT5
    
    
    


    -Phil
Sign In or Register to comment.