Skip to content

Commit 575e697

Browse files
authored
Merge pull request thuva4#372 from Anat-Port/master
Added Xor Swap in Python, JavaScript and C#
2 parents aa9a229 + 7dd18b1 commit 575e697

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,4 @@ Unfortunately, sometimes the bug can be only reproduced in your project or in yo
154154
- [Judar Lima](https://github.com/judarlima)
155155
- [Jhalaa](https://github.com/jhalaa)
156156
- [langlk](https://github.com/langlk)
157+
- [Anat Portnoy](https://github.com/Anat-Port)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ TernarySearch | :+1: |:+1: | | :+1: | :+1: | | | |
6161
Topological Sort | | | | | :+1: | | | |
6262
Segmented Sieve |:+1:| :+1: | | | :+1: | | | |
6363
Union Find |:+1:|:+1:| | :+1: | | | | |
64-
Xor swap |:+1:| | | | | | | |
64+
Xor swap |:+1:|:+1:| | | |:+1:|:+1:| |
6565
Connected-component labeling | | | | |:+1:| | | |
6666

6767

XorSwap/C#/XorSwap.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
public class XorSwap
4+
{
5+
void Swap( ref int a, ref int b)
6+
{
7+
a ^= b;
8+
b ^= a;
9+
a ^= b;
10+
}
11+
12+
void Main()
13+
{
14+
int a = 5;
15+
int b = 10;
16+
XorSwap(ref a, ref b);
17+
}
18+
}
19+
20+

XorSwap/JavaScript/XorSwap.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* xorSwap
3+
*
4+
* Swaps two variables without using a temporary variable
5+
*
6+
*/
7+
function xorSwap()
8+
{
9+
var a = 5, b = 10;
10+
a = a ^ b;
11+
b = a ^ b;
12+
a = a ^ b;
13+
14+
console.log("a = " + a + ", b = " + b);
15+
}
16+
17+

XorSwap/Python/XorSwap.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Swaps two variables without using a temporary variable
2+
def xorswap(a, b):
3+
a = a ^ b
4+
b = a ^ b
5+
a = a ^ b
6+
return a, b
7+
8+
a = 5
9+
b = 10
10+
a, b = xorswap(a, b)
11+
print (a,b)

0 commit comments

Comments
 (0)