Post Reply 
(15c)(35s)(some other RPN) Flags toggle / inverting
04-26-2018, 06:21 PM (This post was last modified: 04-26-2018 06:27 PM by Dieter.)
Post: #4
RE: (15c)(35s)(some other RPN) Flags toggle / inverting
(04-26-2018 03:20 PM)pinkman Wrote:  I've been subject to terrible headaches while searching for the best method to toggle a flag, ie to invert it.
In any conventional language that implements booleans, the code would be :
FLAG1 := NOT(FLAG1)

But in RPN, there is no way to do this easily, while it would be very convenient.

This is not related to RPN. Only to the available command set of the particular calculator. For instance, some calculators have flag tests that clear the flag (after the test has been done). These are usually labelled FS?C (flag set? clear afterwards). Others also feature a FC?C (flag clear? clear afterwards). And there even are cases with a dedicated flip command.

Examples:

1. RPN-calculator WP34s
FF 01

Doesn't get shorter than this: a dedicated "flip flag" command.

2. RPN-calculator HP-41C/CV/CX
FC?C 01
SF 01

Two lines, short and effective.

3. RPN-calculators HP67 and 97:
F? 2
F? 2
SF 2

On the 67/97 the tests for flag 2 and 3 also clear the flag afterwards. The sequence above toggles flag 2. On the other hand every following test of this flag also clears it... #-)

But this can be another method for the HP41:
FS?C nn
FS? nn
SF nn

Yes, the second test can be a simple FS?. But, as already noted, there is an even shorter method (cf. 2.).

(04-26-2018 03:20 PM)pinkman Wrote:  Method 2: sacrifice the stack
Desc: memorize the status of the flag on stack (X)
Pros: preserves other flags
Cons: modifies the stack, 7 steps
Code:
1
FS? 1
CLX
SF 1
X=0?
CF 1
Rv

There is one more pro: You get a visual feedback of the flag status. Imagine a short routine that toggles a mode setting: print on/off, TVM begin/end mode, etc. Here it's even just four steps since the 0 or 1 remains in X and the final Rv is omitted.

(04-26-2018 03:20 PM)pinkman Wrote:  Method 3: sacrifice a register (and the stack)
Desc: use R1 to simulate a flag, with simple integers algebra
Pros: also works on HP12c, only 6 steps

Only 5 steps:

Code:
1
RCL 1
-
STO 1
Rv

Or on the 15C even just 3 steps, and with one stack level less:

Code:
1
RCL-1
STO 1
Rv

(04-26-2018 03:20 PM)pinkman Wrote:  What do you think?
I'm sure some of you have other methods, or a favorite one.

You bet. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (15c)(35s)(some other RPN) Flags toggle / inverting - Dieter - 04-26-2018 06:21 PM



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