SWAP
1
Name:
Anonymous
2011-10-17 15:25
#define SWAP(a, b) ((&(a) == &(b)) || \
(((a) -= (b)), ((b) += (a)), ((a) = (b) - (a))))
ENTERPRISE SWAPPING
inb4 overflow
2
Name:
Anonymous
2011-10-17 17:08
overflow wraps around anyway
3
Name:
Anonymous
2011-10-17 17:37
#define SWAP(a,b,c) c = a;\
a = b;\
a = c
4
Name:
Anonymous
2011-10-17 17:43
>>3
This is a masterpiece.
5
Name:
Anonymous
2011-10-17 17:49
>>1
floating point precision
6
Name:
Anonymous
2011-10-17 18:31
7
Name:
Anonymous
2011-10-17 20:02
Jesus, that's clever,
>>1 .
Show more of your work!
|10/10 |
8
Name:
Anonymous
2011-10-17 20:31
>>7
Let x,y,m be integers
Maximum Function:
m = x ^ ((x ^ y) & -(x < y));
where r ends up as the maximum of x and y
Minimum Function:
m = y ^ ((x ^ y) & -(x < y));
where r ends up as the minimum of x and y
9
Name:
swagger
2011-10-17 21:05
#define SWAG(a, b) ((&(a) == &(b)) || \
(((a) -= (b)), ((b) += (a)), ((a) = (b) - (a))))
10
Name:
Anonymous
2011-10-17 21:10
>>9
not enough!
#define SWAG(a, b) do((&(a) == &(b)) || \
(((a) -= (b)), ((b) += (a)), ((a) = (b) - (a))))while(0)
11
Name:
Anonymous
2011-10-17 21:23
>>10
guess I got my swagger back
12
Name:
Anonymous
2011-10-17 22:17
That's really clever, and it's cool to see, but xor swap is probably faster, seeing how the xor operation is simpler to do than addition (at least I as a human being could do an xor a lot faster than an addition. I don't know about how intense modern architectures are...)
#define eXchange(a, b) do{(&(a) == &(b)) || \
((a) ^= (b), \
(b) ^= (a), \
(a) ^= (b))}while(0)
13
Name:
Anonymous
2011-10-17 23:32
>>11
TRUTH
NEW WATCH ALERT
14
Name:
Anonymous
2011-10-17 23:54
mov eax, [a]
xchg eax, [b]