fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. int main(void)
  5. {
  6. uint16_t w = 0xb2e3; // our 16 bit word
  7. uint16_t mask0 = 0x0001; // mask for LS bit
  8. uint16_t mask1 = 0x8000; // mask for MS bit
  9. uint16_t shift = 15; // distance between high and low bit positions
  10. int b;
  11.  
  12. for (b = 0; b < 8; ++b) // for each pair of low/high bits
  13. {
  14. uint16_t b0 = w & mask0; // get low bit
  15. uint16_t b1 = w & mask1; // get high bit
  16. w &= ~(mask0 | mask1); // clear low/high bit in word
  17. b0 <<= shift; // swap bit positions
  18. b1 >>= shift;
  19. w |= (b0 | b1); // insert swapped bits back into word
  20. mask0 <<= 1; // update masks for next pair of bits
  21. mask1 >>= 1;
  22. shift -= 2; // update distance for next pair of bits
  23. }
  24.  
  25. printf("%#x\n", w); // w should now contain 0xc74d
  26.  
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
0xc74d