Showing posts with label xor operator. Show all posts
Showing posts with label xor operator. Show all posts

Saturday, 24 January 2015

Swap two number using bit operator

Swap two number using bitwise operator.

We use  ^ (XOR) operator for this.

public class SwapNumberUsingBitOperator {

public static void main(String[] args) {
int i=9;
int j=4;
System.out.println("Before swap i="+i+"j="+j);
i=i^j;
j=i^j;
i=i^j;
System.out.println("After Swap i="+i+"j="+j);
}

}