12.04.2016, 17:54
Variable swap macro, without using temporary variables.
Steps taken:
1. OR the NOT bits (a.k.a. XOR) of var 2 into var 1.
2. OR the new NOT bits of var 1 into var 2.
3. OR the new NOT bits of var 2 into var 1.
Walkthrough (given that var1 is 10111000 and var2 is 01001000):
pawn Код:
#define swapvars(%0,%1)\
(((%0) ^= (%1)), ((%1) ^= (%0)), ((%0) ^= (%1)))
1. OR the NOT bits (a.k.a. XOR) of var 2 into var 1.
2. OR the new NOT bits of var 1 into var 2.
3. OR the new NOT bits of var 2 into var 1.
Walkthrough (given that var1 is 10111000 and var2 is 01001000):
pawn Код:
//Step 1: OR the NOT bits (a.k.a. XOR) of var 2 into var 1.
10111000 // var1
^
01001000 // var2
=
11110000 // new var1
//Step 2: OR the new NOT bits of var 1 into var 2.
01001000 // var2
^
11110000 // new var1
=
10111000 // new, final var2 (which is the old bit of var1)
//Step 2: OR the new NOT bits of var 2 into var 1.
11110000 // new var1
^
10111000 // final var2
=
01001000 // final var1 (which is the old bit of var2)