Skip to content

Commit

Permalink
Merge pull request #6788 from Acuspeedster/master
Browse files Browse the repository at this point in the history
feat: Implement XOR swap algorithm in JavaScript
  • Loading branch information
AdiChat authored Oct 3, 2024
2 parents 81fe641 + afed82b commit 06d3d81
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions code/bit_manipulation/src/xor_swap/xor_swap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Part of Cosmos by OpenGenus Foundation

function xorSwap(a, b) {
console.log(`Before swap: a = ${a}, b = ${b}`);

a = a ^ b; // Step 1: a now contains the XOR of both
b = a ^ b; // Step 2: b gets the original value of a
a = a ^ b; // Step 3: a gets the original value of b

console.log(`After swap: a = ${a}, b = ${b}`);
}

// Example usage:
let x = 5;
let y = 10;
xorSwap(x, y);

0 comments on commit 06d3d81

Please sign in to comment.