Quote:
Originally posted by WizardOfAz
The manual (Basic_Stamp_Manual_V2-0.pdf, pg 153) says you should not use bitwise operators in IF statements, but when used with awareness of how the bit variables are handled, they seem to work. Can anyone clarify whether what's safe WRT this?
|
As you said, as long as you understand how bit ops work, you should be fine. However, there are many, many caveats regarding bitwise stuff, so be very careful. Here's some examples:
5 AND 2 will evaluate to true, while
5 & 2 will be false, since 101b & 10b = 000b.
5 AND NOT 2 will evaluate to false, while
5 & ~2 will be true, since 101b & 111111101b=101b=5
Also, as far as I know, the exact value of =, >, <, etc is not officially defined except for the fact that it is zero for false and non-zero for true. Thus, things like:
(5>2) & (3<10)
could evaluate to either true or false, depending on how >, < get evaluated.
In summary, if you know what you're doing and you're only working with bit-sized variables, you can probably get by with the bitwise stuff. Otherwise, I would HIGHLY recommend using the more correct logical operators. It will make your code easier to maintain, easier to understand, and probably more correct.
--Rob
P.S. If anyone knows for sure what =, >, etc evaluate to, I would greatly appreciate knowing in order to fix up RoboEmu. I'm going with the assumption of either 0 or 1 for now, but it could be changed very easily.