Skip to content

cppf/extra-swap

Repository files navigation

Macros to swap variables in C.

Download: header file.

default

// Swap variables (xor).
ESWAP(vara, varb)
// swap a, b
ESWAP(a, b);
printf("%x %x", a, b);

var

// 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);

buf

// Swap variables using temporary buffer.
ESWAP_BUF(varx, vary)
// swap a, b
ESWAP_BUF(a, b);
printf("%x %x", a, b);

addsub

// Swap variables using addition and subtraction.
ESWAP_ADDSUB(varx, vary)
// swap a, b
ESWAP_ADDSUB(a, b);
printf("%x %x", a, b);

xor

// Swap variables using XOR.
ESWAP_XOR(varx, vary)
// swap a, b
ESWAP_XOR(a, b);
printf("%x %x", a, b);

bitsxor

// 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);

bits

// 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);



cppf