Skip to main content
GameDev.net gamedev.net
🔒 Locked

Swapping values

Started by nomibegood Jun 13, 2002 at 4:43 AM 6 replies 350+ views
Original Post
nomibegood
nomibegood
Thanks alot [edited by - nomibegood on June 13, 2002 7:49:52 AM]
Useful
Useful
cant
ALiENiD
ALiENiD

  
x ^= y;
y ^= x;
x ^= y;


That should do it (it''s even faster than using a temporary value if I don''t recall wrong).

/ALiENiD
nomibegood
nomibegood
quote:
Original post by Useful
cant


yes my friend you can there is a way , i can''t figure it out though anyway thanks for reply..

felonius
felonius

You *can* do so:

x = x+y;
y = x-y;
x = x-y;

or for a longer discussion see this thread:

http://www.gamedev.net/community/forums/topic.asp?topic_id=98678

Jacob Marner, M.Sc.Console Programmer, Deadline Games
nomibegood
nomibegood
quote:
Original post by ALiENiD


    
x ^= y;
y ^= x;
x ^= y;


That should do it (it''s even faster than using a temporary value if I don''t recall wrong).

/ALiENiD

Thankyou very much for the reply ALiENiD I will try

nomibegood
nomibegood
quote:
Original post by felonius

You *can* do so:

x = x+y;
y = x-y;
x = x-y;

or for a longer discussion see this thread:

http://www.gamedev.net/community/forums/topic.asp?topic_id=98678




Thanks for the reply felonius

Serge K
Serge K
quote:
Original post by ALiENiD


    
x ^= y;
y ^= x;
x ^= y;


That should do it (it''s even faster than using a temporary value if I don''t recall wrong).

/ALiENiD

No, it''s not faster.
Any modern compiler can optimize away temporary variable and keep it all in the registers.
Swapping with XOR creates dependency between CPUs instructions - this code cannot be parallelized, which makes it bad for modern superscalar CPU.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.