DominO123 Posted June 4, 2004 Report Share Posted June 4, 2004 sebastia... Sip's answer to that will be that he met "one"-- at office depot -- but it didnt have a good/reliable hard drive--- if u dotn get it...just forget it- im not making sense anymore.... To the contrary dear, I think you are making more sense than ever. Quote Link to comment Share on other sites More sharing options...
Harut Posted June 30, 2004 Report Share Posted June 30, 2004 You have 2 integers A and B How do you swap their values without using any temporary variables. permanent = A A = B B = permanent j/k Quote Link to comment Share on other sites More sharing options...
Twilight Bark Posted June 30, 2004 Report Share Posted June 30, 2004 I am not sure if Sipchik or anyone else has asked this question. Most likely they have. But I will give it another try today. You have 2 integers A and B How do you swap their values without using any temporary variables. A = A + B B = A - B A = A - B Quote Link to comment Share on other sites More sharing options...
Sasun Posted June 30, 2004 Report Share Posted June 30, 2004 A = A + B B = A - B A = A - B that's the coolest thing I have seen lately Quote Link to comment Share on other sites More sharing options...
Sip Posted June 30, 2004 Author Report Share Posted June 30, 2004 TB is obviously thinking like a mathematician! The more computery dude would probably come up with A = A xor B B = A xor B A = A xor B In C/C++ or Java, you can even write it as A ^= B ^= A ^= B; Conceptually there is really no difference between the arithmetic version and the logical version but as it turns out, this one can be made much faster in hardware. Also, in the strict "binary" bit-wise sense of the contents of A and B, the XOR method will swap the bits while the arithmetic version requires some notion of what addition and subtraction mean. It clearly works for integers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.