# 01-swap.py
# Topic: Swap Without a Third Variable, XOR Swap

input("XOR swap - exchange two values without a third variable.  Press Enter ")
print("  before: a = 5   b = 9")
a, b = 5, 9
a ^= b; b ^= a; a ^= b
print("  after:  a =", a, "  b =", b)

n = int(input("Enter a number (try 3 or 7): "))
guess = input("After XOR swap of " + str(n) + " and 8 what does n become? ")
a, b = n, 8
a ^= b; b ^= a; a ^= b
input("XOR swap exchanges the values.  Press Enter ")
print("  n became:", a, "  your guess:", guess)



# 03-sign-detect.py
# Topic: XOR for Sign Detection

input("XOR sign detection - n ^ m < 0 means different signs.  Press Enter ")
print("  4 ^ 2 =", 4 ^ 2, "  same signs  positive")
print("  4 ^ -2 =", 4 ^ -2, "  different signs  negative")

n = int(input("Enter a number (try 5 or -3): "))
guess = input("Will " + str(n) + " ^ -8 be positive or negative? ")
input("XOR is negative when signs differ.  Press Enter ")
print(" ", n, "^ -8 =", n ^ -8, "  your guess:", guess)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: