Swap two numbers

Easy Bit Manipulation XOR Basics Original on GeeksforGeeks

Reported atHCLTech

Swap two numbers is a easy bit manipulation problem solved with the xor basics pattern. The best approach, xor swap, runs in O(1) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from temporary variable up.

Problem

Swap two integers without using a third variable, using XOR. It is rarely used in real code, but it is the classic exercise for understanding that XOR cancels itself.

Examples

Example 1

Input
a = 13, b = 9
Output
a = 9, b = 13

Example 2

Input
a = 15, b = 15
Output
a = 15, b = 15

Constraints

  • Swap without a temporary variable.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 8 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Temporary variableO(1)O(1)
XOR swapO(1)O(1)

1Temporary variable

TimeO(1)
SpaceO(1)

The usual way, shown for comparison.

  1. t = a; a = b; b = t.
class Solution {
    static int[] swap(int a, int b) {
        int t = a;
        a = b;
        b = t;
        return new int[] { a, b };
    }
}

2XOR swap

TimeO(1)
SpaceO(1)

After a ^= b, a holds a^b. Then b ^= a gives b ^ a ^ b = a. Then a ^= b gives (a^b) ^ a = b.

  1. a ^= b; b ^= a; a ^= b.
class Solution {
    static int[] swap(int a, int b) {
        a ^= b;
        b ^= a;
        a ^= b;
        return new int[] { a, b };
    }
}

Edge cases to test

  • a and b refer to the same memory location (XOR swap then zeroes it)
  • Equal values (fine)

Hints

Hint 1

x ^ x = 0 and x ^ 0 = x. Apply a ^= b, b ^= a, a ^= b.

FAQ

What is the best time complexity for Swap two numbers?

XOR swap runs in O(1) time and O(1) extra space.

Which pattern does Swap two numbers use?

It is a bit manipulation problem that uses the xor basics pattern. Other problems with the same pattern: Hamming Distance, Count Paths That Can Form a Palindrome in a Tree, XOR Operation in an Array.

Is there a brute force solution for Swap two numbers?

Yes. Temporary variable takes O(1) time and O(1) space. The usual way, shown for comparison.

Which edge cases should I test for Swap two numbers?

a and b refer to the same memory location (XOR swap then zeroes it); Equal values (fine).