Shop OBEX P1 Docs P2 Docs Learn Events
Simple Variable Question — Parallax Forums

Simple Variable Question

TCTC Posts: 1,019
edited 2011-01-13 17:32 in Propeller 1
I am learning SPIN, need to have control of one bit of a variable.

If done in PBASIC:
BlaBla.bit8 = 1
Or
BlaBla.bit8 = 0
Thanks TC

Comments

  • JonnyMacJonnyMac Posts: 9,235
    edited 2011-01-13 17:14
    There are no direct bit manipulations in Spin (or PASM for that matter), but it's very easy to do with a mask. I have this in my standard template:
    pub putbit(target, pos, value)
    
    '' writes value.0 to target.pos
    
      if (value & 1)
        return target | (1 << pos)
      else
        return target & !(1 << pos)
    

    To update your examples, you'd do this
    blabla := putbit(blabla, 8, 1)
    blabla := putbit(blabla, 8, 0)
    
  • TCTC Posts: 1,019
    edited 2011-01-13 17:16
    Thank You Jonny, I think I will save that one too.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-01-13 17:18
    Check out the bitwise decode/encode - page 160 in my manual (Help/Propeller Manual in the Proptool) though this might not be the latest manual. Search for "bitwise encode".

    Pin := |<PinNum

    If PinNum is 3, Pin is set equal to %00000000 00000000 00000000 00001000.

    Near this section in the manual are the bitwise rotates.

    Then there is AND and OR. I tend to use these rather than the bitwise encode as they are rather similar to the way that assembly works. So you have to learn less commands.

    In general, to set one bit high, use an OR - eg on bit 3 %00000000_00000000_00000000_00001000

    For each bit, if you OR then the truth table is 0,0=0, 0,1=1, 1,0=1 1,1=1. So for all the bits that you leave as 0, they stay the same. And the one bit you have as 1 gets forced high.

    To set one bit low, use a logical AND, and a mask like %11111111_11111111_11111111_11110111

    the truth table for and is 0,0=0, 0,1=0, 1,0=0 and 1,1=1. So if a bit is high in the mask and high in the data you want to change, it stays high. If it is low it stays low. The only one that changes is the one bit you set low, which forces that bit low.

    X := %00101100 & %00001111
    The above example ANDs %00101100 with %00001111 and writes the result, %00001100, to X.

    In pasm, what you find is you can set up a series of masks in a section at the end, and then reuse them over and over.

    HTH
  • TCTC Posts: 1,019
    edited 2011-01-13 17:32
    @Dr
    I did look at that but I was having trouble understanding what I needed to do. The value will change and must stay intact but I must be able to change one bit without disturbing the rest of the value. Because of what Jonny gave, I understand it better. I thank you for your help.
Sign In or Register to comment.