From 2f5172d92c21e1f7f0a247a3ddf8c89b7918b35d Mon Sep 17 00:00:00 2001 From: Balamurugan315 Date: Wed, 1 Oct 2025 10:03:28 +0530 Subject: [PATCH 1/3] Add XOR-based swap algorithm for two numbers --- bit_manipulation/xor_swap_two_integers.py | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 bit_manipulation/xor_swap_two_integers.py diff --git a/bit_manipulation/xor_swap_two_integers.py b/bit_manipulation/xor_swap_two_integers.py new file mode 100644 index 000000000000..5257b644358a --- /dev/null +++ b/bit_manipulation/xor_swap_two_integers.py @@ -0,0 +1,38 @@ +# Information on XOR swap: https://en.wikipedia.org/wiki/Bitwise_operation#XOR + +# Algorithm: +# 1. Take two integers a and b. +# 2. Apply XOR between a and b and store the result in a: +# a = a ^ b +# 3. XOR the new value of a with b to get the original value of a and store it in b: +# b = a ^ b +# 4. XOR the new value of a with the new value of b. +# This gives the original value of b, which we store in a: +# a = a ^ b +# 5. Return the swapped values (a, b). +# This method swaps two numbers without using a temporary variable. + + +def xor_swap(a: int, b: int) -> tuple[int, int]: + """ + Swap two integers using bitwise XOR operation and return the swapped values. + + >>> xor_swap(5, 10) + (10, 5) + >>> xor_swap(0, 0) + (0, 0) + >>> xor_swap(-1, 1) + (1, -1) + >>> xor_swap(123, 456) + (456, 123) + """ + a = a ^ b + b = a ^ b + a = a ^ b + return a, b + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 8271669c7f202687b4197d840cbec6536e72c4a9 Mon Sep 17 00:00:00 2001 From: cclauss Date: Wed, 23 Sep 2026 21:32:23 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index e387ce355396..a91cedb78b44 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -106,6 +106,7 @@ * [Rotate Bits](bit_manipulation/rotate_bits.py) * [Single Bit Manipulation Operations](bit_manipulation/single_bit_manipulation_operations.py) * [Swap All Odd And Even Bits](bit_manipulation/swap_all_odd_and_even_bits.py) + * [Xor Swap Two Integers](bit_manipulation/xor_swap_two_integers.py) ## [Blockchain](blockchain) * [Diophantine Equation](blockchain/diophantine_equation.py) From 6bf9f62f16b604d7d51722966cc1aad74b0e99af Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 23 Sep 2026 23:35:28 +0200 Subject: [PATCH 3/3] Include additional test case for xor_swap function Add example for xor_swap with larger integers --- bit_manipulation/xor_swap_two_integers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bit_manipulation/xor_swap_two_integers.py b/bit_manipulation/xor_swap_two_integers.py index 5257b644358a..3c558eb9a0f9 100644 --- a/bit_manipulation/xor_swap_two_integers.py +++ b/bit_manipulation/xor_swap_two_integers.py @@ -25,6 +25,8 @@ def xor_swap(a: int, b: int) -> tuple[int, int]: (1, -1) >>> xor_swap(123, 456) (456, 123) + >>> xor_swap(12345, 54321) + (54321, 12345) """ a = a ^ b b = a ^ b