-
Notifications
You must be signed in to change notification settings - Fork 136
/
one_line_swap.c
33 lines (25 loc) · 1.61 KB
/
one_line_swap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Intro- Strange one-liners that swaps value of two variables.
#include <stdio.h>
main(){
int a=5, b=7;
a-=b=(a+=b)-b; // Developer- Minhas Kamal (https://minhaskamal.github.io)
// Details- https://cloud.githubusercontent.com/assets/5456665/21447433/bc21ad60-c8fe-11e6-9154-b3b682cad607.png
printf("%d %d\n", a, b);
a=(int[]){b, b=a}[0]; // Expanded- int[] arr = {b, b=a}; a = arr[0];
printf("%d %d\n", a, b);
a^=b^=a^=b; // Developer- Chris Taylor (http://chris-taylor.github.io/)
// Details- http://chris-taylor.github.io/blog/2013/02/25/xor-trick/
printf("%d %d\n", a, b);
a=b+a-(b=a); //Details- http://stackoverflow.com/questions/20800684/why-is-a-ab-b-a-a-bad-choice-for-swapping-two-integers
printf("%d %d\n", a, b);
// This python code swaps value of two variables-
// a, b = b, a
// Details- https://stackoverflow.com/questions/21047524/how-does-swapping-of-members-in-the-python-tuples-a-b-b-a-work-internally
// Bonus- Get the max number without using logical operation
int m = (a+b+((a-b)>>sizeof(int)*8-1|1)*(a-b))/2; // Developer- Minhas Kamal (https://minhaskamal.github.io)
// Details- https://stackoverflow.com/a/41736445/4684058
// Another Solution- If both both numbers are positive
// then this works too:
// m = (int[]){a,b}[1-!!(a/b)]
printf("%d \n", m);
}