https://discuss.leetcode.com/topic/87/swap-nibbles
Given a 64-Bit Unsigned Integer, write a function to accept that integer as an argument and return an integer with all the nibbles swapped.
Example:
Input: 0x0123456789ABCDEF
Output: 0x1032547698BADCFE
Given a 64-Bit Unsigned Integer, write a function to accept that integer as an argument and return an integer with all the nibbles swapped.
Example:
Input: 0x0123456789ABCDEF
Output: 0x1032547698BADCFE
uint64_t p = 0x0123456789ABCDEF;
uint64_t swapped = 0x0000000000000000;
uint64_t mask1 = 0xF000000000000000;
uint64_t mask2 = 0x0F00000000000000;
int i=0;
for(i=0;i<64;i+=8)
{
uint64_t nib1 = p & mask1>>i;
uint64_t nib2 = p & mask2>>i;
swapped |= nib1>>4;
swapped |= nib2<<4;
}
printf("%lu\n",swapped);.