Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

XOR

Keyword Abbreviation Token (hex) Version(s) Classification
XOR X{Shift+O} CE 08 7.0 Function

  Syntax  
XOR ( subject , mask )
 
Parameters Type Legal Value(s) Default Value Note(s)
subject Float or unsigned Integer
0 to 65535
true is illegal!
mask Float or unsigned Integer  0 to 65535    true is illegal!
 
Returns Type Value(s) Note(s)
result unsigned Integer
0 to 65535
Never returns -1
 
  Purpose  
Evaluate a bit-wise boolean operation.

 
  Remarks  
The XOR function (unlike the OR and AND operators) is exclusively used for bit-wise operations (see notes about logical operation below) because it requires unsigned parameters, and it returns an unsigned result.  First, if either parameter is a string, a TYPE MISMATCH ERROR is generated.  Next, each parameter, if floating-point, is converted to an integer; if the result is not a legal value (see above) then ILLEGAL QUANTITY ERROR is generated.  Finally, each of the 16 corresponding bit pairs is evaluated and the corresponding bit in the result is assigned according to this truth table:
 
XOR Mask
0 1
Subject 0 0 1 Result
1 1 0
 
 
For bit-wise operations, it may be helpful to think of the mask as filtering the subject, such that if a mask bit is 0, the result bit is the same as the original subject.  If the mask bit is 1, the result bit will always the complement of the subject bit.  So you might also think of it as selectively flipping bits.  I hope this doesn't confuse you, but another way to look at it is a simple 1-bit addition with wrap-around.  This is different than normal addition because each bit pair is processed individually (instead of all bits being considered a single number).  Yet another way to think of it is as subtraction with wrap-around.
 
Because all BASIC functions and operators that return a boolean value return -1 for true, this function can not be directly used for logical comparisons.  However, it is possible if you apply the ABS function to both parameters.  Although this trick allows XOR to act like a logical operator, a true result would be +1 which might cause problems.  So you might also need to negate the result too.
 
Examples are provided below, but to really understand XOR, you need to know how to convert a decimal number into binary (and back again).  I won't try to explain that here; you may find this web page helpful for experimenting, or use a calculator.
 
The XOR function is occassionally used with IF, DO, and LOOP (but not very often due to the use of UNSGINED integers).
  
Some "logical" examples:
A=1 : B=2

READY.
PRINT XOR(A>0, A>B)

?ILLEGAL QUANTITY ERROR
READY.
PRINT -XOR(ABS(A>0), ABS(A>B))
-1

READY.
PRINT -XOR(ABS(A<0), ABS(A>B))
 0

READY.
 
Some examples demonstrating bit-wise complement:
A=5      : REM binary 0000 0000 0000 0101

READY.
B=3      : REM binary 0000 0000 0000 0011

READY.
PRINT XOR(A,B)
 6       : REM binary 0000 0000 0000 0110

READY.
 
  Compare With  
+, -, OR
 
  Contrast With  
 
  See Also  

© H2Obsession, 2014
Comments