[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Exclusive Or in Applesoft?
Raymond Wiker replied:
>mjmahon@aol.com (Michael J. Mahon) writes:
>
>> I hastily wrote:
>>
>>>Jayson Smith asked:
>>>
>>>>Is there any way to Exclusive Or in Applesoft or does this have to be done
>>>>in machine code?
>>>
>>>There is no exclusive-or operator in Applesoft.
>>>
>>>If you need to exclusive-or single boolean values, then you can do
>>>it with an expression: NOT (B1 = B2).
>>
>> I was thinking a little _too_ boolean!
>>
>> Obviously B1 <> B2 will do the job faster.
>
> Or even (B1 + B2) = 1... I think this formulation would be the
>simplest to expand into three or more terms.
Your reply got me wondering about "expand(ing) into three or more
terms", and I'm not sure I get your point.
For three terms, my suggestion would become:
(B1 <> B2) <> B3
and for four it would be:
(B1 <> B2) <> (B3 <> B4)
which seems pretty straightforward.
In fact, since in the boolean case, <> is distributive, the parentheses
are irrelevant, and I must retract my earlier statement that Applesoft
does not have an exclusive-or operator--for boolean variables (0 or 1),
"<>" is the exclusive-or operator:
B1 <> B2 <> B3 <>...Bn
Using addition, as you suggest, could also be done, but if there
are more than two operands, then the sum must be tested for
being odd, rather than equality with one, which is rather slow in
Applesoft (having no MOD operator).
A simple test case:
100 REM Test Exclusive-OR
110:
120 PRINT B1" "B2" "B3" "B4" "B1 <> B2 <> B3 <> B4
130 B1=NOT B1:IF NOT B1 THEN B2=NOT B2:IF NOT B2
THEN B3=NOT B3:IF NOT B3 THEN B4=NOT B4:
IF NOT B4 THEN END
140 GOTO 120
Line 120 displays the inputs and the result, and line 130 is
a "ripple counter" for the boolean variables.
-michael
New Applesoft BASIC interface for NadaNet networking!
Home page: http://members.aol.com/MJMahon/