XOR operator (bitwise)

Performs a bitwise exclusive OR operation on two expressions of the same flag enum type and returns a new instance of the same type.

Syntax

flag-enum-expression XOR flag-enum-expression
flag-enum-expression
An expression whose value is a flag enum instance. Both expressions must be the same flag enum type, and neither expression can be the Unknown value (?).

Example

When you perform an XOR operation on two flag enum instances, the resulting instance has a flag set if the state of that flag was different for the original two expressions. For example, using this enum type:

ENUM Alignment FLAGS:
    DEFINE ENUM Left
                Right
                Top
                Bottom.
END ENUM.

The following code fragment shows examples of two different XOR operations.

DEFINE myAlignment AS Alignment.

myAlignment = Alignment:Left.
/* The initial value for myAlignment has the Left set. */

myAlignment = myAlignment XOR Alignment:Top.
/* After the first XOR operation, myAlignment has both the Left and Top
   flags set because initially, myAlignment has the Left flag set and
   Alignment:Top does not, and vice versa for the Top flag. */

myAlignment = myAlignment XOR (Alignment:Top OR Alignmen:Bottom).
/* myAlignmnet now starts with the Left and Top flags set, and the enum
   instance created by the OR operation has both the Top and Bottom
   flags set. After the XOR operation, myAlignment has the Left and
   Bottom flags set because the status of those flags are different in
   the two enum instances operated on by the XOR.  */

The first XOR operation in the previous example shows how you can use bitwise XOR to toggle a flag on and off. Performing an XOR operation on myAlignment with an enum instance that has only one flag set will result in an instance with that flag turned on if if it was off in myAlignment or off it was turned on in myAlignment.

The second XOR operation shows how you can use bitwise XOR to toggle the status of two flags that you want to be mutually exclusive. When myAlignment starts with either Top or Bottom set, but not both, using the XOR operation with the enum instance created by the OR operation switches the status of the two flags.

Notes

See also

AND operator (bitwise), ENUM statement, NOT operator (bitwise), OR operator (bitwise)