Available on: all targets

XOR

Performs a logical exclusion on two expressions, as a bitwise exclusion. For comparisons managed as a boolean result (TRUE or FALSE), result is TRUE if both expresions evaluate differently each other. The following table shows how result is determined:

TRUE XOR TRUE = FALSE
TRUE XOR FALSE = TRUE
FALSE XOR TRUE = TRUE
FALSE XOR FALSE = FALSE


Generally speaking, the XOR operator performs a bitwise comparison of the bits of two numeric expressions and sets the corresponding bit in result according to the previous table.

Note that ugBASIC uses the convention, very common in BASICs of the 1970s and 1980s, of considering Boolean logic as implemented through the so-called "two's complement".

In other words, the value FALSE is associated with a number composed of all 0s, in terms of bits. The value TRUE is, instead, associated with a number composed of all 1s, again in terms of bits. According to the 2's complement representation, a number composed of all ones is always equivalent to the number -1, regardless of how many bits the number is composed of, while a number composed of all zeros is always equivalent to zero.

According to this convention, there is a coincidence between bitwise and logical operations: in fact, a bitwise XOR, applied to all the bits of the number, will be equivalent to the logical operation. Note that the XOR operator always evaluates both expressions, which can include executing routine calls.

Because the logical and bitwise operators have lower precedence than other arithmetic and relational operators, all bitwise operations must be enclosed in parentheses to ensure accurate results.

If the operands consist of a SIGNED BYTE expression and a numeric expression, converts the SIGNED BYTE expression to a numeric value (-1 for TRUE and 0 for FALSE) and performs a bitwise operation. So, the data type of the result is a numeric type appropriate for the data types of both expressions.

SYNTAX

 = x OR y


Legend
  • id : identifier
  • type : datatype
  • v : value
  • "..." : string
  • [...] : optional

EXAMPLE

 IF x XOR x THEN: PRINT "never executed" : ELSE : PRINT "always executed": ENDIF


ABBREVIATION: Xr

Join BASIC 10Liner Contest with ugBASIC!

An interesting competition is held at the beginning of each year: the BASIC 10Liner Contest. It is possible to use ugBASIC to participate in the next "BASIC10Liner" competition, in the following categories:

  • PUR-120 - A game in 10 lines of max 120 characters (w/abbrev.)
  • EXTREME-256 - A game in 10 lines of max 256 characters (w/abbrev.)
  • SCHAU - Any program in 10 lines of max 256 characters (w/abbrev.)
In order to reduce space you can use this abbreviation for this instruction:

XOR ↔ Xr

Any problem?

If you have found a problem with this keyword, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO KEYWORDS