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]

very inteligent

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.

every individual bitof any number in it’sBinary Form.number of bitsof the two operands in its binary formmust 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.