Home

How do you Nibble swap using bitwise operators?

Answer:

Answer

If i is an unsigned char (1 byte unsigned integer) then:
i = (i << 4) | (i >> 4);

If you know how to write inline assembly on your compiler (it differs between compilers), then rotating the byte is much more straightforward way to swap nibbles. In Intel assembly, it would look like:
ror i, 4
So in Microsoft compilers it would look like:
__asm
{

ror i,4

}

Related answers:
Can you answer these?