ANSWERS: 12
  • I've never heard that a reliable alternative is possible. At the lowest level one could get the job done by using a register to store a simple value temporarily, but that doesn't avoid the use of a temporary variable, so much as locate the temporary variable more efficiently. (Operating on registers is generally faster than operating on RAM.) Also at the level of assembly there is an operator called XCHG which can be used to do something like what you're asking about. I found this mentioned here: http://www.forth.com/archive/sftalk/2003/2554.html Note that there are performance issues. If one wants to consider a single register as multiple variables, then there are also rotate operations in X86 assembly that could be used. These considerations might be useful in a few circumstances, but generally, most programmers don't work at that level and even if they do, such consideration would not be helpful in a large portion of the circumstances where a variable swap would be desired. For more general programming, you might on some occassions be able to use a non-temporary variable to help out with the swap. This kind of thing is generally faux pax, but when performance becomes critical, as is often the case, I hear, in game programming, the lag caused by allocating memory for a temporary variable can be avoided by broadening its scope. (This is not generally a safe thing to do. Specifically, it is not thread-safe, so it is a technique that also is not very broadly applicable.) This is not something I have ever done and it is a note which I make being fully aware that I have spoken a most grievous heresy. I fairly expect to get docked in my answer rating for having the audacity to have mentioned the technique. Nonetheless, it exists. (I'm really not sure what the performance gains are supposed to be.)
  • (a=5) (b=7) b=b-a (b==2) a=a+b (a==7) b=a-b (b==5) duh..
  • Here are some wayz to swap two variables without using temp variable ---------------------------- a=a+b b=a-b a=a-b -------------------- a=a*b b=a/b a=a/b ----------------- a=a^b b=a^b a=a^b
  • The answer by muthusrinivasan is very nice. As I said, its a very sneaky trick. I really like it. But, before you use it, please think it over before you do. If for some reason you don't have another variable at your disposal, then you could probably use the above method. (Of course only for ints, and maybe for chars with a ascii value of less than 128). Even if you use this, you will still in memory be using extra space. For the operation a = a + b, not only the memory spaces of a and b is used. A tmp is used in anyway to store the value of a + b, before is stores that value in a. So even though you're not using a tmp variable in your source code, it gets compiled to be using a temporary space in memory. And, besides the fact that you're using the same number of memory addresses, you have extra instructions now also. Three extra arithmetic operations. And even though they take only one clock cycle, it is still one clock cycle too much. So, rather stick with the tried and tested.
  • byjust interchanging them by using a keyword
  • by interchanging
  • a=a+b b=a-b a=a-b
  • b=(a+b)-(a=b);
  • Don't use these tricks others are suggesting in real code. They don't work for all cases. If you don't know why look into values like "non a number", "infinity" and look at floating point arithmetic & storage when exponents are significantly different, it simply will not work and is REALLY BAD code. Moreover the temporary variable will only use an on chip register with a good compiler. What do you thing those addition and subtraction operators use? That's right a register so in practice you save nothing. You can even explicitly hint at a register in your code for static variables limited in scope e.g. register float tmpstore; Make sure this is limited in scope though, you definitely don't want to be doing this over large blocks of code, heck it's probably ignored these days anyway.
  • A bit late, but here goes: You could use the XOR method on two variables of the same type. Except if those two variables are stored in the same physical location, then it will just zero out. (but then you would'nt need to swap them?) a = a XOR b b = a XOR b a = a XOR b No temporary variable needed. XOR function: 0 xor 0 = 0 0 xor 1 = 1 1 xor 0 = 1 1 xor 1 = 0 Example: a = 00111100 b = 11110000 a = a XOR b = 11001100 b = a XOR b = 00111100 a = a XOR b = 11110000 Thus swapped.
  • b=b^a^b
  • #include<stdio.h> #include<conio.h> void main() { int m,n; printf("n ENTER VALUE OF m AND n"); scanf("%d%d",&m,&n); m=m+n; n=m-n; m=m-n; getch(); }

Copyright 2023, Wired Ivy, LLC

Answerbag | Terms of Service | Privacy Policy