HP Forums

Full Version: (15c)(35s)(some other RPN) Flags toggle / inverting
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

I tried the following four methods to toggle Flag1 (or Reg1 for the third one), each of them having pros and cons.

Method 1: sacrifice another flag
Desc: memorize the status of the first flag with a second one.
Pros: stack or Regs unchanged, only 6 steps
Cons: uses another flag
Code:

CF 2
FS? 1
SF 2
SF 1
FS? 2
CF 1

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

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
Cons: modifies the stack, use a register, not a flag operation, assume to have R1 correctly initialized with 0 or 1
Code:

RCL 1
CHS
1
+
STO 1
Rv

Method 4: sacrifice the labels
Desc: use GTO instructions to branch
Pros: preserves other flags, stack and registers, 5 steps on a 35s
Cons: uses conditional branching, perfect on a 35s but consuming LBLs on a 15c
Code:

; HP 15c style
FS? 1
GTO 0
SF 1
GTO 1
LBL 0
CF 1
LBL 1

; HP 35s style
A002 FS? 1
A003 GTO A006
A004 SF 1
A005 GTO A007
A006 CF 1
A007 ...

What do you think?
I'm sure some of you have other methods, or a favorite one.

Thibault
… searching for the best method to toggle a flag, ie to invert it … conventional language that implements booleans … FLAG1 := NOT(FLAG1) … But in RPN, there is no way to do this easily…

The (some other RPN) refrenece in your post omits the 41 & its' inherent command(s):
FC?C Check whether flag nn is clear and then clear
&
FS?C Check whether flag nn is set and then clear
Simply a technical point of reference.

BEST!
SlideRule
(04-26-2018 04:02 PM)SlideRule Wrote: [ -> ]The (some other RPN) refrenece in your post omits the 41 & its' inherent command(s):
FC?C Check whether flag nn is clear and then clear
&
FS?C Check whether flag nn is set and then clear

FS?C commands were already available on the 67/97: testing flags 2 and 3 also cleared them, so in effect it's a FS?C 2 and FS?C 3.

But this alone does not toggle a flag. Here the solution is:

FC?C nn
SF nn

This toggles flag nn with just two commands, i.e. with a minimum number of steps.

As long as there is not a dedicated "flip" command, that is: the WP34s has a FF command (flip flag) that does exactly this. ;-)

Dieter
(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
(04-26-2018 06:21 PM)Dieter Wrote: [ -> ]Or on the 15C even just 3 steps, and with one stack level less

I won't bet! Very nice, thanks.
Reference URL's