15 Write code that swaps the most significant 16 bits and th
15. Write code that swaps the most significant 16 bits and the least significant 16 bits of a 32-bit int variable x For example, if x initially contains the value 0x12345678, the new value should be 0x56781234. You may use intermediate variables int swap int x) TODO: add your code here
Solution
unsigned int reverse(unsigned int t) { size_t n = t;//store in 64 bit number for call to BSWAP __asm__(\"BSWAP %0\" : \"=r\"(n) : \"0\"(n)); n >>= ((sizeof(size_t) - sizeof(unsigned int)) * 8); n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1); n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2); n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4); return n; }