Macros to swap variables in C.
Macros to swap variables in C.
Download: header file.
// Swap variables (xor).
ESWAP(vara, varb)
// swap a, b
ESWAP(a, b);
printf("%x %x", a, b);
// Swap variables using third variable.
ESWAP_VAR(varx, vary, type)
// define integers a, b
int a = 0x1122;
int b = 0x3344;
printf("%x %x", a, b);
// swap a, b
ESWAP_VAR(a, b, int);
printf("%x %x", a, b);
// Swap variables using temporary buffer.
ESWAP_BUF(varx, vary)
// swap a, b
ESWAP_BUF(a, b);
printf("%x %x", a, b);
// Swap variables using addition and subtraction.
ESWAP_ADDSUB(varx, vary)
// swap a, b
ESWAP_ADDSUB(a, b);
printf("%x %x", a, b);
// Swap variables using XOR.
ESWAP_XOR(varx, vary)
// swap a, b
ESWAP_XOR(a, b);
printf("%x %x", a, b);
// Swap individual bits using XOR.
ESWAP_BITSXOR(varx, vary, bits)
// swap higher 8 bits of a, b
ESWAP_BITSXOR(a, b, 0xFF00);
printf("%x %x", a, b);
// Swap individual bits.
ESWAP_BITS(varx, vary, bits)
// swap higher 8 bits of a, b
ESWAP_BITS(a, b, 0xFF00);
printf("%x %x", a, b);