Wednesday , 27 September 2017
Home » Class 10 » Swapping two numbers without using third variable [Method 1]

Swapping two numbers without using third variable [Method 1]

This is the Java programming code written in BlueJ which swaps the values of two numbers without using any third variable.

This is the first method in which we have used the concept of Bitwise XOR operator which is indicated by a caret ( ^ ).

[Note: Lessons on Bitwise XOR operator will be added soon.]

Method 2 of swapping two numbers using without using any third variable and by using the concept of simple mathematical operations including addition and subtraction can be read from here: [Method 2]

Programming Code:

/**
 * The class Swapping_Method1 takes 2 numbers as input and swaps their value without using any 3rd variable
 * This is Method 1
 * @author : www.javaforschool.com
 * @Program Type : BlueJ Program - Java
 */

import java.io.*;
class Swapping_Method1
    {
        public static void main(String args[])throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int a,b;
            System.out.print("Enter the 1st no: ");
            a=Integer.parseInt(br.readLine());
            System.out.print("Enter the 2nd no: ");
            b=Integer.parseInt(br.readLine());

            System.out.println("-------------------------------");
            System.out.println("The numbers before swapping are");
            System.out.println("a = "+a);
            System.out.println("b = "+b);

            //Beginning of Swapping
            a=a^b;
            b=a^b;
            a=a^b;
            //End of Swapping

            System.out.println("-------------------------------");
            System.out.println("The numbers after swapping are");
            System.out.println("a = "+a);
            System.out.println("b = "+b);
        }
    }

Output:

Enter the 1st no: 25
Enter the 2nd no: 13
——————————-
The numbers before swapping are
a = 25
b = 13
——————————-
The numbers after swapping are
a = 13
b = 25

Working:

Initially a=25 and b=13,

Step 1: a=a^b gives, a=25^13
Now,
Binary equivalent of 25 = 11001
Binary equivalent of 13 = 01101
=======================
25^13 = 10100 (i.e. 20)
=======================
therefore, a=a^b gives, a=20

Step 2: b=a^b gives, b=20^13
Now,
Binary equivalent of 20 = 10100
Binary equivalent of 13 = 01101
=======================
20^13 = 11001 (i.e. 25)
=======================
therefore, b=a^b gives, b=25

Step 3: a=a^b gives, a=20^25
Now,
Binary equivalent of 20 = 10100
Binary equivalent of 25 = 11001
=======================
20^25 = 01101 (i.e. 13)
=======================
therefore, a=a^b gives, a=13

Hence, finally we have a=13 and b=25. [Swapping Done]

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

6 comments

  1. very inteligent

  2. sir,what’s the use of ‘xor’ (^)?

    • Bitwise XOR (^) operator is one of the Bitwise operator which performs the Bitwise Logical exclusive OR operation.
      It operates on every bit of the operands when the operands are taken in their binary form.

      1. So, the important point to note regarding Bitwise XOR is that it operates on every individual bit of any number in it’s Binary Form.
      2. And also that the number of bits of the two operands in its binary form must be same.

      Working Principle:

      The result is 1 when the bits of both the operands are different (i.e. one bit is 1 and other bit is 0) and the result is 0 when the bits of the operands are same.
      i.e.
      0^0=0
      0^1=1
      1^0=1
      1^1=0

      Illustration:

      In the above program we used two numbers 25 and 13 and performed the Bitwise XOR operation between them.
      Binary equivalent of 25 = 11001
      Binary equivalent of 13 = 01101
      ================================
      25^13 = 10100
      ================================

      Explanation:

      Starting from the right, the right most bit of 25 was 1 and that of 13 was also 1.
      Since both the bits were 1, hence result was 0.
      Similarly for 0^0 we got 0, for 0^1 we got 1, for 1^1 we got 0 and for 1^0 we got 1.

      Detailed notes on these operators will be posted soon.

Leave a Reply to junaid Cancel reply

Your email address will not be published. Required fields are marked *