Skip to content

Instantly share code, notes, and snippets.

@andrejrakic
Created August 26, 2021 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrejrakic/3f972eba41d28a5839d2de80120bf95d to your computer and use it in GitHub Desktop.
Save andrejrakic/3f972eba41d28a5839d2de80120bf95d to your computer and use it in GitHub Desktop.
Simple smart contract to swap two variables in Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
// Simple contract to swap two variables in Solidity
contract Swapper {
uint8 public x = 1;
uint8 public y = 2;
function swap() public {
/**
* Old school way
*
* uint8 temp = x;
* x = y;
* y = temp;
*/
// more cool way 😎
(x, y) = (y, x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment