Java Program to Swap two numbers using Bitwise XOR Operator?

By | August 7, 2019

In this post of Java Program to Swap two numbers using Bitwise XOR Operator. we will write a java program to swaps two numbers using bitwise XOR operator. Before we go through the program, let’s see what is a bitwise XOR operator: A bitwise XOR compares corresponding bits of two operands and returns 1 if they are not equal and 0 if they are  equal. For example:

num1 = 11;    /* equal to 00001011*/

num2 = 22; /* equal to 00010110 */

num1 ^ num2 =        00011101

num1 ^ num2 compares corresponding bits of num1 and num2 and generates 1 if they are not equal, else it returns 0 if both bits are equal. In our case it would return 29 which is binary equivalent to 00011101

Now let’s write this in a Java program:

Sample output of above program::

You may also like –

How to convert number to words in java
How to swap two numbers with or without temporary variable in java
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
Java Program to Calculate the Power of a Number
Check whether a number is prime or not

Thanks for visiting blog. If you have any doubts or any suggestions to make please drop a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *