Logical operators...??

Odessa

Senior Member
I see in the Picaxe manuals where traditional boolean operators such as "and" operate bitwise mathematically on registers, but what about logical operations? Are logical operations available for "and", and "or"?

For example, the following code is acceptable to the Picaxe Editor (14M), and when simulated, if Pin4 and Pin1 are simultaneously set to 1, then the code branches to Motoroff, but there is no branching when Pin4 is set to 0 and Pin0 is set to 1.

How is this code really interpreted?
______________

Start:
If Pin4=1 and Pin1=1 or Pin4=0 and Pin0=1 then
Goto Motoroff EndIf

Goto Start

MotorOff:
Goto Start
_______________

Is it possible to reliably apply logical operators to If/Then statements, and if so, what is the order of execution for logical operators such as "and", and "or"?

Thanks in advance for any help, Odessa.
 

hippy

Ex-Staff (retired)
Rev-Ed's recommendation is to not mix AND and OR in any conditional; IF-THEN, LOOP-WHILE, LOOP-UNTIL.
 

westaust55

Moderator
Rev-Ed's recommendation is to not mix AND and OR in any conditional; IF-THEN, LOOP-WHILE, LOOP-UNTIL.
Manual 1 gets the closes to clarifying the situation with:
Two switches (or more) can be combined by the AND or OR key words.
That is you can use one OR the other.


In simple terms, you can use multiple AND's
or you can use multiple OR's
but you cannot mix them on the same IF THEN test/check.

Maybe it can be better expressed in the next manual revision.
 

Odessa

Senior Member
Ok, WestAust55, thanks for that clarification. I had missed that because I did not read the tutorial in the manual on digital inputs. When I did a search on "Logical" in the manual, there was nothing found on this topic. It seems there should be at least a small section on this in the core of the manual, instead of it relegated to a tutorial on an only partially related subject.

It seems this would this be primarily a compiler limitation, to be overcome into the future. Certainly this has come up before, I would think quite often in terms of code optimization...

Here is an example where 1) implementing the code today costs 30 bytes; and 2) supposing the code could be implemented and work correctly, the second entry (where logical "and" takes precedence in the execution prior to logical "or", as is commonplace in most programming languages), only costs 16 bytes (both as per today's PE syntax check, yet assuming this is a fair comparison, being that the second example does not really execute as desired):

__________________

' This one takes 30 bytes:

Start:
If Pin4=1 and Pin1=1 then
Gosub MotorOff
ElseIf Pin4=0 and Pin0=1 then
Gosub MotorOff EndIf
Goto Start

MotorOff:
Return
________________________

'This one takes 16 bytes ( only 53.3% as much!):

Start:
If Pin4=1 and Pin1=1 or Pin4=0 and Pin0=1 then Motoroff
Goto Start

MotorOff:
REturn

_______________________


When suggestions like these are made here, do they make it to the people that develop the compiler and edit the manuals? Or, is there a specific spot outside the main forum where they should be better made?

Thanks, Odessa.
 

hippy

Ex-Staff (retired)
I certainly look at all new threads and most posts though it is possible that some get missed. Rev-Ed staff do note what PICAXE and Rev-Ed product users say and discuss issues, problems people are having, changes and improvements. For compiler related issues there are also the Programming Editor Software and AXEpad and Compiler Support forums.

Optimisation is quite an art form. These two versions implement what you need without a large program footprint, the first in line with your original code, the second using a Gosub-Return for clarity but slightly larger.

Code:
' 19 bytes

Start:
  If Pin4=1 and Pin1=1 then MotorOff
  If Pin4=0 and Pin0=1 then MotorOff
  Goto Start

MotorOff:
  Goto Start
Code:
' 22 bytes

Start:
  Gosub CheckMotor
  Goto Start

CheckMotor:
  If Pin4=1 and Pin1=1 then MotorOff
  If Pin4=0 and Pin0=1 then MotorOff
  Return
MotorOff:
  Return
 

manie

Senior Member
Hippy: What would be good is to get the "(" and ")" bracketing done. That way one can group the operators as in
If (Pin4=1 and Pin1=1) or (Pin4=0 and Pin0=1) then....
Manie
 

Odessa

Senior Member
Hippy, thanks for your reply. I actually AM doing what you mention in your first suggestion, except mine is an 18 byte implementation, HOWEVER, you cannot really suggest your solution as equivalent. It happens I can serendipitously use the technique IN THIS APPLICATION, but ONLY because the two sequential separate conditions causing the branch to MotorOff in my code can never occur simultaneously. If they could occur simultaneously, then there would be two jumps to MotorOff instead of one and that is not acceptable....so I would still be stuck with ~ twice the memory usage penalty....

I agree with manie in that the parentheses are also desirable (I believe they are already available for use with the 28X1 and higher performance Picaxes, so hopefully that functionality would be extended to the lower pincount devices as well).

Thanks, Odessa.
 

SilentScreamer

Senior Member
I believe they are already available for use with the 28X1 and higher performance Picaxes, so hopefully that functionality would be extended to the lower pincount devices as well
I don't think they will ever leave the X1s or higher as the firmware doesn't support it (unless Rev Ed make brackets that are compiled but would have no effect on the space used thus making it kinda pointless). They are not yet available on the X1s due to a bug in the programming editor however hopefully V5.3 will bring them??? :confused:
 

lbenson

Senior Member
Screamer--it shouldn't be a firmware issue--if you can work around the problem with code on the PC side, then the compiler on the PC side could do the same thing. I'm not sure why there should be an X1+ restriction on the use of parentheses.
 

Odessa

Senior Member
"In simple terms, you can use multiple AND's or you can use multiple OR's but you cannot mix them on the same IF THEN test/check."

Can anyone knowledgeable on this please indicate whether this extends to:
1) concatenated "AND"s & "NAND"s & "ANDNOT"s
2) concatenated "Or"s & "NOR"s & "ORNOT"s
3) concatenated "XOR"s & "XNOR"s

For the purpose of If/Then statements, is XOR logic considered an extension of OR logic, i.e. can these types be mixed?



Thanks, Odessa.
 

hippy

Ex-Staff (retired)
XOR, NAND, NOR, XNOR, ANDNOT, ORNOT, and XORNOT cannot be used in IF conditionals.

They can be used in assignments (LET) and there is no issue there; expression evaluation is left to right and they can all be mixed along with AND and OR.
 
Top