Bit flags revisited
James Long
Posts: 1,181
Ok.....so I remember we had a discussion about bit flags.
I'm loosing my memory...but I don't remember a direct way to set a bit (in a byte) and use the other bits as well.
Like having a whole byte as flags...and being able to set each one for a different event.
As such:
Bit #0 would be for a set point if a temperature had been reached.
Bit #1 would be for a motor had been turned on.
so on and so forth.
I know I could read the byte...then just add a number to it that would equal the bit to be set. But that could really be a nightmare if you got the number wrong.
I'm just asking...to be sure. I don't mind doing it this way....but it takes more instructions to do so.
Thanks for the revisit,
James L
I'm loosing my memory...but I don't remember a direct way to set a bit (in a byte) and use the other bits as well.
Like having a whole byte as flags...and being able to set each one for a different event.
As such:
Bit #0 would be for a set point if a temperature had been reached.
Bit #1 would be for a motor had been turned on.
so on and so forth.
I know I could read the byte...then just add a number to it that would equal the bit to be set. But that could really be a nightmare if you got the number wrong.
I'm just asking...to be sure. I don't mind doing it this way....but it takes more instructions to do so.
Thanks for the revisit,
James L
Comments
You can combine several bits in one instruction by adding (+) or using the bit or operator (|).
Oh....ok...I haven't researched any assembly commands yet.....I'm still working in Spin.
I understand all of that except the (+) or (-) operator.....I'll take a look in the book.
You are so much better at assembly than I. I just don't know it. And haven't started to learn it.
Thanks,
James L
This works the same way in SPIN as in assembly except that there isn't a single operator for ANDN.
The comment about the (+) ... You can combine separate bit with addition as well as bit or because
the bit "columns" add without carry as long as you only have one of each bit. In other words, you
can add or use logical or for %00010101 to %10101000 to get %10111101. On the other hand,
if you add %101 to %001 you get %110. If you use logical or, %101 | %001 is %101.
Thanks that is a great help.
That will make setting individual bits very easy.
Now that I can set them......I'm scratching my head how to read them.
considering myvariable.bit[noparse][[/noparse]1] is not allowed.
Hmmm.....another hurdle......aint this fun!!!
Thanks again.....you are a great resource.
James L
Post Edited (James Long) : 1/1/2007 4:32:22 PM GMT
In SPIN, use "if temp & |<3" to test for true, "if not temp & |<3" to test for false.
In assembly, use
to jump to somewhere if true. Use "if_z" to jump if false.
How about programming it for me too.......
Just kidding....you have helped me alot.
I have quite a few flags to set...and didn't want to use a full byte for each.
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
links:
My band's website
Our album on the iTunes Music Store
James L