Help Center
Help CenterFreeBASIC

Operator Xor (Exclusive Disjunction)operator

Returns the bitwise-xor (exclusive disjunction) of two numeric values

Operatorsoperatordocumented

Syntax

Declare Operator Xor ( ByRef lhs As T1, ByRef rhs As T2 ) As Ret

Parameters

NameDescription
lhsThe left-hand side expression.
T1Any numeric or boolean type.
rhsThe right-hand side expression.
T2Any numeric or boolean type.
RetA numeric or boolean type (varies with
T1and
T2).

Return value


Returns the bitwise-xor of the two operands.

Description


This operator returns the bitwise-exclusion of its operands, a logical operation that results in a value with bits set depending on the bits of the operands (for conversion of a boolean to an integer, false or true boolean value becomes 0 or -1 integer value).

The truth table below demonstrates all combinations of a boolean-exclusion operation:

Lhs BitRhs BitResult
000
101
011
110


No short-circuiting is performed - both expressions are always evaluated.

The return type depends on the types of values passed. Byte, UByte and floating-point type values are first converted to Integer. If the left and right-hand side types differ only in signedness, then the return type is the same as the left-hand side type (T1), otherwise, the larger of the two types is returned. Only if the left and right-hand side types are both Boolean, the return type is also Boolean.

This operator can be overloaded for user-defined types.

Remarks

Usage


result = lhs Xor rhs

Dialect Differences


  • In the -lang qb dialect, this operator cannot be overloaded.

Differences from QB


  • None

See also


Example


' Using the XOR operator on two numeric values

Dim As UByte numeric_value1, numeric_value2

numeric_value1 = 15 '00001111

numeric_value2 = 30 '00011110



'Result =  17  =     00010001

Print numeric_value1 Xor numeric_value2

Sleep


' Using the XOR operator on two conditional expressions

Dim As UByte numeric_value1, numeric_value2

numeric_value1 = 10

numeric_value2 = 15



If numeric_value1 = 10 Xor numeric_value2 = 20 Then Print "Numeric_Value1 equals 10 or Numeric_Value2 equals 20"

Sleep



' This will output "Numeric_Value1 equals 10 or Numeric_Value2 equals 20"

' because only the first condition of the IF statement is true


Reference

  • Documented in KeyPgOpXor.html