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

}

First answer by ID0995891549. Last edit by Sudennis315. Contributor trust: 30 [recommend contributor recommended]. Question popularity: 3 [Recommended].