Skip to content

Instantly share code, notes, and snippets.

@krishvishal
Last active June 18, 2024 08:05
Show Gist options
  • Save krishvishal/47e470da10e13e4cd4950d5febe9f157 to your computer and use it in GitHub Desktop.
Save krishvishal/47e470da10e13e4cd4950d5febe9f157 to your computer and use it in GitHub Desktop.
Swap variables without using temporaries in Julia
function swap(a, b)
a = xor(a, b)
b = xor(a, b)
a = xor(a, b)
return a, b
end
a, b = 1, 2
println(a, " ", b)
a, b = swap(a, b)
println(a, " ", b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment