   Bitwise XOR vs. Temp variable question
  In another discussion I had posted a little example of switching variables using a bit-wise XOR operator (in this case, I had used it in a situation where it doesn''t work), in what way is this less efficient than using the temp variable method (once again assuming you _can_ use it )?  Because of the many facets of effeciency, and my finding things all over the place (including gamedev.net) I was wondering exactly why it is that it''s more efficient to code the temp variable as I was told the post here: http://www.gamedev.net/community/forums/topic.asp?topic_id=208240.Note:  I''m not challanging anyone''s statements there, I just want further clarification and understanding.  I''m creating this topic because the other was off-topic and I didn''t want to clutter it with something else that was going on.  If you''d rather not create new topics like this, then I will do that from now on if you say so. 
  You cant use the XOR method on types that dont support the XOR operation.Efficiency should not be a concern here: the operation is too small and is probably overshadowed by other functions. Use a profiler before you try to use tricks for efficiency''s sake.   Colin Jeanne | Invader''s Realm"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud ;  You might as well read this recent thread which pretty much covers XOR completely. ;  Basically why the XOR version is slower is because it works against the instruction level parallel execution performed by modern processors by creating a chain of instructions where each successive instruction depends on the result of the computation in a previous instruction. In contrast the temp variable method contains a more open data flow structure, where instructions can operate in parallel. The XOR trick is (quite literally) a textbook example of working against ILP in super scalar architectures. ;  The ''XOR trick'' only works correctly for integer variables. No floats, no user-defined data types. Especially not if they have nontrivial copy semantics. Look at how the C++ swap function (yes, it''s in the library) is implemented.It hides the intent of your code for the sake of a  micro-optimization. Compilers can be programmed to recognize that pattern and generate the proper code, such as a an x86 XCHG, which does *not* use an extra register. Even if it did, and you were *that* worried about performance, you''d have to balance the costs of locking an ALU (possibly the only you have) for 3 cycles v.s. using an extra register for 2 cycles. Before you make efficiency claims, make sure you''ve done your research.And Oluseyi already pointed out the same quote that is in my signature (though I honestly believe it is attributed to Kernighan, not Ritchie). "Real" code has to be not only debugged, but also maintained ... and not necessarily by you. Cleverness for its own sake only leads to trouble. At least, ''clever'' code, especially if you find a killer way to do some common operation, should be isolated in a library, not peppered all over your code base.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  Thanks that''s exactly what I was looking for.  I didn''t make any efficiency claims.  Other people are saying that it''s faster (even some saying that it''s only marginally faster), and then I''m told that it''s not.  I was just looking for a why is all.Honestly, after finding out about std::swap, I figure I''ll use that as long as I can or until I find something better (easier!).  I''ll probably take a look at std::swap tomorrow during class and see what it''s doing.  I''m printing out some stuff on it right now.  Thanks for the tip.Really, if you think I''m attacking you or I''m trying to be cocky, please be assured that I am not.  I just don''t take an answer without an explination, so just saying "That doesn''t work" is something I''m going to want a why along with.  I know I don''t have all the answers, and I don''t care about being wrong or making mistakes as long as I can learn from them.  Because of that, people may take me for being arrogant or jerk-ish when I''m really just being quizzical and wanting to know more - especially from you guys that have tons more experience than I could dream of having at this point.To be honest, my C++ classes were a joke.  I was able to do all 13 labs and the final project without even trying.  I should think that someone who could do little more than a guessing game program (not even really using classes properly) would have much to learn.  I didn''t.  Things like pointers and what-not weren''t heavily covered and string operations were slightly glazed over.  We didn''t even touch the STL except for cout, cin, and cin.get.  Shoot, I still have to learn data structures on my own because that professor doesn''t even know what she''s talking about.  She teaches straight from the book, and if you ask her a question the response you get is "Did you read the book?  If you did, you''d get it because I get it.  If it''s that tough, maybe you''re in the wrong major." So if I have inane practices, bad implementations, or are just plain off, please correct me.  Fortunately, the Web Programming and Database teachers are good, so I know that stuff well.  Overall, I''d say I''m dissapointed at the end result of my education.So I guess if I sound like a jerk, I apologize.  The only successful programming experience has been learning from a system I''m using to make a game now called BYOND and I''ve learned enough of DirectX to eek out a Tetris Clone.  Aside from that, I''d say I''m completely (as in 100%) lacking in advanced topics.(Sorry for the long post.  If you read it all, thanks ) ;  excuse me but WTF?The XOR trick will work for more than just integer types if you use it at a byte level, Or in a way that you don''t in fact care about what you are swaping. Just that it need to be swaped.I''ve done it using about 3 different compilers. Granted the microsoft visual compilers are not among those three but a crappy microsoft c complier is.And its worked for large structors of data.The reason I used it was for space considerations. PalmOS and hand held barcode scanners.I''m not saying this to bost, are to say "I''m better than you..". I''m saying this in because in specific situations you need to code cleverly, in wich case knowing tricks such as this is _very_ usful and may work. The only way to find out ofcourse is to try it. Armand -------------------------It is a good day to code. ;  True, its probably handy to know, but for the most part you might as well forget about it for the reasons that SiCrane and Fruny outlined, certainly when starting out.If you need to use it, like you did, then great, if you havent got a convincing case for wanting to use it then go with something clearer, it will be easier to maintain.;  The only thing it''s really good for is demonstrating that you know a thing or two about binary :/ ;quote:Original post by Armand  excuse me but WTF?The XOR trick will work for more than just integer types if you use it at a byte level, Or in a way that you don''t in fact care about what you are swaping. Just that it need to be swaped.Yes, but to do that you have to decompose the object into bytes and XOR the individual bytes. In which case you might as well have used a temporary.Of course, you could XOR the addresses of the variables, but doing so may yield undesirable semantics. In short, no. 