Professional Documents
Culture Documents
New chat
java Copy code
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:
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