-
Notifications
You must be signed in to change notification settings - Fork 6
/
SwapNumbers.java
30 lines (23 loc) · 1.02 KB
/
SwapNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* The SwapNumbers program implements an application that
* swap two given numbers using of Bitwise XOR in Java.
* Displays the result to the standard output
*
* @author Sarju S
* @version 1.0
* @since 2020-09-18
*/
package com.sjcet.basicPrograms;
public class SwapNumbers {
public static void main(String[] args) {
int firstNumber = 10; // Binary equivalent of 10 is 1010
int secondNumber = 5;// Binary equivalent of 5 is 0101
System.out.println("The First number before swapping:"+firstNumber);
System.out.println("The Second number before swapping:"+secondNumber);
firstNumber = firstNumber ^ secondNumber;//firstNumber becomes 1111 = 15
secondNumber = firstNumber ^ secondNumber;//secondNumber becomes 1010 = 10
firstNumber = firstNumber ^ secondNumber;//firstNumber becomes 0101 = 5
System.out.println("The First number after swapping:"+firstNumber);
System.out.println("The Second number after swapping:"+secondNumber);
}
}