You are on page 1of 1

7/3/23, 11:00 AM ChatGPT

New chat
java Copy code

public class NumberSwap {


public static void main(String[] args) {
int a = 5;
int b = 10;

System.out.println("Before swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);

a = a ^ b;
b = a ^ b;
a = a ^ b;

System.out.println("\nAfter swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}

Output:

less Copy code

Before swapping:
a = 5
b = 10

After swapping:
a = 10
b = 5

In this Java example, we use the XOR bitwise operator to swap the values of `a` and `b`.
The XOR operation between two values returns a new value that contains the bits set in
either of the operands but not both. By performing XOR operations with `a` and `b`, the
values are interchanged without the need for a third variable.
Send a message

Free Research Preview. ChatGPT may produce inaccurate information about people, places, or facts. ChatGPT May 24 Version

https://chat.openai.com/c/2a082408-3bce-46a6-93f6-1a536652da99 1/1

You might also like