Shop OBEX P1 Docs P2 Docs Learn Events
The best way to acess BITs of data in variables. — Parallax Forums

The best way to acess BITs of data in variables.

I am using a word to store the states of data that is being sent to 2 8bit shift registers. Bits 0-9 are used and 10-15 are unused. The shift registers are attached to relays and i can see so far 2 ways of doing this. 1)use the rotate command to get to bit needing updated, then rotating back to original or 2) using the output variable and using & %xxxxxx00_00000000 and change the needed 0..9 bit to 1 to update it. X being the unused shift register pins. If I was using the BS2 i could just create a 10 bit array and update it that way, but since the Propeller cant access Bits n Nibbles like the BS can, I need to make it do want I want. So as my title implies, whats the best way to access each Bit in my Word variable??

Joe

Comments

  • Are you using Spin or PASM?
  • Spin. I havent really learned PASM yet.

    Joe
  • ElectrodudeElectrodude Posts: 1,658
    edited 2015-11-10 01:37
    To get bit x of y in Spin, simply do "(y >> x) & 1". That shifts y right x bits, leaving the bit of interest on the bottom, and then masks out everything but that bottom bit.
  • What about rotating the bits so i dont loose the data before it? Its for a relayboard that each relay is controlled individually and i have it designed so either relay can be turned on momentarily or latched on n off.

    Joe
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-11-10 15:47
    I use these methods (in my standard template program).
    pub wr_bit(target, pos, value)
    
    '' writes value.0 to target.pos
    
      if (value & 1)
        return target | (1 << pos)
      else
        return target & !(1 << pos)
    
    
    pub rd_bit(value, pos)
    
    '' returns bit (0..1) of value.pos
    
      if ((pos => 0) and (pos =< 31))
        return (value >> pos) & 1
      else
        return 0
    
    The wr_bit() method returns the updated value -- you can either put it back into the variable you passed, or move it to another.
  • SapphireSapphire Posts: 496
    edited 2015-11-10 02:15
    Try using the bitwise decode |< operator. This converts a value (0-31) into a single bit.

    To turn on your relay, assuming your word is called Relays and the relay you want to turn on is Number (0-9):
    Relays |= (|< Number)
    
    To turn off your relay:
    Relays &= !(|< Number)
    
    Only the bit corresponding to Number will change (either turn on or off)


  • ElectrodudeElectrodude Posts: 1,658
    edited 2015-11-10 02:37
    What about rotating the bits so i dont loose the data before it? Its for a relayboard that each relay is controlled individually and i have it designed so either relay can be turned on momentarily or latched on n off.

    Joe

    "x >> y" doesn't modify x or y, somewhat similarly to how on a BS2 if you do "x = 1" and then "y = x + 1", x doesn't magically end up equalling 3 even though you're adding 1 to it (although y obviously does). You won't lose your data with my method.

    I originally asked if you were using PASM because this is something you would need to worry about in PASM, but it's not a problem in Spin.

    EDIT: It seems you were asking more about setting bits than reading them, sorry. Use Sapphire's method for setting them, it's pretty much the standard way of doing it.

    You can extract a bit using something similar Sapphire's method with "Relays & (|< Number)", but that will leave the bit in the same position. This means that, if Number = 4, instead of getting either 1 or 0, you'll get either 32 (%10000) or 0. That's fine (and the fastest way) for the argument to an if statement, which considers anything that's not 0 to be true.

    "|< x" is pretty much equivalent to "1 << x", but it's better because it's faster.
  • Thanks everyone. Will try n learn all of those methods for future projects.

    Joe
Sign In or Register to comment.