Include a snippet for swapping two variables using bitwise operators ()

This swaps the two values in the two variables without needing a third variable.
pull/231/head
Quan Yang 4 years ago committed by GitHub
parent 99b0e687b0
commit 83e97337a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,6 +18,7 @@ Some helpful utility snippets:
- Turn off k<sup>th</sup> bit: `num &= ~(1 << k)`.
- Toggle the k<sup>th</sup> 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

Loading…
Cancel
Save