View Single Post
  #7   Spotlight this post!  
Unread 08-04-2003, 21:57
WizardOfAz's Avatar
WizardOfAz WizardOfAz is offline
Lead Mentor
AKA: Bill Bennett
FRC #1011 (CRUSH)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Tucson, AZ
Posts: 101
WizardOfAz will become famous soon enough
Send a message via AIM to WizardOfAz
bitwise operators in IF statements

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?

For example

'(note all variables in these code snippets are single bits)
b1 var bit
b2 var bit

if (b1 & b2) then

appears to behave equivalently to

if (b1=1) and (b2=1) then

And produces smaller code, which is the goal of this question.

Also...

though it's understandable if unfortunate that you don't get what you might expect when you code

if (~b1) then

since ~b1 will always evaluate to %11111111111111x and therefore always be true regardless of the value of the bit stored in b1.

Seems like

if (b1 & ~b2) then

Will give the expected and desired result since the leading zeros in b1 will mask off the leading ones in b2, and the result will depend only on the logical & of the single variable bits. Again, this produces smaller code than does

if (b1=1) and (b2=0) then

Any feedback?

Bill