Java Program to Swap Two Numbers Using Bitwise XOR Operation Last Updated : 20 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two numbers x and y. We have to write a Java Program to Swap the contents of two numbers using Bitwise XOR Operation. Input 1: x = 5, y = 10 Output : x = 10, y = 5 Explanation : 1. x = x ^ y -> x = 15 2. y = x ^ y -> y = 5 3. x = x ^ y -> x = 10 Input 2: x = 15, y = 20 Output : x = 20, y = 15The bitwise XOR operator(represented by ^) compares corresponding bits of two operands and returns 1 if they are equal and 0 if they are not equal. Let's say we have two numbers x and y, so what actually x^y will do is that it will compare every corresponding bit of x and y, and if they are different, it will generate 1 as the output of that two bits(one of x and one of y) taken into consideration and if bits are same, then it will generate 0 as the output of two bits. Below is the code implementation of the above approach:- Java // Java program to swap the elements using XOR Operator import java.io.*; class GFG { public static void main(String[] args) { int x = 5, y = 10; // binary equivalent of 5 is 0101 // binary equivalent of 10 is 1010 // binary equivalent of x will become 1111 ie x=15 x = x ^ y; // binary equivalent of y will become 0101 ie y=5 y = x ^ y; // binary equivalent of x will become 1010 ie x=10 x = x ^ y; System.out.println("The value of x is " + x + " and the value of y is " + y); } } OutputThe value of x is 10 and the value of y is 5 Comment More infoAdvertise with us Next Article Java Program to Add Two numbers Without using Arithmetic Operator L lavishgarg26 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Add Two numbers Without using Arithmetic Operator Here, we need to write a function that returns the sum of two stated integers. And the function must not utilize any of the arithmetic operators such as +, ++, â, -, .. Etc.). It will be a very basic elementary level program to compute the sum by simply using the '+' operator and thereby simply prin 2 min read Java Program to Swap Two Numbers Problem Statement: Given two integers m and n. The goal is simply to swap their values in the memory block and write the java code demonstrating approaches.Illustration:Input : m=9, n=5Output : m=5, n=9 Input : m=15, n=5Output : m=5, n=15Here 'm' and 'n' are integer valueApproaches:There are 3 stand 6 min read Java Program to Rotate bits of a number Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at th 3 min read Java Program to Add Two Numbers Given two integers num1 and num2, the task is to find the sum of the given two numbers in Java. Example of Addition of Two Numbers Input: A = 5, B = 6Output: sum = 11 Input: A = 4, B = 11Â Output: sum = 15 Â Program to Add Two Numbers in Java Below is the implementation of adding two Numbers are ment 4 min read Java Program to Check if two numbers are bit rotations of each other or not Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y < 2^32 Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.More info 3 min read Java Program to Convert Binary Code into Gray Code Without Using Recursion Binary Code of a number is the representation of a number in Binary (base-2) number system. In Binary Number System, each number is expressed using only two literals (0 and 1). Each of these literals is called a bit. The binary number system is very useful in digital electronic circuits. Gray Code o 4 min read Like