https://github.com/aliosm/oneline-cpp-swap-function
Swap two integers in C++ programming language using only one line!
https://github.com/aliosm/oneline-cpp-swap-function
oneline swap
Last synced: over 1 year ago
JSON representation
Swap two integers in C++ programming language using only one line!
- Host: GitHub
- URL: https://github.com/aliosm/oneline-cpp-swap-function
- Owner: AliOsm
- Created: 2016-10-08T19:56:22.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-10-08T20:10:01.000Z (almost 10 years ago)
- Last Synced: 2025-03-29T11:44:20.984Z (over 1 year ago)
- Topics: oneline, swap
- Language: C++
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# oneline-cpp-swap-function
Swap two integers in C++ programming language using only one line!
## How is this done?
With math you can do this and alot of things! so let us declare two variables, x and y and set them to 1 and 2 respectively:
```
int x = 1, y = 2;
```
Now if we apply three equations on our variables we will swap them without declare temporary variable, the equations is:
```
x; y; // x = 1 | y = 2
x = x - y; // x = -1 | y = 2
y = x + y; // x = -1 | y = 1
x = y - x; // x = 2 | y = 1
```
And if we want we can write our equations in one line like this:
```
x = (y = (x = x - y) + y) - x;
```
Have fun :D