[Tutor] Variable Swap
Steve Nelson
sanelson at gmail.com
Tue Jan 30 13:09:22 CET 2007
On 1/29/07, Kent Johnson <kent37 at tds.net> wrote:
> Because 2 ^ 3 == 1, right? Are you sure you understand what xor does? It
> is a bitwise exclusive or:
Yes... at a binary level, it returns true if either input is true, but not both:
A B Q
0 0 0
0 1 1
1 0 1
1 1 0
Thus it has the effect of swapping / reversing.
For some reason I had not been thinking straight, and had thought we
might just see a toggle of the value of the variable, which is, of
course, nonsense.
So in my example:
2 = 00000010
3 = 00000011
xor:
Q = 00000001
Lesson: think first... was just me being blind.
> x, y = y, x
Doesn't this use temporary variables?
> Kent
S.
More information about the Tutor
mailing list