Swapping a byte's nibbles

For those compilers that do not have a built-in function for swapping a byte's nibbles, a simple user function can be created:


Code C - [expand]
1
2
3
4
unsigned char SwapNibbles (unsigned char x)
{
  return ((x >> 4) & 0x0F) | ((x << 4) & 0xF0);
}



And a couple of function call examples:


Code C - [expand]
1
2
3
unsigned char ch1, ch2;
ch1 = SwapNibbles(0xA3);  //ch1=0x3A
ch2 = SwapNibbles(ch1);   //ch2=0xA3



Happy coding!
Alexandros

Comments

There are no comments to display.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…