Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
swapping variables
#7
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!

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?
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.

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?)
Reply


Messages In This Thread
swapping variables - by GummyBear - 2008-10-24, 02:58 AM
swapping variables - by loddlaen - 2008-10-24, 03:02 AM
swapping variables - by Fiel - 2008-10-24, 03:05 AM
swapping variables - by Stereo - 2008-10-24, 09:32 AM
swapping variables - by GummyBear - 2008-10-27, 12:29 AM
swapping variables - by Horusmaster - 2008-10-27, 12:39 AM
swapping variables - by Russt - 2008-10-27, 01:36 AM
swapping variables - by Horusmaster - 2008-10-27, 02:00 AM
swapping variables - by Russt - 2008-10-27, 02:02 AM
swapping variables - by Horusmaster - 2008-10-27, 02:07 AM
swapping variables - by Nikkey - 2008-10-27, 02:15 AM
swapping variables - by Russt - 2008-10-27, 02:21 AM
swapping variables - by Orit - 2008-10-27, 02:54 AM
swapping variables - by Nikkey - 2008-10-27, 04:51 AM
swapping variables - by Stereo - 2008-10-27, 07:53 AM
swapping variables - by Orit - 2008-10-27, 09:21 AM
swapping variables - by Nikkey - 2008-10-27, 10:17 AM
swapping variables - by Orit - 2008-10-27, 10:49 AM
swapping variables - by Nikkey - 2008-10-27, 11:59 AM
swapping variables - by Devil - 2008-10-27, 12:03 PM
swapping variables - by Orit - 2008-10-27, 12:18 PM
swapping variables - by Nikkey - 2008-10-27, 12:18 PM
swapping variables - by GummyBear - 2008-10-27, 07:33 PM

Forum Jump:


Users browsing this thread: