From 83e97337a607bb514a10583d7f0d2f20eba808f3 Mon Sep 17 00:00:00 2001 From: Quan Yang Date: Sat, 2 Oct 2021 02:33:25 -0700 Subject: [PATCH] Include a snippet for swapping two variables using bitwise operators (#229) This swaps the two values in the two variables without needing a third variable. --- contents/algorithms/binary.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contents/algorithms/binary.md b/contents/algorithms/binary.md index c11dd1f3..fa4ee73a 100644 --- a/contents/algorithms/binary.md +++ b/contents/algorithms/binary.md @@ -18,6 +18,7 @@ Some helpful utility snippets: - Turn off kth bit: `num &= ~(1 << k)`. - Toggle the kth bit: `num ^= (1 << k)`. - To check if a number is a power of 2, `(num & num - 1) == 0` or `(num & (-num)) == num`. +- Swapping two variables: `num1 ^= num2; num2 ^= num1; num1 ^= num2` ## Corner cases