Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Bit-Manipulation/SwapWithoutTemp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function swapWithoutTemp(a, b) {
a = a ^ b; // Step 1: XOR a and b, store result in a
b = a ^ b; // Step 2: XOR new a (a ^ b) and original b, store result in b (now b = a)
a = a ^ b; // Step 3: XOR new a (a ^ b) and new b (a), store result in a (now a = b)
}

let a = 5;
let b = 7;

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

swapWithoutTemp(a, b);

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