It is similar. Just use the appropriate masks (0x000F and 0xF000) and shifts (12) to swap the highest and lowest nibble of a 16 bit value. But don't forget to let the middle bits untouched (return them too):
#define swap(v) ((((v) & 0x000F) << 12) | (((v) & 0xF000) >> 12) | ((v) & 0x0FF0))
Because the assignment is to use a macro and these don't know about the type of the passed argument, it requires also adding a cast to get the desired
short
result and casting the operand for the right shift to perform an
unsigned
shift:
#define swap(v) ((short int)((((v) & 0x000F) << 12) | (((unsigned)(v) & 0xF000) >> 12) | ((v) & 0x0FF0)))