From d73e01700b9e38057132c04e1ddad84225402e94 Mon Sep 17 00:00:00 2001 From: Quan Yang Date: Fri, 1 Oct 2021 13:43:30 -0700 Subject: [PATCH] Include a snippet for swapping two variables using bitwise operators 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