The best way to acess BITs of data in variables.
jknightandkarr
Posts: 234
in Propeller 1
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
Joe
Comments
Joe
Joe
The wr_bit() method returns the updated value -- you can either put it back into the variable you passed, or move it to another.
To turn on your relay, assuming your word is called Relays and the relay you want to turn on is Number (0-9):
To turn off your relay:
Only the bit corresponding to Number will change (either turn on or off)
"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.
Joe