HP 35s Logic Operator AND
06-19-2016, 06:37 AM
Post: #1
 MNH Junior Member Posts: 44 Joined: May 2014
HP 35s Logic Operator AND
I'm burning some serious midnight oil here in Florida. I just started programming the HP 35s this month. How would I use the logic operator AND to test two flags? Also, in the absence of IF-ELSE and IF-THEN-ELSE statements, is there a better alternative than branching to subroutines? Thanks, and happy Father's Day to all the dads!
06-19-2016, 07:18 AM
Post: #2
 Thomas Radtke Senior Member Posts: 746 Joined: Dec 2013
RE: HP 35s Logic Operator AND
(06-19-2016 06:37 AM)MNH Wrote:  I'm burning some serious midnight oil here in Florida. I just started programming the HP 35s this month. How would I use the logic operator AND to test two flags?
In program mode, use something like this:
Code:
 ; first flag FS? XY GTO B001 0 GTO C001 LBL B 1 LBL C ; second flag FS? AB GTO D001 0 GTO E001 LBL D 1 LBL E ; AND AND
Untested, also you don't actually need more than one lable as you can branch to any line within a program.
06-19-2016, 01:40 PM (This post was last modified: 06-19-2016 01:54 PM by Dieter.)
Post: #3
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP 35s Logic Operator AND
(06-19-2016 06:37 AM)MNH Wrote:  I'm burning some serious midnight oil here in Florida. I just started programming the HP 35s this month.

The 35s AND is a bitwise AND, not a logical AND. So 123 AND 45 is 41:

Code:
123 = 01111011 bin  45 = 00101101 bin -------------- AND = 00101001 bin     =       41 dec

(06-19-2016 06:37 AM)MNH Wrote:  How would I use the logic operator AND to test two flags?

As pointed out, you cannot use AND or OR or NOT for logical operations. But in the olden days the good RPN programmer knew how to handle this. You can combine two or more tests to simulate some logical operations. If you have one test A directly followed by another one B, this combination simulates a (NOT A) OR B. For instance X=0? FS? 1 is equivalent to a test whether X does not equal zero OR flag 1 is set.

An AND is somewhat trickier. Usually you would write the program differently to avoid this and use an OR test like the one above instead. Testing if both of two flags are set usually requires a FC? command which the 35s lacks. But since it has line number addressing it can be done this way. The example does a linear regression if both flag 1 and 2 are set:

Code:
A001 LBL A A002 FS? 1 A003 GTO A005 A004 GTO A007 A005 FS? 2 A006 L.R. A007 ...

Or, with just one jump:
Code:
A001 LBL A A002 CF 0 A003 FS? 1 A004 FS? 0 A005 GTO A008 A006 FS? 2 A007 L.R. A008 ...

The flag 0 test can be anything that tests false. So if you know that at this point the value in X cannot be negative you could also use "X<0?" instead of "FS? 0" and remove the initial CF 0 line. (Note to HP67/97 users: two consecutive tests of flag 2 or 3 yield a FC? test since these two flags are cleared when tested.)

Yes, this uses another trick on the same kind: Have a test followed by another one that tests false means that the first test is inverted.

For more information on this take a look at the old forum, e.g. here.

(06-19-2016 06:37 AM)MNH Wrote:  Also, in the absence of IF-ELSE and IF-THEN-ELSE statements, is there a better alternative than branching to subroutines? Thanks, and happy Father's Day to all the dads!

There is no need for subroutines. Simple GTOs will do.
Example: if x>0 compute ln²(x), else compute 2 · ex

Code:
A001 LBL A A002 X>0? A003 GTO A008 -----+ A004 e^x           | A005 2             | A006 x             | A007 GTO A010 --+  | A008 LN  <------|--+  A009 x²         | A010 ... <------+

Dieter
06-19-2016, 02:28 PM (This post was last modified: 06-19-2016 02:34 PM by Dieter.)
Post: #4
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP 35s Logic Operator AND
(06-19-2016 07:18 AM)Thomas Radtke Wrote:  In program mode, use something like this:
(...)
Untested, also you don't actually need more than one lable as you can branch to any line within a program.

This looks a bit complicated. I assume you want to do something like this:

Code:
0 FS? 1 e^x   // or any other command that turns a 0 into a 1 0 FS? 2 e^x AND

Or even better, using the 35s NOT operator:

Code:
0 FS? 1 NOT 0 FS? 2 NOT AND

This yields a 1 resp. –1 if both flags are set, and a 0 otherwise. So the next step after this is a simple X≠0? which actually tests whether both flags are set. Or you could also use an X=0? instead if you want to test if at least one flag is cleared.

This method can be extended to many more logical combinations. The basic idea is: have the tests return a 0 if false or a 1 or -1 if true, and then combine their results with the 35s logic operators.

Dieter
06-19-2016, 04:40 PM
Post: #5
 Thomas Radtke Senior Member Posts: 746 Joined: Dec 2013
RE: HP 35s Logic Operator AND
(06-19-2016 02:28 PM)Dieter Wrote:  This looks a bit complicated.
This was not intended, actually the code should have been as easy to read as possible to be educative to the TO.

Your solutions are of course superior.
06-19-2016, 11:37 PM
Post: #6
 MNH Junior Member Posts: 44 Joined: May 2014
RE: HP 35s Logic Operator AND
Thank you Thomas and Dieter for your help!
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)