Easy programming puzzle

Joined
Sep 21, 2022
Messages
153
Reaction score
21
Variables X and Y hold integers.

Swap the values of X and Y without using a third variable.
Code:
X=(X xor Y)
Y=(X xor Y)
X=(X xor Y)
 
Joined
Jul 4, 2023
Messages
460
Reaction score
55
;)
Code:
X = X + Y  # Now X holds the sum of X and Y
Y = X - Y  # Subtract Y from the sum, so Y becomes the original X
X = X - Y  # Subtract the new Y (original X) from the sum, so X becomes the original Y
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,904
Messages
2,570,003
Members
46,360
Latest member
HelaineO50

Latest Threads

Top