The Xor operator performs a bitwise exclusive Or between the two specified operands.
XOR RULE
1 or 1 = 0 1 or 0 = 1 0 or 1 = 1 0 or 0 = 0
So when we XOR a pair of bits, we only get a ONE result when the bits are different, it'll return a zero when the bits are the same.
XOR is most commonly used in IF decisions where we want to ensure that at only one side of the expression is true (1) before executing the code inside.
; Create two variables A=100 B=200 ; Use XOR to detect ensure we only execute the code ; inside the IF. The XOR will merges the ; expression conditions, so as long as only ; one side of the expressions is TRUE (1) ; it'll run the code inside the IF/ENDIF ; Test #1, If A=100 Xor B=200 Print "A either ='s 100 or B ='s 123456" Else Print "FAILED TEST1" EndIf ; In this, bot the first and second expressions ; are TRUE. When the two states are XOR'd ; together the state will become FALSE ; making the IF FAIL and run the PRINT "FAILED TEST" ; Test #2, If A=100 Xor B=123456 Print "TEST 2 - SUCCESS" EndIf ; In this IF, the First exporession is TRUE ; and the second is FALSE. When these states ; are XOr'ed together, we get a TRUE and it runs ; the code in the if/endif Sync WaitKey |
FACTS:
* Instead of the keyword Xor you can also use the symbol ~
* Xor if often used in simple data encrpytion systems.
* Most graphics rendering commands include Xor blending rules. To set use InkMode
Mini Tutorial #1:
Bitwise operations And, Or and Xor
; bitwise And Print Bin$(43) + " And" : Print Bin$(58) + " =" Print Bin$(43 And 58) Print "" ; bitwise Or Print Bin$(43) + " Or" : Print Bin$(58) + " =" Print Bin$(43 Or 58) Print "" ; bitwise Xor Print Bin$(43) + " Xor" : Print Bin$(58) + " =" Print Bin$(43 Xor 58) ; Display the Screen and wait for the user to press a key Sync WaitKey |
This example would output. %00000000000000000000000000101011 And %00000000000000000000000000111010 = %00000000000000000000000000101010 %00000000000000000000000000101011 Or %00000000000000000000000000111010 = %00000000000000000000000000111011 %00000000000000000000000000101011 Xor %00000000000000000000000000111010 = %00000000000000000000000000010001 |
Mini Tutorial #2: Encryption Example
This snippet uses XOR to both scramble and restore a text message.
|