8:14 pm - Thursday April 25, 2019

Java Example Program to Swap number using bitwise operator

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

/**
*
* @author:canditjava.com
* @description:java example program for Swap number using bitwise operator
*/
public class Swap {

public static void main(String args[]) {
int a = 89, b = 90;
System.out.println("Before swapping:");

System.out.println("A=" + a);
System.out.println("B=" + b);

a = a ^ b; // XOR operation
b = a ^ b; // XOR operation
a = a ^ b; // XOR operation

System.out.println("After swapping:");
System.out.println("A=" + a);
System.out.println("B=" + b);
}
}


output

Before swapping:
A=89
B=90
After swapping:
A=90
B=89

Filed in: Z-A All Java Codes

Comments are closed.