XOR - QB64 Wiki

XOR

From QB64 Wiki

Jump to: navigation, search

The XOR numerical operator returns the bitwise exclusive OR value where only bits that differ return a bit value.


Contents

Syntax

result% = value1% XOR value2%


  • Given two whole integer type values XOR returns an integer with the property that each bit returns:
  • a bit value of 0 if both of the corresponding bit values compared are off or on.
  • a bit value of 1 if one of the two compared bit values is on and the other is off.
  • XOR can be used to turn off bits that are on or turn on bits that are off, like toggling the bit values.
  • Byte (8 bit) values can be from 0 to 255. 0 = all bits off. 255 = all bits on.


The results of the bitwise logical operations, where A and B are operands, and T and F indicate that a bit is set or not set:
Operands Operations
A B NOT B A AND B A OR BA XOR BA EQV BA IMP B
T T F T T F T T
T F T F T T F F
F T F F T T F T
F F T F F F T T
Relational Operations return negative one (-1, all bits set) and zero (0, no bits set) for true and false, respectively.
This allows relational tests to be inverted and combined using the bitwise logical operations.



Example 1: Comparing the results of XOR, AND and OR value comparisons on one byte number values up to 255.

' bit# 76543210 also the exponent of 2 value on ' -------- a% = 233 ' binary 11101001 values used b% = 111 ' binary 01101111 ' -------- result1% = a% XOR b% ' 10000110 binary Result1% = 134 result2% = a% AND b% ' 01101001 binary Result2% = 105 result3% = a% OR b% ' 11101111 binary Result3% = 239

Explanation: Look down the columns to see how the bits are changed. The three values returned by XOR, AND and OR are relational. If you add the values returned by XOR(134) and AND(105) the result is the value of the OR operation, 239.


Example 2: Using XOR to turn a parallel port's data register bits on or off

address% = 888 ByteValue% = INP(address%) OUT address%, ByteValue% XOR 7

Explanation: Only the first 3 Bits are affected. If on then turned off. If off then turned on. Each time the OUT statement above is used, the bit values alternate. That is because the value of 7 compares the bit's status to an on only value.


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Views
  • Page
  • Discussion
  • View source
  • History
Personal tools
  • Log in
Toolbox
  • What links here
  • Related changes
  • Special pages
  • Printable version