Below java program going to swap two numbers using bitwise XOR operator. A bitwise XOR operator comparing bits of two operands and if they are equal then returns 1 and if they are not equal then returns 0.

For example:

number1 = 5; /* binary equal to 00000101*/

number2 = 10; /* binary equal to 00001010 */

number1 ^ number2 compares corresponding bits of number1 and number2 and generates 1 if they are equal and if not equal returns 0.

Example: Swapping two numbers using Bitwise operator

import java.util.Scanner; public class BitwiseSwap { public static void main(String args[]) { int number1, number2; Scanner scanner = new Scanner(System.in); System.out.print("Enter 1st number:"); number1 = scanner.nextInt(); System.out.print("Enter 2nd number:"); number2 = scanner.nextInt();number1 = number1 ^ number2; number2 = number1 ^ number2; number1 = number1 ^ number2; scanner.close(); System.out.println("1st number after swapping:"+number1); System.out.println("2nd number after swapping:"+number2); }

}