Export (0) Print
Expand All

Xor Operator

Performs a logical exclusion operation on two Boolean expressions, or a bitwise exclusion on two numeric expressions..

result = expression1 Xor expression2

Parts

result
Required. Any Boolean or numeric variable. The result for Boolean comparison is the logical exclusion of two expressions. For bitwise operations the result is a numeric value resulting from the bitwise exclusion of two numeric expressions
expression1
Required. Any Boolean or numeric expression.
expression2
Required. Any Boolean or numeric expression of the same type as expression1.

Remarks

For Boolean comparisons, if one and only one of the expressions evaluates to True, result is True. Otherwise, result is False. If either expression is stated as Nothing, that expression is evaluated as False.

If expression1 is And expression2 is Then result is
True True False
True False True
False True True
False False False

For numeric expressions, the Xor operator performs as a bitwise operator. A bitwise comparison of two expressions using exclusive-or logic to form the result, as shown in the following table:

If bit in expression1 is And bit in expression2 is Then result is
0 0 0
0 1 1
1 0 1
1 1 0
Note   Since the logical/bitwise operators have a lower precedence than other arithmetic and relational operators, any bitwise operations should be enclosed in parentheses to ensure accurate execution.

If the operands consist of one Boolean expression and one numeric expression, the result Boolean expression will be converted to a numeric value (-1 for True, and 0 for False) and the bitwise operation will result.

Example

This example uses the Xor operator to perform logical exclusion on two expressions. The result is a Boolean value representing whether only one of the expressions is true.

Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Boolean
myCheck = A > B Xor B > C   ' Returns False.
myCheck = B > A Xor B > C   ' Returns True.
myCheck = B > A Xor C > B   ' Returns False.

This example uses the Xor operator to perform logical exclusion of the individual bits of two numeric expressions. The bit in the result pattern is set if only one of the corresponding bits in the operands are set.

Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Integer
myCheck = (A Xor B)   ' Returns 2.
myCheck = (A Xor C)   ' Returns 12.
myCheck = (B Xor C)   ' Returns 14.

See Also

Logical/Bitwise Operators | Operator Precedence in Visual Basic | Operators Listed by Functionality | Logical Operators

Show:
© 2014 Microsoft