fork download
  1. #include <limits.h>
  2. #include <assert.h>
  3. #include <stdio.h>
  4.  
  5. unsigned pad(unsigned pattern, unsigned patternLen,
  6. unsigned leftBit, unsigned leftBitCnt,
  7. unsigned rightBit, unsigned rightBitCnt)
  8. {
  9. unsigned r;
  10. assert(leftBitCnt < sizeof(unsigned) * CHAR_BIT);
  11. assert(rightBitCnt < sizeof(unsigned) * CHAR_BIT);
  12. assert(patternLen < sizeof(unsigned) * CHAR_BIT);
  13. assert(leftBitCnt + patternLen + rightBitCnt <= sizeof(unsigned) * CHAR_BIT);
  14.  
  15. r = (leftBit << leftBitCnt) - leftBit;
  16. r <<= patternLen;
  17. r |= pattern;
  18. r <<= rightBitCnt;
  19. r |= (rightBit << rightBitCnt) - rightBit;
  20.  
  21. return r;
  22. }
  23.  
  24. void printBin(unsigned x)
  25. {
  26. unsigned i;
  27. for (i = 0; i < sizeof(unsigned) * CHAR_BIT; i++)
  28. printf("%u", (x >> (sizeof(unsigned) * CHAR_BIT - 1 - i)) & 1);
  29. printf("\n");
  30. }
  31.  
  32. int main(void)
  33. {
  34. printBin(pad(0x0F0, 12, 0, 2, 0, 2));
  35. printBin(pad(0x0F0, 12, 0, 2, 1, 2));
  36. printBin(pad(0x0F0, 12, 1, 2, 0, 2));
  37. printBin(pad(0x0F0, 12, 1, 2, 1, 2));
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.02s 1676KB
stdin
Standard input is empty
stdout
00000000000000000000001111000000
00000000000000000000001111000011
00000000000000001100001111000000
00000000000000001100001111000011