Horusmaster Wrote:wow my comp teacher told us last year that we cant swap variable without assigning a temporary variable, i guess he's wrong!I'm not sure if this applies to coding as well, but on a graphing calculator, using xor (or any boolean operation) on integers is the same as converting them to binary and applying it to each respective digit.
but i don't get how xor works.
isn't xor for boolean operations only?
if a xor b = 2 then
that means if a = 2 or b =2 but not both.
i don't get how it works for changing the number though.
any1 wanna explain?
E.g. 17 xor 54
17 = 10001_2
54 = 110110_2
10001_2 xor 110110_2 = 100111_2
100111_2 = 39
So 17 xor 54 = 39.
The variable swapping would go like this (not the actual code, but showing the process):
a = 17
b = 54
a = 17 xor 54 = 39
b = 39 xor 54 = 17
a = 39 xor 17 = 54
Anyway, Stereo's method accomplishes the same thing using addition/subtraction.
(Lemme guess, Fibonacci numbers?)

