How can we swap two numbers without third variable and without arithmetic operator?
            Asked
            
        
        
            Active
            
        
            Viewed 1.4k times
        
    4
            
            
        - 
                    2One possible duplicate: http://stackoverflow.com/questions/756750/swap-the-values-of-two-variables-without-using-third-variable – Florin Oct 14 '10 at 13:12
- 
                    1Why would you want to do that? – Klaus Byskov Pedersen Oct 14 '10 at 13:12
- 
                    2Another possible duplicate: http://stackoverflow.com/questions/1826159/swapping-two-variable-value-without-using-3rd-variable – Florin Oct 14 '10 at 13:13
5 Answers
9
            Does XOR count as an arithmetic Operator? If not, then:
X := X XOR Y
Y := X XOR Y
X := X XOR Y
Converting this pseudo-code to Java code that compiles is left as an exercise to the reader (with regards to the ‘java’ tag).
 
    
    
        Florian Mayer
        
- 3,041
- 24
- 17
- 
                    1If I remember correctly, though, this won't work if one of the values you're trying to swap is 0. – Anthony Oct 14 '10 at 13:13
- 
                    
- 
                    Funny, I always thought that XOR was arithmetic, but at least it isn't the +, -, *, or / operators the poster is probably thinking about. – Edwin Buck Oct 14 '10 at 14:06
- 
                    @Edwin, @Florian: XOR is a bitwise operator, so this fits the given requirements. @Shynthriir: 0 is fine, but it *is* a problem if X and Y refer to the same location in memory. – Michael Madsen Oct 14 '10 at 14:41
1
            
            
        private static void swap() {
    int a = 5;
    int b = 6;
    System.out.println("Before Swaping: a = " + a + " and b= " + b);
    // swapping value of two numbers without using temp variable and XOR bitwise operator
    a = a ^ b; // now a is 3 and b is 6
    b = a ^ b; // now a is 3 but b is 5 (original value of a)
    a = a ^ b; // now a is 6 and b is 5, numbers are swapped
    System.out.println("After  Swaping: a = " + a + " and b= " + b);
}
Output :
Before Swaping: a = 5 and b= 6
After  Swaping: a = 6 and b= 5
 
    
    
        Sameer Shrestha
        
- 105
- 4
1
            
            
        public class SwapNumbers {
 public static void main(String[] args) {
  int x = 11;
  int y = 22;
  System.out.println("Before Swapping");
  System.out.println("Value of x is :" + x);
  System.out.println("Value of y is :" + y);
  x = x + y;
  y = x - y;
  x = x - y;
  System.out.println("Before Swapping");
  System.out.println("Value of x is :" + x);
  System.out.println("Value of y is :" + y);
 }
}
 
    
    
        Hemlata Gehlot
        
- 341
- 2
- 12
-1
            
            
        class Swap{
 int a;
 int b; 
 public static void main(String args[]){
    a = 10;
    b =20;
    System.out.println("******Before Swap*********");
    System.out.println("a= "+a);
    System.out.println("b= "+b);
    a = a + b;
    b = a - b;
    a = a - b;
    System.out.println("******After Swap*********");
    System.out.println("a= "+a);
    System.out.println("b= "+b);
 } 
}
Your Output will be :
******Before Swap*********
a= 10
b= 20
******After Swap*********
a= 20
b= 10
 
    
    
        ritesh9984
        
- 418
- 1
- 5
- 17
-4
            
            
        Using this ::
X = X ^ Y;
Y = X ^ Y;
X = X ^ Y;
*** ^ means XOR operation
 
    
    
        Saiful
        
- 1,891
- 1
- 15
- 39
- 
                    But + and - are arithmetic operators which is exactly what you're not allowed to use. – Dan Jan 11 '12 at 08:05
- 
                    2Ok. check this http://stackoverflow.com/questions/756750/swap-the-values-of-two-variables-without-using-third-variable. – Saiful Jan 11 '12 at 08:33
- 
                    Rolled back the inappropriate edit which just changed this answer into the other one. – Erick Robertson Jan 13 '14 at 14:36
 
    